Skip to content

Commit

Permalink
Merge pull request #21 from thoov/multipleCloseIssue
Browse files Browse the repository at this point in the history
If you call close on a non open socket it will act like it is open
  • Loading branch information
thoov committed Mar 30, 2015
2 parents 820f7d7 + f3d23f8 commit 8300125
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
12 changes: 7 additions & 5 deletions dist/mock-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,12 @@ SocketService.prototype = {
* @param {client: object} the context of the client
*/
closeConnectionFromClient: function(messageEvent, client) {
this.notifyOnlyFor(client, 'updateReadyState', globalContext.MockSocket.CLOSING);
this.notifyOnlyFor(client, 'clientOnclose', messageEvent);
this.notifyOnlyFor(client, 'updateReadyState', globalContext.MockSocket.CLOSED);
this.notify('clientHasLeft');
if(client.readyState === globalContext.MockSocket.OPEN) {
this.notifyOnlyFor(client, 'updateReadyState', globalContext.MockSocket.CLOSING);
this.notifyOnlyFor(client, 'clientOnclose', messageEvent);
this.notifyOnlyFor(client, 'updateReadyState', globalContext.MockSocket.CLOSED);
this.notify('clientHasLeft');
}
},


Expand All @@ -510,7 +512,7 @@ SocketService.prototype = {
* @param {messageEvent: object} the mock message event.
*/
sendMessageToServer: function(messageEvent) {
this.notify('clientHasSentMessage', messageEvent.data);
this.notify('clientHasSentMessage', messageEvent.data, messageEvent);
},

/*
Expand Down
2 changes: 1 addition & 1 deletion dist/mock-socket.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ SocketService.prototype = {
* @param {client: object} the context of the client
*/
closeConnectionFromClient: function(messageEvent, client) {
this.notifyOnlyFor(client, 'updateReadyState', globalContext.MockSocket.CLOSING);
this.notifyOnlyFor(client, 'clientOnclose', messageEvent);
this.notifyOnlyFor(client, 'updateReadyState', globalContext.MockSocket.CLOSED);
this.notify('clientHasLeft');
if(client.readyState === globalContext.MockSocket.OPEN) {
this.notifyOnlyFor(client, 'updateReadyState', globalContext.MockSocket.CLOSING);
this.notifyOnlyFor(client, 'clientOnclose', messageEvent);
this.notifyOnlyFor(client, 'updateReadyState', globalContext.MockSocket.CLOSED);
this.notify('clientHasLeft');
}
},


Expand Down
19 changes: 19 additions & 0 deletions tests/on-close-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,22 @@ asyncTest('that the mocksocket onclose function is called after closing the mock

mockServer.close();
});

asyncTest('that closing a mock socket will only call the mock servers onclose callback once', function() {
var socketUrl = 'ws://localhost:8080';
var mockServer = new MockServer(socketUrl);
var mockSocket = new MockSocket(socketUrl);

expect(1);

mockServer.on('close', function() {
ok(true, 'mock server on close fires as expected');
});

mockSocket.close();
mockSocket.close();

setTimeout(function() {
start();
}, 100);
});

0 comments on commit 8300125

Please sign in to comment.