Skip to content

Commit

Permalink
fix(server): respect sockPath on transportMode: 'ws' (#2310) (#2311)
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-lahtinen authored and hiroppy committed Nov 29, 2019
1 parent 3a31917 commit e188542
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
12 changes: 11 additions & 1 deletion lib/servers/WebsocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ module.exports = class WebsocketServer extends BaseServer {
constructor(server) {
super(server);
this.wsServer = new ws.Server({
server: this.server.listeningApp,
noServer: true,
path: this.server.sockPath,
});

this.server.listeningApp.on('upgrade', (req, sock, head) => {
if (!this.wsServer.shouldHandle(req)) {
return;
}

this.wsServer.handleUpgrade(req, sock, head, (connection) => {
this.wsServer.emit('connection', connection, req);
});
});

this.wsServer.on('error', (err) => {
this.server.log.error(err.message);
});
Expand Down
80 changes: 43 additions & 37 deletions test/server/proxy-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,49 +257,55 @@ describe('proxy option', () => {
let wsServer;
let responseMessage;

beforeAll((done) => {
testServer.start(
config,
{
contentBase,
proxy: [
const transportModes = ['sockjs', 'ws'];
transportModes.forEach((transportMode) => {
describe(`with transportMode: ${transportMode}`, () => {
beforeAll((done) => {
testServer.start(
config,
{
context: '/',
target: `http://localhost:${port4}`,
ws: true,
contentBase,
transportMode,
proxy: [
{
context: '/',
target: `http://localhost:${port4}`,
ws: true,
},
],
port: port3,
},
],
port: port3,
},
done
);

wsServer = new WebSocketServer({ port: port4 });
wsServer.on('connection', (server) => {
server.on('message', (message) => {
server.send(message);
done
);

wsServer = new WebSocketServer({ port: port4 });
wsServer.on('connection', (server) => {
server.on('message', (message) => {
server.send(message);
});
});
});
});
});

beforeEach((done) => {
ws = new WebSocket(`ws://localhost:${port3}/proxy3/socket`);
ws.on('message', (message) => {
responseMessage = message;
done();
});
ws.on('open', () => {
ws.send('foo');
});
});
beforeEach((done) => {
ws = new WebSocket(`ws://localhost:${port3}/proxy3/socket`);
ws.on('message', (message) => {
responseMessage = message;
done();
});
ws.on('open', () => {
ws.send('foo');
});
});

it('Should receive response', () => {
expect(responseMessage).toEqual('foo');
});
it('Should receive response', () => {
expect(responseMessage).toEqual('foo');
});

afterAll((done) => {
wsServer.close();
testServer.close(done);
afterAll((done) => {
wsServer.close();
testServer.close(done);
});
});
});
});

Expand Down

0 comments on commit e188542

Please sign in to comment.