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.close does nothing while 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) {

}

}
No description
3 Replies
Zetax
ZetaxOP5mo ago
Also, for some reason my waitUntil code runs before the handler, which it isn't supposed to do, right?
kev-ac
kev-ac5mo ago
I'm unsure if waitUntil() is really neccessary here. All methods are called synchronously, there is nothing to await for. Why are you checking if ws === server? getWebSockets() should return all accepted websockets, so all client connections. Thats where you want to send your messages to.
Zetax
ZetaxOP5mo ago
I am using waitUntil to actually test if I can actually close the websocket connecton, because if I do ws.close before returning the response, then it will just give me an http error because it did not return the upgrade response. And I am checking if ws === server to only send the message to the client connecting (At least that's what I think "server" represents, because when I do client.send, it doesnt do anything)
Want results from more Discord servers?
Add your server