dont crash when unknown websocket requests come in

This commit is contained in:
Ryan Haskell-Glatz 2021-05-08 12:29:00 -05:00
parent f97dc4ebd6
commit 2dad9f9662

View File

@ -52,9 +52,11 @@ const start = async () => new Promise((resolve, reject) => {
const ws = new websocket.server({ httpServer: server })
const script = ` new WebSocket('ws://' + window.location.host, 'elm-spa').onmessage = function () { window.location.reload() } `
ws.on('request', (req) => {
const conn = req.accept('elm-spa', req.origin)
connections[req.remoteAddress] = conn
conn.on('close', () => delete connections[conn.remoteAddress])
try {
const conn = req.accept('elm-spa', req.origin)
connections[req.remoteAddress] = conn
conn.on('close', () => delete connections[conn.remoteAddress])
} catch (_) { /* Safely ignores unknown requests */ }
})
// Send reload if any files change