Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
zig-cache/
zig-out/
zig-out/
ws-client/
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

21 changes: 8 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,26 @@ Example
By default, ws uses the `Stream` interface of `net` namespace.
You can use your choice of stream through `ws.Client` interface.
```zig
const std = @import("std");
const ws = @import("ws");
test "Simple connection to :8080" {
const allocator = std.testing.allocator;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

var client = try ws.connect(allocator, "ws://localhost:8080", &.{
var cli = try connect(allocator, try std.Uri.parse("ws://localhost:8080"), &.{
.{"Host", "localhost"},
.{"Origin", "http://localhost/"},
});
defer client.deinit(allocator);
defer cli.deinit(allocator);

while (true) {
var msg = try client.receive();
const msg = try cli.receive();
switch (msg.type) {
.text => {
std.debug.print("received: {s}\n", .{msg.data});
try client.send(.text, msg.data);
try cli.send(.text, msg.data);
},

.ping => {
std.debug.print("got ping! sending pong...\n", .{});
try client.pong();
try cli.pong();
},

.close => {
Expand All @@ -56,7 +51,7 @@ pub fn main() !void {
}
}

try client.close();
try cli.close();
}
```

Expand Down
41 changes: 12 additions & 29 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
const std = @import("std");
const deps = @import("deps.zig");

pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
b.addModule(.{
.name = "ws",
.source_file = .{ .path = "src/main.zig" },
});

const exe = b.addExecutable("ws", "src/main.zig");
deps.addAllTo(exe);
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();

const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}

const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

const exe_tests = b.addTest("src/main.zig");
deps.addAllTo(exe_tests);
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
const test_compile = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
test_step.dependOn(&test_compile.step);
}
9 changes: 0 additions & 9 deletions deps.zig

This file was deleted.

1 change: 0 additions & 1 deletion lib/zuri
Submodule zuri deleted from d5cce7
31 changes: 31 additions & 0 deletions src/incoming_connection.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const std = @import("std");
const net = std.net;
const mem = std.mem;
const io = @import("io.zig");

// FIXME: add an option to change request buffer size?
// FIXME: use StringHashMapUnmanaged([]const u8) instead
pub const IncomingConnection = struct {
underlying_stream: net.Stream,
address: net.Address,
//ws_client: WsClient,
// this will hold url and request headers
req_buffer: [4096]u8 = undefined,
headers: std.StringHashMap([]const u8),
//buffered_reader: BufferedReader,
sender: Sender,
receiver: Receiver,

//const WsClient = Client(Reader, Writer, READ_BUFFER_SIZE, WRITE_BUFFER_SIZE);
//const BufferedReader = std.io.BufferedReader(4096, net.Stream.Reader);
//const Reader = BufferedReader.Reader;

const Sender = io.SenderImpl(net.Stream.Writer, .server);
const Receiver = io.ReceiverImpl(net.Stream.Reader, .server);

pub fn deinit(self: *IncomingConnection, allocator: mem.Allocator) void {
self.receiver.deinit(allocator);
self.underlying_stream.close();
self.headers.deinit();
}
};
Loading