Skip to content
/ zig-ws Public
forked from nikneym/ws

WebSocket library for Zig ⚡

License

Notifications You must be signed in to change notification settings

iacore/zig-ws

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ws

ws

a lightweight WebSocket library for Zig ⚡

Features

  • Only allocates for WebSocket handshake, message parsing and building does not allocate
  • Ease of use, can be used directly with net.Stream
  • Does buffered reads and writes (can be used with any other reader/writer too)
  • Supports streaming output thanks to WebSocket fragmentation

Example

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();
}

Planned

  • WebSocket server support
  • TLS support out of the box (tracks std.crypto.tls.Client)
  • Request & response headers
  • WebSocket Compression support

Acknowledgements

This library wouldn't be possible without these cool projects & posts:

License

MIT License, check out.

About

WebSocket library for Zig ⚡

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Zig 100.0%