Durable object websocket close not working
Hey, I recently got into Durable objects but I think i'm not getting some parts of it. In my code I was just trying to close the connection immediately after establishing it, but it is not working for some reason and
ws.closews.close does nothing while ws.send()ws.send()works great. This is my code:export class Manager extends DurableObject {
async fetch(request: Request) {
// Creates two ends of a WebSocket connection.
const webSocketPair = new WebSocketPair();
const [client, server] = Object.values(webSocketPair);
// Calling `acceptWebSocket()` informs the runtime that this WebSocket is to begin terminating
// request within the Durable Object. It has the effect of "accepting" the connection,
// and allowing the WebSocket to send and receive messages.
// Unlike `ws.accept()`, `state.acceptWebSocket(ws)` informs the Workers Runtime that the WebSocket
// is "hibernatable", so the runtime does not need to pin this Durable Object to memory while
// the connection is open. During periods of inactivity, the Durable Object can be evicted
// from memory, but the WebSocket connection will remain open. If at some later point the
// WebSocket receives a message, the runtime will recreate the Durable Object
// (run the `constructor`) and deliver the message to the appropriate handler.
this.ctx.acceptWebSocket(server);
this.ctx.waitUntil((async () => {
const websockets = this.ctx.getWebSockets();
for (const ws of websockets) {
if (ws === server) {
ws.send('Hello from the Durable Object!');
ws.close(1008, 'Connection closed');
}
}
})());
console.log('Handler finished');
return new Response(null, {
status: 101,
webSocket: client,
});
}
async processRequest(request: Request) {
}
}export class Manager extends DurableObject {
async fetch(request: Request) {
// Creates two ends of a WebSocket connection.
const webSocketPair = new WebSocketPair();
const [client, server] = Object.values(webSocketPair);
// Calling `acceptWebSocket()` informs the runtime that this WebSocket is to begin terminating
// request within the Durable Object. It has the effect of "accepting" the connection,
// and allowing the WebSocket to send and receive messages.
// Unlike `ws.accept()`, `state.acceptWebSocket(ws)` informs the Workers Runtime that the WebSocket
// is "hibernatable", so the runtime does not need to pin this Durable Object to memory while
// the connection is open. During periods of inactivity, the Durable Object can be evicted
// from memory, but the WebSocket connection will remain open. If at some later point the
// WebSocket receives a message, the runtime will recreate the Durable Object
// (run the `constructor`) and deliver the message to the appropriate handler.
this.ctx.acceptWebSocket(server);
this.ctx.waitUntil((async () => {
const websockets = this.ctx.getWebSockets();
for (const ws of websockets) {
if (ws === server) {
ws.send('Hello from the Durable Object!');
ws.close(1008, 'Connection closed');
}
}
})());
console.log('Handler finished');
return new Response(null, {
status: 101,
webSocket: client,
});
}
async processRequest(request: Request) {
}
}