forked from zigzap/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsockets.zig
More file actions
237 lines (210 loc) · 6.8 KB
/
Copy pathwebsockets.zig
File metadata and controls
237 lines (210 loc) · 6.8 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build websockets`.
//! Run me with `zig build run-websockets`.
//!
const std = @import("std");
const zap = @import("zap");
const WebSockets = zap.WebSockets;
const Context = struct {
userName: []const u8,
channel: []const u8,
// we need to hold on to them and just re-use them for every incoming
// connection
subscribeArgs: WebsocketHandler.SubscribeArgs,
settings: WebsocketHandler.WebSocketSettings,
};
const ContextList = std.ArrayList(*Context);
const ContextManager = struct {
io: std.Io,
allocator: std.mem.Allocator,
channel: []const u8,
usernamePrefix: []const u8,
lock: std.Io.Mutex = .init,
contexts: ContextList = undefined,
pub fn init(
io: std.Io,
allocator: std.mem.Allocator,
channelName: []const u8,
usernamePrefix: []const u8,
) ContextManager {
return .{
.io = io,
.allocator = allocator,
.channel = channelName,
.usernamePrefix = usernamePrefix,
.contexts = ContextList.empty,
};
}
pub fn deinit(self: *ContextManager) void {
for (self.contexts.items) |ctx| {
self.allocator.free(ctx.userName);
}
self.contexts.deinit(self.allocator);
}
pub fn newContext(self: *ContextManager) !*Context {
self.lock.lockUncancelable(self.io);
defer self.lock.unlock(self.io);
const ctx = try self.allocator.create(Context);
const userName = try std.fmt.allocPrint(
self.allocator,
"{s}{d}",
.{ self.usernamePrefix, self.contexts.items.len },
);
ctx.* = .{
.userName = userName,
.channel = self.channel,
// used in subscribe()
.subscribeArgs = .{
.channel = self.channel,
.force_text = true,
.context = ctx,
},
// used in upgrade()
.settings = .{
.on_open = on_open_websocket,
.on_close = on_close_websocket,
.on_message = handle_websocket_message,
.context = ctx,
},
};
try self.contexts.append(self.allocator, ctx);
return ctx;
}
};
//
// Websocket Callbacks
//
fn on_open_websocket(context: ?*Context, handle: WebSockets.WsHandle) !void {
if (context) |ctx| {
_ = try WebsocketHandler.subscribe(handle, &ctx.subscribeArgs);
// say hello
var buf: [128]u8 = undefined;
const message = try std.fmt.bufPrint(
&buf,
"{s} joined the chat.",
.{ctx.userName},
);
// send notification to all others
WebsocketHandler.publish(.{ .channel = ctx.channel, .message = message });
std.log.info("new websocket opened: {s}", .{message});
}
}
fn on_close_websocket(context: ?*Context, uuid: isize) !void {
_ = uuid;
if (context) |ctx| {
// say goodbye
var buf: [128]u8 = undefined;
const message = try std.fmt.bufPrint(
&buf,
"{s} left the chat.",
.{ctx.userName},
);
// send notification to all others
WebsocketHandler.publish(.{ .channel = ctx.channel, .message = message });
std.log.info("websocket closed: {s}", .{message});
}
}
fn handle_websocket_message(
context: ?*Context,
handle: WebSockets.WsHandle,
message: []const u8,
is_text: bool,
) !void {
_ = handle;
_ = is_text;
if (context) |ctx| {
// send message
const buflen = 128; // arbitrary len
var buf: [buflen]u8 = undefined;
const format_string = "{s}: {s}";
const fmt_string_extra_len = 2; // ": " between the two strings
//
const max_msg_len = buflen - ctx.userName.len - fmt_string_extra_len;
if (max_msg_len > 0) {
// there is space for the message, because the user name + format
// string extra do not exceed the buffer now, let's check: do we
// need to trim the message?
var trimmed_message: []const u8 = message;
if (message.len > max_msg_len) {
trimmed_message = message[0..max_msg_len];
}
const chat_message = try std.fmt.bufPrint(
&buf,
format_string,
.{ ctx.userName, trimmed_message },
);
// send notification to all others
WebsocketHandler.publish(
.{ .channel = ctx.channel, .message = chat_message },
);
std.log.info("{s}", .{chat_message});
} else {
std.log.warn(
"Username is very long, cannot deal with that size: {d}",
.{ctx.userName.len},
);
}
}
}
//
// HTTP stuff
//
fn on_request(r: zap.Request) !void {
try r.setHeader("Server", "zap.example");
try r.sendBody(
\\ <html><body>
\\ <h1>This is a simple Websocket chatroom example</h1>
\\ </body></html>
);
}
fn on_upgrade(r: zap.Request, target_protocol: []const u8) !void {
// make sure we're talking the right protocol
if (!std.mem.eql(u8, target_protocol, "websocket")) {
std.log.warn("received illegal protocol: {s}", .{target_protocol});
r.setStatus(.bad_request);
r.sendBody("400 - BAD REQUEST") catch unreachable;
return;
}
var context = GlobalContextManager.newContext() catch |err| {
std.log.err("Error creating context: {any}", .{err});
return;
};
try WebsocketHandler.upgrade(r.h, &context.settings);
std.log.info("connection upgrade OK", .{});
}
// global variables, yeah!
var GlobalContextManager: ContextManager = undefined;
const WebsocketHandler = WebSockets.Handler(Context);
// here we go
pub fn main(init: std.process.Init) !void {
const io = init.io;
var gpa = std.heap.DebugAllocator(.{
.thread_safe = true,
}){};
const allocator = gpa.allocator();
GlobalContextManager = ContextManager.init(io, allocator, "chatroom", "user-");
defer GlobalContextManager.deinit();
// setup listener
var listener = zap.HttpListener.init(
.{
.port = 3010,
.on_request = on_request,
.on_upgrade = on_upgrade,
.max_clients = 1000,
.max_body_size = 1 * 1024,
.public_folder = "examples/websockets/frontend",
.log = true,
},
);
try listener.listen();
std.log.info("", .{});
std.log.info("Connect with browser to http://localhost:3010.", .{});
std.log.info("Connect to websocket on ws://localhost:3010.", .{});
std.log.info("Terminate with CTRL+C", .{});
zap.start(.{
.threads = 1,
.workers = 1,
});
}