Skip to content

Commit

Permalink
Handle external upgrade for all websocket proxies (#843)
Browse files Browse the repository at this point in the history
* Upgrade websocket proxies without initial http request
* Add websocket upgrade proxy test.
  • Loading branch information
jamesblight authored and SpaceK33z committed Mar 14, 2017
1 parent 35a44d1 commit d69559a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/Server.js
Expand Up @@ -127,6 +127,9 @@ function Server(compiler, options) {
contentBase = process.cwd();
}

// Keep track of websocket proxies for external websocket upgrade.
const websocketProxies = [];

const features = {
compress() {
if(options.compress) {
Expand Down Expand Up @@ -205,6 +208,9 @@ function Server(compiler, options) {
}

proxyMiddleware = getProxyMiddleware(proxyConfig);
if(proxyConfig.ws) {
websocketProxies.push(proxyMiddleware);
}

app.use((req, res, next) => {
if(typeof proxyConfigOrCallback === "function") {
Expand Down Expand Up @@ -361,6 +367,12 @@ function Server(compiler, options) {
} else {
this.listeningApp = http.createServer(app);
}

// Proxy websockets without the initial http request
// https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade
websocketProxies.forEach(function(wsProxy) {
this.listeningApp.on("upgrade", wsProxy.upgrade);
}, this);
}

Server.prototype.use = function() {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -43,7 +43,8 @@
"style-loader": "~0.13.0",
"supertest": "^2.0.1",
"url-loader": "~0.5.6",
"webpack": "^2.2.0"
"webpack": "^2.2.0",
"ws": "^1.1.1"
},
"license": "MIT",
"repository": {
Expand Down
47 changes: 47 additions & 0 deletions test/Proxy.test.js
Expand Up @@ -3,9 +3,12 @@
const request = require("supertest");
const path = require("path");
const express = require("express");
const WebSocket = require("ws");
const helper = require("./helper");
const should = require("should");
const config = require("./fixtures/proxy-config/webpack.config");

const WebSocketServer = WebSocket.Server;
const contentBase = path.join(__dirname, "fixtures/proxy-config");

const proxyOption = {
Expand Down Expand Up @@ -184,4 +187,48 @@ describe("Proxy", function() {
req.get("/proxy2").expect(200, "from proxy", done);
});
});

context("External websocket upgrade", function() {
let ws;
let wsServer;
let responseMessage;

before(function(done) {
helper.start(config, {
contentBase,
proxy: [{
context: "/",
target: "http://localhost:9003",
ws: true
}]
}, done);

wsServer = new WebSocketServer({ port: 9003 });
wsServer.on("connection", function connection(ws) {
ws.on("message", function incoming(message) {
ws.send(message);
});
});
});

beforeEach(function(done) {
ws = new WebSocket("ws://localhost:8080/proxy3/socket");
ws.on("message", function(message) {
responseMessage = message;
done()
});
ws.on("open", function open() {
ws.send("foo");
});
})

it("Should receive response", function() {
should(responseMessage).equal("foo");
});

after(function(done) {
wsServer.close();
helper.close(done);
});
});
});

0 comments on commit d69559a

Please sign in to comment.