diff options
| -rw-r--r-- | build.zig | 41 | ||||
| -rw-r--r-- | src/main.zig | 37 |
2 files changed, 20 insertions, 58 deletions
@@ -21,26 +21,6 @@ pub fn build(b: *std.Build) void { // target and optimize options) will be listed when running `zig build --help` // in this directory. - // This creates a module, which represents a collection of source files alongside - // some compilation options, such as optimization mode and linked system libraries. - // Zig modules are the preferred way of making Zig code available to consumers. - // addModule defines a module that we intend to make available for importing - // to our consumers. We must give it a name because a Zig package can expose - // multiple modules and consumers will need to be able to specify which - // module they want to access. - const mod = b.addModule("default", .{ - // The root source file is the "entry point" of this module. Users of - // this module will only be able to access public declarations contained - // in this file, which means that if you have declarations that you - // intend to expose to consumers that were defined in other files part - // of this module, you will have to make sure to re-export them from - // the root file. - .root_source_file = b.path("src/root.zig"), - // Later on we'll use this module as the root module of a test executable - // which requires us to specify a target. - .target = target, - }); - // Here we define an executable. An executable needs to have a root module // which needs to expose a `main` function. While we could add a main function // to the module defined above, it's sometimes preferable to split business @@ -70,16 +50,6 @@ pub fn build(b: *std.Build) void { // definition if desireable (e.g. firmware for embedded devices). .target = target, .optimize = optimize, - // List of modules available for import in source files part of the - // root module. - .imports = &.{ - // Here "default" is the name you will use in your source code to - // import this module (e.g. `@import("default")`). The name is - // repeated because you are allowed to rename your imports, which - // can be extremely useful in case of collisions (which can happen - // importing modules from different packages). - .{ .name = "default", .module = mod }, - }, }), }); @@ -115,16 +85,6 @@ pub fn build(b: *std.Build) void { run_cmd.addArgs(args); } - // Creates an executable that will run `test` blocks from the provided module. - // Here `mod` needs to define a target, which is why earlier we made sure to - // set the releative field. - const mod_tests = b.addTest(.{ - .root_module = mod, - }); - - // A run step that will run the test executable. - const run_mod_tests = b.addRunArtifact(mod_tests); - // Creates an executable that will run `test` blocks from the executable's // root module. Note that test executables only test one module at a time, // hence why we have to create two separate ones. @@ -139,7 +99,6 @@ pub fn build(b: *std.Build) void { // times and since the two run steps do not depend on one another, this will // make the two of them run in parallel. const test_step = b.step("test", "Run tests"); - test_step.dependOn(&run_mod_tests.step); test_step.dependOn(&run_exe_tests.step); // Just like flags, top level steps are also listed in the `--help` menu. 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", .{}); } } |
