Bun 的伺服器端 WebSocket
API 提供一個原生發布訂閱 API。Socket 可以使用 socket.subscribe(<name>)
訂閱一組命名頻道;訊息可以使用 socket.publish(<name>, <message>)
發布到頻道。
這個程式碼片段實作了一個簡單的單一頻道聊天伺服器。
const server = Bun.serve<{ username: string }>({
fetch(req, server) {
const cookies = req.headers.get("cookie");
const username = getUsernameFromCookies(cookies);
const success = server.upgrade(req, { data: { username } });
if (success) return undefined;
return new Response("Hello world");
},
websocket: {
open(ws) {
const msg = `${ws.data.username} has entered the chat`;
ws.subscribe("the-group-chat");
server.publish("the-group-chat", msg);
},
message(ws, message) {
// the server re-broadcasts incoming messages to everyone
server.publish("the-group-chat", `${ws.data.username}: ${message}`);
},
close(ws) {
const msg = `${ws.data.username} has left the chat`;
server.publish("the-group-chat", msg);
ws.unsubscribe("the-group-chat");
},
},
});
console.log(`Listening on ${server.hostname}:${server.port}`);