diff --git a/server/lib/Lobby.js b/server/lib/Lobby.js index c2c22f0..36644ab 100644 --- a/server/lib/Lobby.js +++ b/server/lib/Lobby.js @@ -87,36 +87,28 @@ class Lobby extends EventEmitter if (this._closed) return; - peer.socket.emit('notification', { method: 'enteredLobby', data: {} }); + this._notification(peer.socket, 'enteredLobby'); this._peers.set(peer.id, peer); - peer.on('authenticationChange', () => + peer.on('authenticationChanged', () => { logger.info('parkPeer() | authenticationChange [peer:"%s"]', peer.id); peer.authenticated && this.emit('peerAuthenticated', peer); }); - peer.socket.on('request', (request, cb) => + peer.on('displayNameChanged', () => { - logger.debug( - 'Peer "request" event [method:"%s", peer:"%s"]', - request.method, peer.id); - - if (this._closed) - return; - - this._handleSocketRequest(peer, request, cb) - .catch((error) => - { - logger.error('request failed [error:"%o"]', error); - - cb(error); - }); + this.emit('displayNameChanged', peer); }); - peer.socket.on('disconnect', () => + peer.on('pictureChanged', () => + { + this.emit('pictureChanged', peer); + }); + + peer.on('close', () => { logger.debug('Peer "close" event [peer:"%s"]', peer.id); @@ -132,34 +124,6 @@ class Lobby extends EventEmitter }); } - async _handleSocketRequest(peer, request, cb) - { - logger.debug( - '_handleSocketRequest [peer:"%s"], [request:"%s"]', - peer.id, - request.method - ); - - if (this._closed) - return; - - switch (request.method) - { - case 'changeDisplayName': - { - const { displayName } = request.data; - - peer.displayName = displayName; - - this.emit('lobbyPeerDisplayNameChanged', peer); - - cb(); - - break; - } - } - } - _notification(socket, method, data = {}, broadcast = false) { if (broadcast) diff --git a/server/lib/Peer.js b/server/lib/Peer.js index dcd02cd..847ac52 100644 --- a/server/lib/Peer.js +++ b/server/lib/Peer.js @@ -69,6 +69,24 @@ class Peer extends EventEmitter return next(); }); + this.socket.on('request', (request, cb) => + { + logger.debug( + 'Peer "request" event [method:"%s", peer:"%s"]', + request.method, this.id); + + if (this._closed) + return; + + this._handleSocketRequest(request, cb) + .catch((error) => + { + logger.error('request failed [error:"%o"]', error); + + cb(error); + }); + }); + this.socket.on('disconnect', () => { if (this.closed) @@ -80,6 +98,32 @@ class Peer extends EventEmitter }); } + async _handleSocketRequest(request, cb) + { + logger.debug( + '_handleSocketRequest [peer:"%s"], [request:"%s"]', + this.id, + request.method + ); + + if (this._closed) + return; + + switch (request.method) + { + case 'changeDisplayName': + { + const { displayName } = request.data; + + this.displayName = displayName; + + cb(); + + break; + } + } + } + _checkAuthentication() { this.authenticated = @@ -131,8 +175,11 @@ class Peer extends EventEmitter { if (authenticated !== this._authenticated) { + const oldAuthenticated = this._authenticated; + this._authenticated = authenticated; - this.emit('authenticationChange'); + + this.emit('authenticationChanged', { oldAuthenticated }); } } @@ -143,7 +190,14 @@ class Peer extends EventEmitter set displayName(displayName) { - this._displayName = displayName; + if (displayName !== this._displayName) + { + const oldDisplayName = this._displayName; + + this._displayName = displayName; + + this.emit('displayNameChanged', { oldDisplayName }); + } } get picture() @@ -153,7 +207,14 @@ class Peer extends EventEmitter set picture(picture) { - this._picture = picture; + if (picture !== this._picture) + { + const oldPicture = this._picture; + + this._picture = picture; + + this.emit('pictureChanged', { oldPicture }); + } } get device() diff --git a/server/lib/Room.js b/server/lib/Room.js index b8c8bf8..4567495 100644 --- a/server/lib/Room.js +++ b/server/lib/Room.js @@ -150,7 +150,12 @@ class Room extends EventEmitter }); }); - this._lobby.on('lobbyPeerDisplayNameChanged', (changedPeer) => + this._lobby.on('peerAuthenticated', (peer) => + { + !this._locked && this._lobby.promotePeer(peer.id); + }); + + this._lobby.on('displayNameChanged', (changedPeer) => { const { id, displayName } = changedPeer; @@ -160,9 +165,14 @@ class Room extends EventEmitter }); }); - this._lobby.on('peerAuthenticated', (peer) => + this._lobby.on('pictureChanged', (changedPeer) => { - !this._locked && this._lobby.promotePeer(peer.id); + const { id, picture } = changedPeer; + + this._peers.forEach((peer) => + { + this._notification(peer.socket, 'lobbyPeerPictureChanged', { peerId: id, picture }); + }); }); this._lobby.on('peerClosed', (closedPeer) => @@ -305,6 +315,31 @@ class Room extends EventEmitter }); }); + peer.on('displayNameChanged', ({ oldDisplayName }) => + { + if (!peer.joined) + return; + + // Spread to others + this._notification(peer.socket, 'changeDisplayName', { + peerId : peer.id, + displayName : peer.displayName, + oldDisplayName : oldDisplayName + }, true); + }); + + peer.on('pictureChanged', () => + { + if (!peer.joined) + return; + + // Spread to others + this._notification(peer.socket, 'changeProfilePicture', { + peerId : peer.id, + picture : peer.picture + }, true); + }); + peer.on('close', () => { if (this._closed) @@ -726,50 +761,6 @@ class Room extends EventEmitter break; } - case 'changeDisplayName': - { - // Ensure the Peer is joined. - if (!peer.joined) - throw new Error('Peer not yet joined'); - - const { displayName } = request.data; - const oldDisplayName = peer.displayName; - - peer.displayName = displayName; - - // Spread to others - this._notification(peer.socket, 'changeDisplayName', { - peerId : peer.id, - displayName : displayName, - oldDisplayName : oldDisplayName - }, true); - - // Return no error - cb(); - - break; - } - - case 'changeProfilePicture': - { - // Ensure the Peer is joined. - if (!peer.joined) - throw new Error('Peer not yet joined'); - - const { picture } = request.data; - - // Spread to others - this._notification(peer.socket, 'changeProfilePicture', { - peerId : peer.id, - picture : picture - }, true); - - // Return no error - cb(); - - break; - } - case 'chatMessage': { const { chatMessage } = request.data;