From bfea8ab1db8866157b39a05e18de179eef544a96 Mon Sep 17 00:00:00 2001 From: Patrick Schönberger Date: Mon, 20 Apr 2026 14:53:03 +0200 Subject: fix build (remove root.zig), change websocket handling --- src/main.zig | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index f06c7ff..107a256 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,9 +1,17 @@ const std = @import("std"); const Io = std.Io; -const default = @import("default"); +fn handle_websocket(websocket: *std.http.Server.WebSocket) void { + websocket.writeMessage("welcome", .text) catch return; -fn handle_request(io: std.Io, stream: std.Io.net.Stream) !void { + while (true) { + const sm = websocket.readSmallMessage() catch break; + std.debug.print("websocket received: {s}\n", .{sm.data}); + websocket.writeMessage(sm.data, sm.opcode) catch break; + } +} + +fn handle_request(io: std.Io, stream: std.Io.net.Stream) void { var recv_buffer: [999]u8 = undefined; var send_buffer: [100]u8 = undefined; @@ -19,20 +27,15 @@ fn handle_request(io: std.Io, stream: std.Io.net.Stream) !void { switch (req.upgradeRequested()) { .websocket => |ws| { std.debug.print("ws: {s}\n", .{ws.?}); - var websocket = req.respondWebSocket(.{ .key = ws.? }) catch { - std.debug.print("error responding\n", .{}); - break; - }; - std.debug.print("responded, now we wait\n", .{}); - try websocket.writeMessage("abcde", .text); - try websocket.flush(); - const sm = try websocket.readSmallMessage(); - std.debug.print("sm: {s}\n", .{sm.data}); + var websocket = req.respondWebSocket(.{ .key = ws.? }) catch break; + std.debug.print("handling websocket business\n", .{}); + handle_websocket(&websocket); + std.debug.print("done handling websocket business\n", .{}); }, else => {}, } - try req.respond( + req.respond( \\ \\

hallo

- , .{ .status = .ok }); + , .{ .status = .ok }) catch break; } - std.debug.print("closing thread\n", .{}); + std.debug.print("closing http thread\n", .{}); } pub fn main(init: std.process.Init) !void { @@ -71,9 +75,8 @@ pub fn main(init: std.process.Init) !void { while (true) { const stream = try net_server.accept(io); - const future = io.async(handle_request, .{ io, stream }); - _ = future; + _ = io.async(handle_request, .{ io, stream }); - std.debug.print("creating thread\n", .{}); + std.debug.print("created http thread\n", .{}); } } -- cgit v1.2.3