mirror of
https://github.com/morris/vanilla-todo.git
synced 2025-01-17 20:58:22 +01:00
warn when dev server disconnects
This commit is contained in:
parent
89f6a0f390
commit
f3c753b645
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
const socket = new WebSocket(
|
const socket = new WebSocket(
|
||||||
`${(location.protocol === 'http:' ? 'ws://' : 'wss://') + location.host}/`,
|
`${(location.protocol === 'http:' ? 'ws://' : 'wss://') + location.host}/`,
|
||||||
);
|
);
|
||||||
@ -28,3 +29,7 @@ socket.addEventListener('message', (message) => {
|
|||||||
// otherwise, reload page
|
// otherwise, reload page
|
||||||
if (reload) location.reload();
|
if (reload) location.reload();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.addEventListener('close', () => {
|
||||||
|
console.warn('Development server disconnected');
|
||||||
|
});
|
||||||
|
@ -125,20 +125,20 @@ server.listen(port);
|
|||||||
|
|
||||||
// WebSocket server
|
// WebSocket server
|
||||||
|
|
||||||
const wsClients = new Set();
|
const webSocketServer = new WebSocketServer({ server });
|
||||||
const wsServer = new WebSocketServer({ server });
|
const webSockets = new Set();
|
||||||
|
|
||||||
wsServer.on('connection', (client) => {
|
webSocketServer.on('connection', (client) => {
|
||||||
wsClients.add(client);
|
webSockets.add(client);
|
||||||
});
|
});
|
||||||
|
|
||||||
function broadcast(message) {
|
function broadcast(message) {
|
||||||
for (const wsClient of wsClients) {
|
for (const webSocket of webSockets) {
|
||||||
if (wsClient.readyState === WebSocket.OPEN) {
|
if (webSocket.readyState === WebSocket.OPEN) {
|
||||||
wsClient.send(JSON.stringify(message));
|
webSocket.send(JSON.stringify(message));
|
||||||
} else {
|
} else {
|
||||||
wsClients.delete(wsClient);
|
webSockets.delete(webSocket);
|
||||||
wsClient.terminate();
|
webSocket.terminate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user