Using fetch with wss:// Upgrade: websocket fails

Following the worker + websocket documentation how to connect to a external websocket server but cannot get it to work. I just get Fetch API cannot load: wss://.... Below is the POC
export default {
async fetch() {
// URL of your WebSocket server
const url =
'wss://REDACTED';

// Custom headers
const headers = {
Upgrade: 'websocket',
Cookie:
'REDACTED',
};

let resp = await fetch(url, {
headers
});

let ws = resp.webSocket;
if (!ws) {
throw new Error("server didn't accept WebSocket");
}

// Call accept() to indicate that you'll be handling the socket here
// in JavaScript, as opposed to returning it on to a client.
ws.accept();

let text = ''

ws.addEventListener('message', msg => {
text = JSON.parse(msg.data);
});

await new Promise((resolve) => setTimeout(resolve, 5000));

return new Response(text);

},
};
export default {
async fetch() {
// URL of your WebSocket server
const url =
'wss://REDACTED';

// Custom headers
const headers = {
Upgrade: 'websocket',
Cookie:
'REDACTED',
};

let resp = await fetch(url, {
headers
});

let ws = resp.webSocket;
if (!ws) {
throw new Error("server didn't accept WebSocket");
}

// Call accept() to indicate that you'll be handling the socket here
// in JavaScript, as opposed to returning it on to a client.
ws.accept();

let text = ''

ws.addEventListener('message', msg => {
text = JSON.parse(msg.data);
});

await new Promise((resolve) => setTimeout(resolve, 5000));

return new Response(text);

},
};
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server