a lightweight WebSocket library for Zig ⚡
net.Stream
By default, ws uses the Stream
interface of net
namespace.
You can use your choice of stream through ws.Client
interface.
test "Simple connection to :8080" {
const allocator = std.testing.allocator;
var cli = try connect(allocator, try std.Uri.parse("ws://localhost:8080"), &.{
.{"Host", "localhost"},
.{"Origin", "http://localhost/"},
});
defer cli.deinit(allocator);
while (true) {
const msg = try cli.receive();
switch (msg.type) {
.text => {
std.debug.print("received: {s}\n", .{msg.data});
try cli.send(.text, msg.data);
},
.ping => {
std.debug.print("got ping! sending pong...\n", .{});
try cli.pong();
},
.close => {
std.debug.print("close", .{});
break;
},
else => {
std.debug.print("got {s}: {s}\n", .{@tagName(msg.type), msg.data});
},
}
}
try cli.close();
}
std.crypto.tls.Client
)This library wouldn’t be possible without these cool projects & posts:
MIT License, check out.