-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
29 lines (28 loc) · 892 Bytes
/
server.ts
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
const server = Bun.serve<{ username: string }>({
port: 3001,
fetch(req, server) {
const url = new URL(req.url);
const username = url.searchParams.get('name');
const success = server.upgrade(req, { data: { username } });
if (success) return;
return new Response('Upgrade failed', { status: 500 });
},
websocket: {
open(ws) {
console.log('Websocket opened');
ws.subscribe('chat');
server.publish('chat', `Welcome ${ws.data.username}!`);
},
message(ws, message) {
console.log(`Received ${message}`);
server.publish('chat', `${ws.data.username}: ${message}`);
},
close(ws) {
console.log('Websocket closed');
const msg = `${ws.data.username} has left the chat`;
server.publish('chat', msg);
ws.unsubscribe('chat');
},
},
});
console.log(`Listening on ${server.hostname}:${server.port}`);