Added notification sound on new peer and new chat
parent
2fba13770b
commit
d5e3027106
135
app/lib/LastN.js
135
app/lib/LastN.js
|
|
@ -1,135 +0,0 @@
|
|||
import { EventEmitter } from 'events';
|
||||
import Logger from './Logger';
|
||||
|
||||
const logger = new Logger('LastN');
|
||||
|
||||
export default class LastN extends EventEmitter
|
||||
{
|
||||
constructor(lastNCount, room)
|
||||
{
|
||||
super();
|
||||
|
||||
this._room = room;
|
||||
this._lastNCount = lastNCount;
|
||||
this._peerList = [];
|
||||
this._currentLastN = [];
|
||||
this._started = false;
|
||||
}
|
||||
|
||||
start()
|
||||
{
|
||||
const peers = this._room.peers;
|
||||
|
||||
for (const peer of peers)
|
||||
{
|
||||
this._handlePeer(peer);
|
||||
}
|
||||
|
||||
this._handleRoom();
|
||||
|
||||
this._started = true;
|
||||
this._lastNUpdated();
|
||||
}
|
||||
|
||||
peerInLastN(peerName)
|
||||
{
|
||||
if (this._started)
|
||||
{
|
||||
return this._currentLastN.indexOf(peerName) !== -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
_handleRoom()
|
||||
{
|
||||
this._room.on('newpeer', (peer) =>
|
||||
{
|
||||
logger.debug(
|
||||
'lastN room "newpeer" event [name:"%s", peer:%o]', peer.name, peer);
|
||||
this._handlePeer(peer);
|
||||
});
|
||||
}
|
||||
|
||||
addSpeakerList(speakerList)
|
||||
{
|
||||
this._peerList = [ ...new Set([ ...speakerList, ...this._peerList ]) ];
|
||||
|
||||
if (this._started)
|
||||
this._lastNUpdated();
|
||||
}
|
||||
|
||||
_handlePeer(peer)
|
||||
{
|
||||
logger.debug('_lastN _handlePeer() [peerName:"%s"]', peer.name);
|
||||
|
||||
if (this._peerList.indexOf(peer.name) === -1) // We don't have this peer in the list
|
||||
{
|
||||
peer.on('close', () =>
|
||||
{
|
||||
const index = this._peerList.indexOf(peer.name);
|
||||
|
||||
if (index > -1) // We have this peer in the list, remove
|
||||
{
|
||||
this._peerList.splice(index, 1);
|
||||
|
||||
this._lastNUpdated();
|
||||
}
|
||||
});
|
||||
|
||||
logger.debug('_handlePeer() | adding peer [peerName:"%s"]', peer.name);
|
||||
|
||||
this._peerList.push(peer.name);
|
||||
|
||||
this._lastNUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
handleActiveSpeaker(peerName)
|
||||
{
|
||||
logger.debug('handleActiveSpeaker() [peerName:"%s"]', peerName);
|
||||
|
||||
const index = this._peerList.indexOf(peerName);
|
||||
|
||||
if (index > -1)
|
||||
{
|
||||
this._peerList.splice(index, 1);
|
||||
this._peerList = [ peerName ].concat(this._peerList);
|
||||
|
||||
this._lastNUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
_lastNUpdated()
|
||||
{
|
||||
if (
|
||||
!this._arraysEqual(
|
||||
this._currentLastN, this._peerList.slice(0, this._lastNCount)
|
||||
)
|
||||
)
|
||||
{
|
||||
logger.debug('_lastNUpdated() | lastN is updated, emitting');
|
||||
|
||||
this._currentLastN = this._peerList.slice(0, this._lastNCount);
|
||||
this.emit('lastn-updated', this._currentLastN);
|
||||
}
|
||||
else
|
||||
logger.debug('_lastNUpdated() | lastN not updated');
|
||||
}
|
||||
|
||||
_arraysEqual(arr1, arr2)
|
||||
{
|
||||
if (arr1.length !== arr2.length)
|
||||
return false;
|
||||
|
||||
for (let i = arr1.length; i--;)
|
||||
{
|
||||
if (arr1[i] !== arr2[i])
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -65,6 +65,9 @@ export default class RoomClient
|
|||
// My peer name.
|
||||
this._peerName = peerName;
|
||||
|
||||
// Alert sound
|
||||
this._soundAlert = new Audio('/resources/sounds/notify.mp3');
|
||||
|
||||
// Socket.io peer connection
|
||||
this._signalingSocket = io(signalingUrl);
|
||||
|
||||
|
|
@ -1112,6 +1115,23 @@ export default class RoomClient
|
|||
|
||||
this._dispatch(
|
||||
stateActions.addResponseMessage({ ...chatMessage, peerName }));
|
||||
|
||||
if (!this._getState().toolarea.toolAreaOpen ||
|
||||
(this._getState().toolarea.toolAreaOpen &&
|
||||
this._getState().toolarea.currentToolTab !== 'chat')) // Make sound
|
||||
{
|
||||
const alertPromise = this._soundAlert.play();
|
||||
|
||||
if (alertPromise !== undefined)
|
||||
{
|
||||
alertPromise
|
||||
.then()
|
||||
.catch((error) =>
|
||||
{
|
||||
logger.error('_soundAlert.play() | failed: %o', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this._signalingSocket.on('file-receive', (data) =>
|
||||
|
|
@ -1173,6 +1193,18 @@ export default class RoomClient
|
|||
logger.debug(
|
||||
'room "newpeer" event [name:"%s", peer:%o]', peer.name, peer);
|
||||
|
||||
const alertPromise = this._soundAlert.play();
|
||||
|
||||
if (alertPromise !== undefined)
|
||||
{
|
||||
alertPromise
|
||||
.then()
|
||||
.catch((error) =>
|
||||
{
|
||||
logger.error('_soundAlert.play() | failed: %o', error);
|
||||
});
|
||||
}
|
||||
|
||||
this._handlePeer(peer);
|
||||
});
|
||||
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in New Issue