From 1b0d110a3cf29a2af64a6232c4cb1e717f3ffe83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Alonso?= Date: Fri, 4 Aug 2017 13:33:36 +0200 Subject: [PATCH] Fixed a bug that did't allow simultaneous clients in different rooms (#16) --- common/api.js | 33 +++++++++++++++++++----------- controllers/ackuaria_controller.js | 5 +---- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/common/api.js b/common/api.js index 4b7d28a..d902661 100644 --- a/common/api.js +++ b/common/api.js @@ -19,7 +19,6 @@ API.rooms = {}; API.streams = {}; API.users = {}; API.states = {}; -API.currentRoom; API.sessions_active = {}; API.lastUpdated; // key: socketId, value: socket @@ -113,16 +112,28 @@ const removeConnection = (socketId) => { API.sockets.delete(socketId); }; +function getParameterByName(name, url) { + name = name.replace(/[\[\]]/g, "\\$&"); + var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), + results = regex.exec(url); + if (!results) return ''; + if (!results[2]) return ''; + return decodeURIComponent(results[2].replace(/\+/g, " ")); +} + const sendEventToClients = function(event, rooms, streams, users, states) { API.sockets.forEach((currentSocket, currentSocketId) => { - currentSocket.emit('newEvent', { - event: event, - rooms: rooms, - streams: streams, - users: users, - states: states - }); - }); + var clientCurrentRoom = getParameterByName('room_id', currentSocket.handshake.headers.referer); + if (clientCurrentRoom == event.roomID || clientCurrentRoom == '') { + currentSocket.emit('newEvent', { + event: event, + rooms: rooms, + streams: streams, + users: users, + states: states + }); + } + }); }; API.api = {}; @@ -454,9 +465,7 @@ API.api.event = function(theEvent) { default: break; } - if (API.currentRoom == event.roomID || API.currentRoom == "") { - sendEventToClients(event, API.rooms, API.streams, API.users, API.states); - } + sendEventToClients(event, API.rooms, API.streams, API.users, API.states); if (config.ackuaria.useDB) { eventsRegistry.addEvent(theEvent, function(saved, error) { if (error) log.error(error); diff --git a/controllers/ackuaria_controller.js b/controllers/ackuaria_controller.js index 1e9afa6..4924571 100644 --- a/controllers/ackuaria_controller.js +++ b/controllers/ackuaria_controller.js @@ -39,7 +39,6 @@ exports.updateRooms = function(req, res, next) { } exports.loadRooms = function(req, res) { - API.currentRoom = ""; res.render('rooms', { view: "rooms", rooms: API.rooms @@ -49,7 +48,6 @@ exports.loadRooms = function(req, res) { exports.loadPublishers = function(req, res) { var roomID = req.query.room_id; var fails = req.query.fails; - API.currentRoom = roomID; var room = API.rooms[roomID]; if (API.rooms[roomID]) var roomName = API.rooms[roomID].roomName; @@ -88,8 +86,7 @@ exports.loadSubscribers = function(req, res) { var streamID = req.query.pub_id; var roomID = req.query.room_id; var room = API.rooms[roomID]; - - API.currentRoom = roomID; + if (API.streams[streamID]) var userName = API.streams[streamID].userName; else var userName = "Publisher not found";