Repository navigation
Expand file tree
/
Copy pathsseServer.zig
More file actions
58 lines (48 loc) · 1.95 KB
/
Copy pathsseServer.zig
File metadata and controls
58 lines (48 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const std = @import("std");
const httpx = @import("httpx");
pub fn main() !void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const io = std.Io.Threaded.global_single_threaded.io();
var server = try httpx.Server.init(allocator, io, .{
.host = "127.0.0.1",
.port = 0,
.maxConnections = 5,
});
defer server.deinit();
try server.get("/events", sseHandler);
const port = server.localPort();
std.debug.print("SSE server running on http://127.0.0.1:{d}\n", .{port});
const ServerThread = struct {
fn run(s: *httpx.Server) void {
s.run();
}
};
const t = try std.Thread.spawn(.{}, ServerThread.run, .{&server});
var client = httpx.Client.init(allocator, io, .{});
defer client.deinit();
var urlBuf: [128]u8 = undefined;
const urlEvents = try std.fmt.bufPrint(&urlBuf, "http://127.0.0.1:{d}/events", .{port});
var res = try client.get(urlEvents, .{});
std.debug.print("GET /events -> status={d}, body={s}\n", .{ res.status, res.body });
res.deinit();
server.requestShutdown();
t.join();
std.debug.print("SSE server verification completed successfully.\n", .{});
}
fn sseHandler(ctx: *httpx.router.Context) anyerror!httpx.router.Response {
var out = std.ArrayList(u8).empty;
var writer = httpx.sse.Event.Writer.init(ctx.allocator);
try writer.writeEvent(&out, &[_][]const u8{"Connected to SSE live stream"}, "status", 1, 5000);
try writer.writeEvent(&out, &[_][]const u8{"First data packet"}, "message", 2, null);
return .{
.status = 200,
.body = out.items,
.contentType = "text/event-stream; charset=utf-8",
.headers = try ctx.allocator.dupe(httpx.router.Header, &[_]httpx.router.Header{
.{ .name = "Cache-Control", .value = "no-cache" },
.{ .name = "Connection", .value = "keep-alive" },
}),
};
}