Skip to content

Commit

Permalink
[fix] Prevent on<event> setters from removing all listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbogers authored and lpinca committed Jan 9, 2017
1 parent c843e6a commit 420eb16
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/WebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ class WebSocket extends EventEmitter {
*/
emitClose () {
this.readyState = WebSocket.CLOSED;
// emit 'cleanup' before 'close' because close will change attributes
// and closed ws-sockets will not be found 'WebSocketServer.clients' js-Set collection
this.emit('close', this._closeCode || 1006, this._closeMessage || '');

if (this.extensions[PerMessageDeflate.extensionName]) {
Expand Down Expand Up @@ -409,8 +411,10 @@ WebSocket.CLOSED = 3;
* @public
*/
get () {
const listener = this.listeners(method)[0];
return listener ? listener._listener ? listener._listener : listener : undefined;
const listeners = this.listeners(method);
for (var i = 0; i < listeners.length; i++) {
if (listeners[i]._listener) return listeners[i]._listener;
}
},
/**
* Add a listener for the event.
Expand All @@ -419,7 +423,11 @@ WebSocket.CLOSED = 3;
* @public
*/
set (listener) {
this.removeAllListeners(method);
const listeners = this.listeners(method);
for (var i = 0; i < listeners.length; i++) {
/* listeners added outside usage of 'addEventListeners' are not removed */
if (listeners[i]._listener) this.removeListener(method, listeners[i]);
}
this.addEventListener(method, listener);
}
});
Expand Down

0 comments on commit 420eb16

Please sign in to comment.