Something went wrong. Try again.
A websocket implementation for zig forked from karlseguin.tngl.sh/websocket.zig
Something went wrong. Try again.
websocket.zig readme.md
# A zig websocket server.
The master branch targets the latest stable of Zig (0.15.1). The dev branch targets the latest version of Zig. If you're looking for an older version, look for an zig-X.YZ branches.
## Zig Version
This is for Zig 0.16.0. Use the [zig-0.15.2](https://github.com/karlseguin/websocket.zig/tree/zig-0.15) branch for Zig 0.15 or the [dev](https://github.com/karlseguin/websocket.zig/tree/dev) which may or may not be up to date with zig dev.
This ZIG 0.16 version is not well tested. Like Zig 0.16 itself, consider this experimental!
Skip to the [client section](#client).
If you're upgrading from a previous version, check out the [Server Migration](https://github.com/karlseguin/websocket.zig/wiki/Server-Migration) and [Client Migration](https://github.com/karlseguin/websocket.zig/wiki/Client-Migration) wikis.
# Server
```zig
const std = @import("std");
const ws = @import("websocket");
pub fn main(init: std.process.Init) !void {
const allocator = init.gpa;
var server = try ws.Server(Handler).init(init.io, allocator, .{
.port = 9224,
.address = "127.0.0.1",
.handshake = .{
.timeout = 3,
.max_size = 1024,
// since we aren't using hanshake.headers
// we can set this to 0 to save a few bytes.
.max_headers = 0,
},
});
// Arbitrary (application-specific) data to pass into each handler
// Pass void ({}) into listen if you have none
var app = App{};
// this blocks
try server.listen(&app);
}
// This is your application-specific wrapper around a websocket connection
const Handler = struct {
app: *App,
conn: *ws.Conn,
// You must define a public init function which takes
pub fn init(h: *ws.Handshake, conn: *ws.Conn, app: *App) !Handler {
// `h` contains the initial websocket "handshake" request
// It can be used to apply application-specific logic to verify / allow
// the connection (e.g. valid url, query string parameters, or headers)
…