Skip to content

Commit

Permalink
more sse stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mookums committed Oct 27, 2024
1 parent 09f9c6f commit 1a8efc5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
14 changes: 11 additions & 3 deletions examples/http/sse/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ const zzz = @import("zzz");
const http = zzz.HTTP;
const log = std.log.scoped(.@"examples/sse");

const tardy = zzz.tardy;
const Runtime = tardy.Runtime;
const Task = tardy.Task;

const Server = http.Server(.plain, .auto);
const Router = Server.Router;
const Context = Server.Context;
const Route = Server.Route;
const SSE = Server.SSE;

fn sse_task(sse: *SSE) !void {
log.debug("ctx ptr: {*}", .{sse.context});
fn sse_process(rt: *Runtime, t: *const Task, sse: *SSE) !void {
_ = rt;
_ = t;
try sse.send(.{ .data = "hi this is a message" }, sse_task);
std.time.sleep(std.time.ns_per_s);
}

fn sse_task(sse: *SSE) !void {
try sse.context.runtime.spawn_delay(*SSE, sse_process, sse, .{ .seconds = 1 });
}

fn sse_handler(ctx: *Context) void {
Expand Down
7 changes: 6 additions & 1 deletion src/http/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const TaskFn = @import("tardy").TaskFn;
const raw_respond = @import("server.zig").raw_respond;

// Context is dependent on the server that gets created.
// This is because the trigger_task ends up being dependent.
pub fn Context(comptime Server: type) type {
return struct {
const Self = @This();
Expand All @@ -36,8 +35,12 @@ pub fn Context(comptime Server: type) type {
captures: []Capture,
queries: *QueryMap,
provision: *Provision,
triggered: bool = false,

pub fn to_sse(self: *Self, then: SSE.SSEHandlerFn) void {
assert(!self.triggered);
self.triggered = true;

self.response.set(.{
.status = .OK,
.body = "",
Expand Down Expand Up @@ -78,6 +81,8 @@ pub fn Context(comptime Server: type) type {
}

pub fn respond(self: *Self, options: ResponseSetOptions) void {
assert(!self.triggered);
self.triggered = true;
self.response.set(options);

const body = options.body orelse "";
Expand Down
4 changes: 2 additions & 2 deletions src/http/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ pub fn Server(
send_then_recv_task,
provision,
provision.socket,
provision.buffer,
encrypted_buffer,
);
},
.plain => {
Expand All @@ -767,7 +767,7 @@ pub fn Server(
send_then_recv_task,
provision,
provision.socket,
provision.buffer,
plain_buffer,
);
},
}
Expand Down
2 changes: 2 additions & 0 deletions src/http/sse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const Pseudoslice = @import("../core/pseudoslice.zig").Pseudoslice;
const Provision = @import("provision.zig").Provision;
const _Context = @import("context.zig").Context;

const Runtime = @import("tardy").Runtime;

const SSEMessage = struct {
id: ?[]const u8 = null,
event: ?[]const u8 = null,
Expand Down
1 change: 1 addition & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
pub const tardy = @import("tardy");

/// HyperText Transfer Protocol.
/// Supports: HTTP/1.1
Expand Down

0 comments on commit 1a8efc5

Please sign in to comment.