WebSocket
A Swarmd Worker can establish a WebSocket connection to a server with the
WebSocket
constructor.
Client
You can use WebSocket as client to connect to other Websocket Servers.
Server
You can also create a WebSocket server by using the Swarmd.upgradeWebSocket
.
type OnRequestType = {
req: FetchEvent;
}
async function onRequest({ req }: OnRequestType) {
const { socket, response } = Swarmd.upgradeWebSocket(req.request);
socket.onopen = () => {
socket.send("Hello mom!");
};
socket.onclose = () => console.log("WebSocket has been closed.");
socket.onerror = (e) => console.error("WebSocket error:", e);
req.respondWith(response)
}
export { onRequest }