fix(bridge): fix cra not updating with HMR

This commit is contained in:
Christopher Astfalk
2020-12-16 01:12:12 +01:00
parent 7e740abd38
commit 69d09a3a17
+18 -3
View File
@@ -14,8 +14,8 @@ const webSocket = require('ws')
const openBridges = new WeakMap()
/**
* Creates a middleware that will add an WebSocket server on to the server,
* when first request is made.
* Creates a middleware that will add a WebSocket server on to the server,
* when the first request is made.
* This is only for development!
* Inspired by ws handling of http-proxy-middleware.
*
@@ -23,11 +23,26 @@ const openBridges = new WeakMap()
*/
function createWebSocketCreationRoute (url) {
let hasSubscribed = false
let app
const wss = new webSocket.Server({ noServer: true })
wss.on('connection', ws => {
openBridges.set(ws, new Bridge(ws, app))
})
return (req, res, next) => {
if (!hasSubscribed) {
hasSubscribed = true
createWebSocketServer(req.app, req.connection.server, url)
app = req.app
// manuel handle upgrade
req.connection.server.on('upgrade', (request, socket, head) => {
if (request.url === url) {
wss.handleUpgrade(request, socket, head, ws => {
wss.emit('connection', ws, request)
})
}
})
}
next()
}