treesummaryrefslogcommitdiff
path: root/src
diff options
context:
space:
mode:
authorPatrick Schönberger2026-04-20 14:53:03 +0200
committerPatrick Schönberger2026-04-20 14:53:03 +0200
commitbfea8ab1db8866157b39a05e18de179eef544a96 (patch)
treee756a4c7019532bdadcc097bc8095c994591178d /src
parent7b2c51a981d32bb256d91e53831c30db5fc6a15e (diff)
downloadzhttpws-bfea8ab1db8866157b39a05e18de179eef544a96.tar.gz
zhttpws-bfea8ab1db8866157b39a05e18de179eef544a96.zip
fix build (remove root.zig), change websocket handling
Diffstat (limited to 'src')
-rw-r--r--src/main.zig37
1 files changed, 20 insertions, 17 deletions
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(
\\ <script>
\\ const socket = new WebSocket("ws://localhost:1234");
\\ socket.addEventListener("open", (event) => {
@@ -41,12 +44,13 @@ fn handle_request(io: std.Io, stream: std.Io.net.Stream) !void {
\\ socket.addEventListener("message", (event) => {
\\ console.log("Message from server ", event.data);
\\ });
+ \\ socket.send("i am here");
\\ </script>
\\ <p>hallo</p>
- , .{ .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", .{});
}
}