treesummaryrefslogcommitdiff
path: root/src/main.zig
blob: f06c7ff2b8b4ddd55489cecce4075a91d0c5fdea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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(
            \\ <script>
            \\ const socket = new WebSocket("ws://localhost:1234");
            \\ socket.addEventListener("open", (event) => {
            \\   socket.send("Hello Server!");
            \\ });
            \\ socket.addEventListener("message", (event) => {
            \\   console.log("Message from server ", event.data);
            \\ });
            \\ </script>
            \\ <p>hallo</p>
        , .{ .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", .{});
    }
}