karlseguin / websocket.zig

A websocket implementation for zig
MIT License
283 stars 25 forks source link

Cannot mutate Handler since passed different pointers #20

Closed tjk closed 10 months ago

tjk commented 10 months ago

Please let me know if I'm doing something wrong but it seems like handler is copied in server.zig and so the mutable self pointer is not the same across different hooks... is this expected?

const Handler = struct {
    conn: *websocket.Conn,
    context: *Context,
    handles: u32,

    pub fn init(_: websocket.Handshake, conn: *websocket.Conn, context: *Context) !Handler {
        return Handler{
            .conn = conn,
            .context = context,
            .handles = 0,
        };
    }

    pub fn handle(self: *Handler, message: websocket.Message) !void {
        self.handles += 1;
    }

    pub fn close(self: *Handler) void {
        std.debug.print("handles: {d}\n", .{self.handles}); // not what i expect
    }
};

I know to use context for state across handlers but I thought I could put handler-local state within that struct... maybe bad assumption? I'm new to zig so I may be doing something silly. Thanks for the library!

karlseguin commented 10 months ago

Ya...afterInit and close would get a different copy than handle. Latest master should make this consistent across all.