ThatBeanLady
ThatBeanLady
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
oh ok
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
thats all it gives me
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
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
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
still throws an error
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
removing it throws an error so i guess that clears up that the code for the routing is throwing an error
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
ill try removing it
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
thats if an error is caught i thought
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
where
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
i know its a mess
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
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();
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
and client side for testing
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
websocket opening error in worker
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}));
}
}
})
}
14 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
When I create a new WebSocket it sends a request with the get method
thank you
15 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
When I create a new WebSocket it sends a request with the get method
@PurpleBlob ?
15 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
When I create a new WebSocket it sends a request with the get method
the upgrade header is just websocket though
15 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
When I create a new WebSocket it sends a request with the get method
it says that it has an invalid upgrade header
15 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
When I create a new WebSocket it sends a request with the get method
yeah i tried implementing that but it didnt like it
15 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
When I create a new WebSocket it sends a request with the get method
im a bit lost
15 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
When I create a new WebSocket it sends a request with the get method
actually i think i might need some code
15 replies
CDCloudflare Developers
Created by ThatBeanLady on 1/14/2024 in #workers-help
When I create a new WebSocket it sends a request with the get method
not asking for code just a description if possible
15 replies