mirror of
https://github.com/sr229/kuru-kuru.git
synced 2024-09-20 04:10:33 +00:00
Make server not send anything if socket state is anything but open
This commit is contained in:
parent
4574557fdc
commit
d00cf3a9c3
1 changed files with 7 additions and 2 deletions
|
@ -20,7 +20,7 @@ export const handler: Handlers = {
|
|||
GET: async (req, ctx) => {
|
||||
let bc = new BroadcastChannel("global-count");
|
||||
|
||||
// check if we're requesting wss:// or ws://, add the response header accordingly
|
||||
// check if we're requesting wss:// or ws://, then upgrade as necessary
|
||||
if (req.headers.get("upgrade") === "websocket") {
|
||||
const { socket, response } = Deno.upgradeWebSocket(req);
|
||||
|
||||
|
@ -34,7 +34,12 @@ export const handler: Handlers = {
|
|||
};
|
||||
|
||||
bc.addEventListener("message", (e) => {
|
||||
socket.send(JSON.stringify({ globalCount: e.data }));
|
||||
try {
|
||||
// don't send if the socket is closed
|
||||
if (socket.readyState === 1) socket.send(JSON.stringify({ globalCount: e.data }));
|
||||
} catch (e) {
|
||||
console.warn(`[${new Date().toISOString()}] ${e.stack}`);
|
||||
}
|
||||
});
|
||||
|
||||
socket.onclose = () => {
|
||||
|
|
Loading…
Reference in a new issue