Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
});
});
});