Skip to content

Commit 7d38597

Browse files
fix(server): respect sockPath on transportMode: 'ws' (webpack#2310)
1 parent 7cb22cc commit 7d38597

File tree

2 files changed

+54
-38
lines changed

2 files changed

+54
-38
lines changed

lib/servers/WebsocketServer.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@ module.exports = class WebsocketServer extends BaseServer {
1010
constructor(server) {
1111
super(server);
1212
this.wsServer = new ws.Server({
13-
server: this.server.listeningApp,
13+
noServer: true,
1414
path: this.server.sockPath,
1515
});
1616

17+
this.server.listeningApp.on('upgrade', (req, sock, head) => {
18+
if (!this.wsServer.shouldHandle(req)) {
19+
return;
20+
}
21+
22+
this.wsServer.handleUpgrade(req, sock, head, (connection) => {
23+
this.wsServer.emit('connection', connection, req);
24+
});
25+
});
26+
1727
this.wsServer.on('error', (err) => {
1828
this.server.log.error(err.message);
1929
});

test/server/proxy-option.test.js

+43-37
Original file line numberDiff line numberDiff line change
@@ -257,49 +257,55 @@ describe('proxy option', () => {
257257
let wsServer;
258258
let responseMessage;
259259

260-
beforeAll((done) => {
261-
testServer.start(
262-
config,
263-
{
264-
contentBase,
265-
proxy: [
260+
const transportModes = ['sockjs', 'ws'];
261+
transportModes.forEach((transportMode) => {
262+
describe(`with transportMode: ${transportMode}`, () => {
263+
beforeAll((done) => {
264+
testServer.start(
265+
config,
266266
{
267-
context: '/',
268-
target: `http://localhost:${port4}`,
269-
ws: true,
267+
contentBase,
268+
transportMode,
269+
proxy: [
270+
{
271+
context: '/',
272+
target: `http://localhost:${port4}`,
273+
ws: true,
274+
},
275+
],
276+
port: port3,
270277
},
271-
],
272-
port: port3,
273-
},
274-
done
275-
);
276-
277-
wsServer = new WebSocketServer({ port: port4 });
278-
wsServer.on('connection', (server) => {
279-
server.on('message', (message) => {
280-
server.send(message);
278+
done
279+
);
280+
281+
wsServer = new WebSocketServer({ port: port4 });
282+
wsServer.on('connection', (server) => {
283+
server.on('message', (message) => {
284+
server.send(message);
285+
});
286+
});
281287
});
282-
});
283-
});
284288

285-
beforeEach((done) => {
286-
ws = new WebSocket(`ws://localhost:${port3}/proxy3/socket`);
287-
ws.on('message', (message) => {
288-
responseMessage = message;
289-
done();
290-
});
291-
ws.on('open', () => {
292-
ws.send('foo');
293-
});
294-
});
289+
beforeEach((done) => {
290+
ws = new WebSocket(`ws://localhost:${port3}/proxy3/socket`);
291+
ws.on('message', (message) => {
292+
responseMessage = message;
293+
done();
294+
});
295+
ws.on('open', () => {
296+
ws.send('foo');
297+
});
298+
});
295299

296-
it('Should receive response', () => {
297-
expect(responseMessage).toEqual('foo');
298-
});
300+
it('Should receive response', () => {
301+
expect(responseMessage).toEqual('foo');
302+
});
299303

300-
afterAll((done) => {
301-
wsServer.close();
302-
testServer.close(done);
304+
afterAll((done) => {
305+
wsServer.close();
306+
testServer.close(done);
307+
});
308+
});
303309
});
304310
});
305311

0 commit comments

Comments
 (0)