const std = @import("std"); const Io = std.Io; const default = @import("default"); fn handle_request(io: std.Io, stream: std.Io.net.Stream) !void { var recv_buffer: [999]u8 = undefined; var send_buffer: [100]u8 = undefined; defer stream.close(io); var connection_br = stream.reader(io, &recv_buffer); var connection_bw = stream.writer(io, &send_buffer); var server = std.http.Server.init(&connection_br.interface, &connection_bw.interface); while (true) { var req = server.receiveHead() catch break; 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}); }, else => {}, } try req.respond( \\ \\
hallo
, .{ .status = .ok }); } std.debug.print("closing thread\n", .{}); } pub fn main(init: std.process.Init) !void { // Prints to stderr, unbuffered, ignoring potential errors. std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); // This is appropriate for anything that lives as long as the process. const arena: std.mem.Allocator = init.arena.allocator(); // Accessing command line arguments: const args = try init.minimal.args.toSlice(arena); for (args) |arg| { std.log.info("arg: {s}", .{arg}); } // In order to do I/O operations need an `Io` instance. const io = init.io; const address = try std.Io.net.IpAddress.parseIp4("0.0.0.0", 1234); var net_server = try address.listen(io, .{ .reuse_address = true }); while (true) { const stream = try net_server.accept(io); const future = io.async(handle_request, .{ io, stream }); _ = future; std.debug.print("creating thread\n", .{}); } }