stukennedy
05/25/2023, 8:41 AMwebsocket.ts
endpoint and it almost seems to be setting up
typescript
async function handleSession(request: Request, websocket: WebSocket) {
websocket.accept();
websocket.addEventListener('message', async ({ data }) => {
websocket.send('<div>Some HTML</div>');
});
websocket.addEventListener('close', async (evt) => {
// Handle when a client closes the WebSocket connection
console.log(evt);
});
}
export const onRequest: PagesFunction = async ({ request }) => {
console.log('websocket');
const upgradeHeader = request.headers.get('Upgrade');
if (!upgradeHeader || upgradeHeader !== 'websocket') {
return new Response('Expected Upgrade: websocket', { status: 400 });
}
const [client, server] = Object.values(new WebSocketPair());
await handleSession(request, server);
return new Response(null, {
status: 101,
webSocket: client,
});
};
I get
websocket
A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.
[mf:inf] GET /websocket 500 Internal Server Error (4ms)