Skip to content

Commit

Permalink
fix server-side socket listeners
Browse files Browse the repository at this point in the history
The `close` and `error` events are not fired when the server shuts down.

The `message` event is the only event handled differently for the server-side socket and WebSocket client, as it's the handler for the message sent from the client to the server so it should have another event type `server::message`. Other events like `close` and `error` are fired and handled at the same time so no reason to have a special event here.

Make the `server::message` the only special event for the `on` handlers for the server-side sockets and use the same type of listeners for other types to handle all the events properly
  • Loading branch information
Atrue committed Feb 9, 2023
1 parent 1ff5f11 commit 1db5c42
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
15 changes: 0 additions & 15 deletions src/algorithms/close.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,12 @@ export function closeWebSocketConnection(context, code, reason) {
code,
reason
});
const serverCloseEvent = createCloseEvent({
type: 'server::close',
target: context,
code,
reason
});

delay(() => {
networkBridge.removeWebSocket(context, context.url);

context.readyState = WebSocket.CLOSED;
context.dispatchEvent(closeEvent);
context.dispatchEvent(serverCloseEvent);

if (server) {
server.dispatchEvent(closeEvent, server);
Expand All @@ -44,13 +37,6 @@ export function failWebSocketConnection(context, code, reason) {
reason,
wasClean: false
});
const serverCloseEvent = createCloseEvent({
type: 'server::close',
target: context,
code,
reason,
wasClean: false
});

const errorEvent = createEvent({
type: 'error',
Expand All @@ -63,7 +49,6 @@ export function failWebSocketConnection(context, code, reason) {
context.readyState = WebSocket.CLOSED;
context.dispatchEvent(errorEvent);
context.dispatchEvent(closeEvent);
context.dispatchEvent(serverCloseEvent);

if (server) {
server.dispatchEvent(closeEvent, server);
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/proxy-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export default function proxyFactory(target) {

if (prop === 'on') {
return function onWrapper(type, cb) {
target.addEventListener(`server::${type}`, cb);
type = type === 'message' ? `server::${type}` : type;
target.addEventListener(type, cb);
};
}

Expand Down
42 changes: 42 additions & 0 deletions tests/functional/websockets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,48 @@ test.cb('that the server gets called when the client sends a message using URL w
};
});

test.cb('that close event is handled both for the client and WebSocket when the server shuts down', t => {
t.plan(2);

const testServer = new Server('ws://localhost:8080');

testServer.on('connection', socket => {
socket.on('close', event => {
t.is(event.target.readyState, WebSocket.CLOSED, 'close event fires as expected in the client');
t.end();
});

testServer.close();
});

const mockSocket = new WebSocket('ws://localhost:8080');

mockSocket.onclose = event => {
t.is(event.target.readyState, WebSocket.CLOSED, 'close event fires as expected in the WebSocket');
};
});

test.cb('that error event is handled both for the client and WebSocket when the server emits error', t => {
t.plan(2);

const testServer = new Server('ws://localhost:8080');

testServer.on('connection', socket => {
socket.on('error', event => {
t.is(event.target.readyState, WebSocket.CLOSED, 'error event fires as expected on the client');
t.end();
});

testServer.simulate('error');
});

const mockSocket = new WebSocket('ws://localhost:8080');

mockSocket.onerror = event => {
t.is(event.target.readyState, WebSocket.CLOSED, 'error event fires as expected on the WebSocket');
};
});

test.cb('that the onopen function will only be called once for each client', t => {
const socketUrl = 'ws://localhost:8080';
const mockServer = new Server(socketUrl);
Expand Down

0 comments on commit 1db5c42

Please sign in to comment.