websocket opening error in worker
When I create a new websocket, the server returns a response with status code 101, which javascript doesnt like and says the status code has to be from 200-599, essentially meaning I can't create a websocket. Here is my server side code for reference:
3 Replies
class chatroom {
static sessions = [];
}
export default {
async fetch(request) {
return handleErrs(request, () => {
return requestHandler(request);
});
}
}
async function handleErrs(request, cbfunc) {
try {
return await cbfunc();
} catch (err) {
const upgradeHeader = request.headers.get('Upgrade');
if (upgradeHeader == 'websocket') {
let pair = new WebSocketPair();
pair[1].accept();
pair[1].send(JSON.stringify({error: err.stack}));
pair[1].close(1011, "Uncaught exception during session setup");
return new Response(null, { status: 101, webSocket: pair[0] });
}
return new Response('Server Error', { status: 499 });
}
}
async function requestHandler(request) {
let url = new URL(request.url);
let pathname = url.pathname;
pathname = pathname.slice(1).split("/");
if (pathname[pathname.length - 1] == "websocket") {
if (request.headers.get("Upgrade") == "websocket") {
let pair = new WebSocketPair();
pair[1].accept();
await initializeChatRoom(pair[1]);
return new Response(null, { status: 101, websocket: pair[0]});
}
return new Response("expected websocket", {status: 400})
} else {
return new Response("page not found", { status: 404})
}
}
async function initializeChatRoom(server) {
chatroom.sessions.push(server);
server.send(JSON.stringify({type: "message", data: "saidj"}))
server.addEventListener("message", async event => {
let data = JSON.parse(event.data);
for (let i = 0; i < chatroom.sessions.length; i += 1) {
let currSession = chatroom.sessions[i];
if (/*currSession.url.slice(1).split("/")[1] == data.room*/ true) {
currSession.send(JSON.stringify({type: "message", data: data.data}));
}
}
})
}
class chatroom {
static sessions = [];
}
export default {
async fetch(request) {
return handleErrs(request, () => {
return requestHandler(request);
});
}
}
async function handleErrs(request, cbfunc) {
try {
return await cbfunc();
} catch (err) {
const upgradeHeader = request.headers.get('Upgrade');
if (upgradeHeader == 'websocket') {
let pair = new WebSocketPair();
pair[1].accept();
pair[1].send(JSON.stringify({error: err.stack}));
pair[1].close(1011, "Uncaught exception during session setup");
return new Response(null, { status: 101, webSocket: pair[0] });
}
return new Response('Server Error', { status: 499 });
}
}
async function requestHandler(request) {
let url = new URL(request.url);
let pathname = url.pathname;
pathname = pathname.slice(1).split("/");
if (pathname[pathname.length - 1] == "websocket") {
if (request.headers.get("Upgrade") == "websocket") {
let pair = new WebSocketPair();
pair[1].accept();
await initializeChatRoom(pair[1]);
return new Response(null, { status: 101, websocket: pair[0]});
}
return new Response("expected websocket", {status: 400})
} else {
return new Response("page not found", { status: 404})
}
}
async function initializeChatRoom(server) {
chatroom.sessions.push(server);
server.send(JSON.stringify({type: "message", data: "saidj"}))
server.addEventListener("message", async event => {
let data = JSON.parse(event.data);
for (let i = 0; i < chatroom.sessions.length; i += 1) {
let currSession = chatroom.sessions[i];
if (/*currSession.url.slice(1).split("/")[1] == data.room*/ true) {
currSession.send(JSON.stringify({type: "message", data: data.data}));
}
}
})
}
const WebSocket = require('ws');
let username = "greg";
let roomCode = "greg";
import("./webserv.mjs");
const asyncFunction = async () => {
let url = new URL('not leaking this
");
try {
let websocket = new WebSocket(url);
if (!websocket) {
console.log(resp.status);
console.log(websocket);
throw new Error("server didn't accept WebSocket");
}
websocket.onopen = () => {
console.log('connection open');
asb(websocket);
}
setTimeout(() => console.log(websocket.readyState), 3000);
} catch (err) {
console.log(err);
}
/*websocket.addEventListener("message", (event) => {
console.log(event);
const data = JSON.parse(event.data);
if (data.type = "message") {
console.log(data.data);
}
})
websocket.send(JSON.stringify({ type: "message", room: roomCode, user: username, data: "message" }))
*/
}
const asb = (ws) => {
ws.addEventListener("message", (event) => {
console.log("message received:");
const data = JSON.parse(event.data);
console.log(data);
if (data.type == "message") {
console.log(data);
}
})
ws.send(JSON.stringify({ type: "message", room: roomCode, user: username, data: "message" }))
}
asyncFunction();
const WebSocket = require('ws');
let username = "greg";
let roomCode = "greg";
import("./webserv.mjs");
const asyncFunction = async () => {
let url = new URL('not leaking this
");
try {
let websocket = new WebSocket(url);
if (!websocket) {
console.log(resp.status);
console.log(websocket);
throw new Error("server didn't accept WebSocket");
}
websocket.onopen = () => {
console.log('connection open');
asb(websocket);
}
setTimeout(() => console.log(websocket.readyState), 3000);
} catch (err) {
console.log(err);
}
/*websocket.addEventListener("message", (event) => {
console.log(event);
const data = JSON.parse(event.data);
if (data.type = "message") {
console.log(data.data);
}
})
websocket.send(JSON.stringify({ type: "message", room: roomCode, user: username, data: "message" }))
*/
}
const asb = (ws) => {
ws.addEventListener("message", (event) => {
console.log("message received:");
const data = JSON.parse(event.data);
console.log(data);
if (data.type == "message") {
console.log(data);
}
})
ws.send(JSON.stringify({ type: "message", room: roomCode, user: username, data: "message" }))
}
asyncFunction();
it would be nice to see the actual error in a console, so I don't have to copy your code into a worker - FYI the class with the static property won't work as it's unlikely that any two users will get routed to the same isolate, so I recommend looking at Durable Objects for such coordination
node:events:492
throw er; // Unhandled 'error' event
^
Error: Unexpected server response: 500
node:events:492
throw er; // Unhandled 'error' event
^
Error: Unexpected server response: 500