Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): respect sockPath on transportMode: 'ws' (#2310) #2311

Merged
merged 2 commits into from
Nov 29, 2019
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: 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