mirror of
https://github.com/LukeHagar/crossws.git
synced 2025-12-09 12:27:45 +00:00
1.3 KiB
1.3 KiB
icon
| icon |
|---|
| simple-icons:bun |
Bun
Integrate crossws with Bun.
To integrate crossws with your Bun server, you need to handle upgrade with handleUpgrade util and also pass the websocket object returned from the adapter to server options. crossws leverages native bun WebSocket API.
import crossws from "crossws/adapters/bun";
const ws = crossws({
hooks: {
message: console.log,
},
});
Bun.serve({
port: 3000,
websocket: ws.websocket,
fetch(req, server) {
if (request.headers.get("upgrade") === "websocket") {
return ws.handleUpgrade(request, server);
}
return new Response(
`<script>new WebSocket("ws://localhost:3000").addEventListener('open', (e) => e.target.send("Hello from client!"));</script>`,
{ headers: { "content-type": "text/html" } },
);
},
});
Adapter Hooks
bun:message (peer, ws, message)bun:open (peer, ws)bun:close (peer, ws)bun:drain (peer)bun:error (peer, ws, error)bun:ping (peer, ws, data)bun:pong (peer, ws, data)
::read-more
See test/fixture/bun.ts for demo and src/adapters/bun.ts for implementation.
::