msg: fix len for msg.Buffer and encode msg size as binary header

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-11-28 00:38:33 +01:00
parent d95462073a
commit b800d0eeb8
4 changed files with 49 additions and 54 deletions

View File

@@ -19,6 +19,7 @@
const std = @import("std");
const ws = @import("websocket");
const Msg = @import("msg.zig").Msg;
const log = std.log.scoped(.handler);
@@ -45,7 +46,7 @@ pub const Stream = struct {
}
fn closeCDP(self: *const Stream) void {
const close_msg: []const u8 = "5:close";
const close_msg: []const u8 = .{ 5, 0 } ++ "close";
self.recv(close_msg) catch |err| {
log.err("stream close error: {any}", .{err});
};
@@ -82,8 +83,10 @@ pub const Handler = struct {
self.stream.closeCDP();
}
pub fn clientMessage(self: *Handler, alloc: std.mem.Allocator, data: []const u8) !void {
const msg = try std.fmt.allocPrint(alloc, "{d}:{s}", .{ data.len, data });
try self.stream.recv(msg);
pub fn clientMessage(self: *Handler, data: []const u8) !void {
var header: [2]u8 = undefined;
Msg.setSize(data.len, &header);
try self.stream.recv(&header);
try self.stream.recv(data);
}
};