Restructured code.
parent
85d1973a6c
commit
cacc9abf7c
|
|
@ -87,36 +87,28 @@ class Lobby extends EventEmitter
|
||||||
if (this._closed)
|
if (this._closed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
peer.socket.emit('notification', { method: 'enteredLobby', data: {} });
|
this._notification(peer.socket, 'enteredLobby');
|
||||||
|
|
||||||
this._peers.set(peer.id, peer);
|
this._peers.set(peer.id, peer);
|
||||||
|
|
||||||
peer.on('authenticationChange', () =>
|
peer.on('authenticationChanged', () =>
|
||||||
{
|
{
|
||||||
logger.info('parkPeer() | authenticationChange [peer:"%s"]', peer.id);
|
logger.info('parkPeer() | authenticationChange [peer:"%s"]', peer.id);
|
||||||
|
|
||||||
peer.authenticated && this.emit('peerAuthenticated', peer);
|
peer.authenticated && this.emit('peerAuthenticated', peer);
|
||||||
});
|
});
|
||||||
|
|
||||||
peer.socket.on('request', (request, cb) =>
|
peer.on('displayNameChanged', () =>
|
||||||
{
|
{
|
||||||
logger.debug(
|
this.emit('displayNameChanged', peer);
|
||||||
'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);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
peer.socket.on('disconnect', () =>
|
peer.on('pictureChanged', () =>
|
||||||
|
{
|
||||||
|
this.emit('pictureChanged', peer);
|
||||||
|
});
|
||||||
|
|
||||||
|
peer.on('close', () =>
|
||||||
{
|
{
|
||||||
logger.debug('Peer "close" event [peer:"%s"]', peer.id);
|
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)
|
_notification(socket, method, data = {}, broadcast = false)
|
||||||
{
|
{
|
||||||
if (broadcast)
|
if (broadcast)
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,24 @@ class Peer extends EventEmitter
|
||||||
return next();
|
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', () =>
|
this.socket.on('disconnect', () =>
|
||||||
{
|
{
|
||||||
if (this.closed)
|
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()
|
_checkAuthentication()
|
||||||
{
|
{
|
||||||
this.authenticated =
|
this.authenticated =
|
||||||
|
|
@ -131,8 +175,11 @@ class Peer extends EventEmitter
|
||||||
{
|
{
|
||||||
if (authenticated !== this._authenticated)
|
if (authenticated !== this._authenticated)
|
||||||
{
|
{
|
||||||
|
const oldAuthenticated = this._authenticated;
|
||||||
|
|
||||||
this._authenticated = authenticated;
|
this._authenticated = authenticated;
|
||||||
this.emit('authenticationChange');
|
|
||||||
|
this.emit('authenticationChanged', { oldAuthenticated });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -143,7 +190,14 @@ class Peer extends EventEmitter
|
||||||
|
|
||||||
set displayName(displayName)
|
set displayName(displayName)
|
||||||
{
|
{
|
||||||
this._displayName = displayName;
|
if (displayName !== this._displayName)
|
||||||
|
{
|
||||||
|
const oldDisplayName = this._displayName;
|
||||||
|
|
||||||
|
this._displayName = displayName;
|
||||||
|
|
||||||
|
this.emit('displayNameChanged', { oldDisplayName });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get picture()
|
get picture()
|
||||||
|
|
@ -153,7 +207,14 @@ class Peer extends EventEmitter
|
||||||
|
|
||||||
set picture(picture)
|
set picture(picture)
|
||||||
{
|
{
|
||||||
this._picture = picture;
|
if (picture !== this._picture)
|
||||||
|
{
|
||||||
|
const oldPicture = this._picture;
|
||||||
|
|
||||||
|
this._picture = picture;
|
||||||
|
|
||||||
|
this.emit('pictureChanged', { oldPicture });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get device()
|
get device()
|
||||||
|
|
|
||||||
|
|
@ -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;
|
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) =>
|
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', () =>
|
peer.on('close', () =>
|
||||||
{
|
{
|
||||||
if (this._closed)
|
if (this._closed)
|
||||||
|
|
@ -726,50 +761,6 @@ class Room extends EventEmitter
|
||||||
break;
|
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':
|
case 'chatMessage':
|
||||||
{
|
{
|
||||||
const { chatMessage } = request.data;
|
const { chatMessage } = request.data;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue