Skip to content

Commit

Permalink
refactor: simplify websocket message sending (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
daychongyang authored May 18, 2020
1 parent d3e08d2 commit c40978f
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/node/server/serverPluginHmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,10 @@ export const hmrPlugin: ServerPlugin = ({

// start a websocket server to send hmr notifications to the client
const wss = new WebSocket.Server({ server })
const sockets = new Set<WebSocket>()

wss.on('connection', (socket) => {
debugHmr('ws client connected')
sockets.add(socket)
socket.send(JSON.stringify({ type: 'connected' }))
socket.on('close', () => {
sockets.delete(socket)
})
})

wss.on('error', (e: Error & { code: string }) => {
Expand All @@ -134,7 +129,12 @@ export const hmrPlugin: ServerPlugin = ({
const send = (payload: HMRPayload) => {
const stringified = JSON.stringify(payload, null, 2)
debugHmr(`update: ${stringified}`)
sockets.forEach((s) => s.send(stringified))

wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(stringified)
}
})
}

watcher.handleVueReload = handleVueReload
Expand Down

0 comments on commit c40978f

Please sign in to comment.