Commit ce918191 authored by Dave Ceddia's avatar Dave Ceddia Committed by Dan Abramov
Browse files

Enable proxying of websockets (#1090)

Added `ws: true` to the httpProxyMiddleware options, and also listen
for the "upgrade" event so that websockets can be proxied immediately,
rather than waiting for an initial HTTP request.
parent ab57f772
No related merge requests found
Showing with 18 additions and 11 deletions
+18 -11
...@@ -181,18 +181,25 @@ function addMiddleware(devServer) { ...@@ -181,18 +181,25 @@ function addMiddleware(devServer) {
// - /sockjs-node/* (WebpackDevServer uses this for hot reloading) // - /sockjs-node/* (WebpackDevServer uses this for hot reloading)
// Tip: use https://jex.im/regulex/ to visualize the regex // Tip: use https://jex.im/regulex/ to visualize the regex
var mayProxy = /^(?!\/(index\.html$|.*\.hot-update\.json$|sockjs-node\/)).*$/; var mayProxy = /^(?!\/(index\.html$|.*\.hot-update\.json$|sockjs-node\/)).*$/;
devServer.use(mayProxy,
// Pass the scope regex both to Express and to the middleware for proxying // Pass the scope regex both to Express and to the middleware for proxying
// of both HTTP and WebSockets to work without false positives. // of both HTTP and WebSockets to work without false positives.
httpProxyMiddleware(pathname => mayProxy.test(pathname), { var hpm = httpProxyMiddleware(pathname => mayProxy.test(pathname), {
target: proxy, target: proxy,
logLevel: 'silent', logLevel: 'silent',
onError: onProxyError(proxy), onError: onProxyError(proxy),
secure: false, secure: false,
changeOrigin: true changeOrigin: true,
}) ws: true
); });
devServer.use(mayProxy, hpm);
// Listen for the websocket 'upgrade' event and upgrade the connection.
// If this is not done, httpProxyMiddleware will not try to upgrade until
// an initial plain HTTP request is made.
devServer.listeningApp.on('upgrade', hpm.upgrade);
} }
// Finally, by now we have certainly resolved the URL. // Finally, by now we have certainly resolved the URL.
// It may be /index.html, so let the dev server try serving it again. // It may be /index.html, so let the dev server try serving it again.
devServer.use(devServer.middleware); devServer.use(devServer.middleware);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment