tga
tga
Explore posts from servers
CDCloudflare Developers
Created by tga on 5/4/2024 in #workers-help
How to broadcast message to all active websocket connections?
oh I see, that makes sense, thank you!
8 replies
CDCloudflare Developers
Created by tga on 5/4/2024 in #workers-help
How to broadcast message to all active websocket connections?
✘ [ERROR] A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.


[wrangler:err] SyntaxError: Unexpected end of JSON input
✘ [ERROR] A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.


[wrangler:err] SyntaxError: Unexpected end of JSON input
8 replies
CDCloudflare Developers
Created by tga on 5/4/2024 in #workers-help
How to broadcast message to all active websocket connections?
I tried to make a minimal repro, it still fails but with a different error msg
const connections: WebSocket[] = []

export default {
fetch: (req) => {
const url = new URL(req.url)
if (url.pathname === "/ws") {
const upgradeHeader = req.headers.get("Upgrade")
if (!upgradeHeader || upgradeHeader !== "websocket") {
return new Response("Expected Upgrade: websocket", { status: 426 })
}
const webSocketPair = new WebSocketPair()
const [client, server] = Object.values(webSocketPair)
server.accept()
connections.push(server)
connections.forEach((ws) => ws.send("new connection!"))
// logic to remove on "close" event
return new Response(null, {
status: 101,
webSocket: client,
})
}
return new Response(`
<script>
const ws = new WebSocket("ws://localhost:8787/ws")
ws.onmessage = (e) => {
console.log("data", e.data)
}
ws.onopen = () => {
console.log("open")
ws.send("hi")
}
</script>
`, {
headers: {
"Content-Type": "text/html; charset=utf-8",
}
})
},
} satisfies ExportedHandler<{}>
const connections: WebSocket[] = []

export default {
fetch: (req) => {
const url = new URL(req.url)
if (url.pathname === "/ws") {
const upgradeHeader = req.headers.get("Upgrade")
if (!upgradeHeader || upgradeHeader !== "websocket") {
return new Response("Expected Upgrade: websocket", { status: 426 })
}
const webSocketPair = new WebSocketPair()
const [client, server] = Object.values(webSocketPair)
server.accept()
connections.push(server)
connections.forEach((ws) => ws.send("new connection!"))
// logic to remove on "close" event
return new Response(null, {
status: 101,
webSocket: client,
})
}
return new Response(`
<script>
const ws = new WebSocket("ws://localhost:8787/ws")
ws.onmessage = (e) => {
console.log("data", e.data)
}
ws.onopen = () => {
console.log("open")
ws.send("hi")
}
</script>
`, {
headers: {
"Content-Type": "text/html; charset=utf-8",
}
})
},
} satisfies ExportedHandler<{}>
8 replies