ceifa
01/08/2024, 11:51 AM0x0dd154
01/09/2024, 12:16 AMstate.acceptWebSocket
function and your durable object interface should implement the handlers - here is an incomplete example:
javascript
export function myDurableObject(state, env) {
const fetch = (request) => {
// stuff that handles websocket upgrade
// calling state.acceptWebSocket()
const [client, server] = new WebSocketPair();
state.acceptWebSocket(server);
return new Response(null, { status: 101, webSocket: client })
}
const webSocketMessage = (ws, message) => {
// handle message
}
const webSocketClose = (ws, code, reason, wasClean) => {
// handle close
}
const webSocketError = (ws, error) => {
// handle error
}
return { fetch, webSocketMessage, webSocketClose, webSocketError };
}
0x0dd154
01/09/2024, 12:18 AMworkers-chat-demo
repo https://github.com/cloudflare/workers-chat-demo/blob/hibernation/src/chat.mjs