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

Remove socket listeners on channel disconnect #1375

Merged
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
4 changes: 4 additions & 0 deletions erizo_controller/erizoController/models/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ class Channel extends events.EventEmitter {
this.socket.on(eventName, listener);
}

socketRemoveListener(eventName, listener) {
this.socket.removeListener(eventName, listener);
}

onReconnected(clientId) {
this.state = CONNECTED;
this.emit('reconnected', clientId);
Expand Down
45 changes: 29 additions & 16 deletions erizo_controller/erizoController/models/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@ const log = logger.getLogger('ErizoController - Client');
const PUBLISHER_INITAL = 101;
const PUBLISHER_READY = 104;

function listenToSocketEvents(client) {
client.channel.socketOn('sendDataStream', client.onSendDataStream.bind(client));
client.channel.socketOn('signaling_message', client.onSignalingMessage.bind(client));
client.channel.socketOn('updateStreamAttributes', client.onUpdateStreamAttributes.bind(client));
client.channel.socketOn('publish', client.onPublish.bind(client));
client.channel.socketOn('subscribe', client.onSubscribe.bind(client));
client.channel.socketOn('startRecorder', client.onStartRecorder.bind(client));
client.channel.socketOn('stopRecorder', client.onStopRecorder.bind(client));
client.channel.socketOn('unpublish', client.onUnpublish.bind(client));
client.channel.socketOn('unsubscribe', client.onUnsubscribe.bind(client));
client.channel.socketOn('getStreamStats', client.onGetStreamStats.bind(client));
client.channel.on('disconnect', client.onDisconnect.bind(client));
}

class Client extends events.EventEmitter {
constructor(channel, token, options, room) {
super();
Expand All @@ -34,7 +20,8 @@ class Client extends events.EventEmitter {
this.token = token;
this.id = uuidv4();
this.options = options;
listenToSocketEvents(this);
this.socketEventListeners = new Map();
this.listenToSocketEvents();
this.user = { name: token.userName, role: token.role, permissions: {} };
const permissions = global.config.erizoController.roles[token.role] || {};
Object.keys(permissions).forEach((right) => {
Expand All @@ -44,7 +31,32 @@ class Client extends events.EventEmitter {
this.state = 'sleeping'; // ?
}

listenToSocketEvents() {
log.debug(`message: Adding listeners to socket events, client.id: ${this.id}`);
this.socketEventListeners.set('sendDataStream', this.onSendDataStream.bind(this));
this.socketEventListeners.set('signaling_message', this.onSignalingMessage.bind(this));
this.socketEventListeners.set('updateStreamAttributes', this.onUpdateStreamAttributes.bind(this));
this.socketEventListeners.set('publish', this.onPublish.bind(this));
this.socketEventListeners.set('subscribe', this.onSubscribe.bind(this));
this.socketEventListeners.set('startRecorder', this.onStartRecorder.bind(this));
this.socketEventListeners.set('stopRecorder', this.onStopRecorder.bind(this));
this.socketEventListeners.set('unpublish', this.onUnpublish.bind(this));
this.socketEventListeners.set('unsubscribe', this.onUnsubscribe.bind(this));
this.socketEventListeners.set('getStreamStats', this.onGetStreamStats.bind(this));
this.socketEventListeners.forEach((value, key) => {
this.channel.socketOn(key, value);
});
this.channel.on('disconnect', this.onDisconnect.bind(this));
}
stopListeningToSocketEvents() {
log.debug(`message: Removing listeners to socket events, client.id: ${this.id}`);
this.socketEventListeners.forEach((value, key) => {
this.channel.socketRemoveListener(key, value);
});
}

disconnect() {
this.stopListeningToSocketEvents();
this.channel.disconnect();
}

Expand All @@ -55,7 +67,7 @@ class Client extends events.EventEmitter {
oldChannel.removeAllListeners();
oldChannel.disconnect();
this.channel = channel;
listenToSocketEvents(this);
this.listenToSocketEvents();
this.channel.sendBuffer(buffer);
}

Expand Down Expand Up @@ -496,6 +508,7 @@ class Client extends events.EventEmitter {
}

onDisconnect() {
this.stopListeningToSocketEvents();
const timeStamp = new Date();

log.info(`message: Channel disconnect, clientId: ${this.id}`, ', channelId:', this.channel.id);
Expand Down
1 change: 1 addition & 0 deletions erizo_controller/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ module.exports.reset = () => {
disconnect: sinon.stub(),
emit: sinon.stub(),
on: sinon.stub(),
removeListener: sinon.stub(),
};

module.exports.socketIoInstance = {
Expand Down