Catching errors in socket callbacks
Catching errors in socket callbacks to avoid having the server crashing when an error occurs.
This commit is contained in:
parent
b50f28176e
commit
256fa51e24
@ -88,46 +88,56 @@ export class IoSocketController {
|
|||||||
y: user y position on map
|
y: user y position on map
|
||||||
*/
|
*/
|
||||||
socket.on(SockerIoEvent.JOIN_ROOM, (message: string) => {
|
socket.on(SockerIoEvent.JOIN_ROOM, (message: string) => {
|
||||||
let messageUserPosition = this.hydrateMessageReceive(message);
|
try {
|
||||||
if (messageUserPosition instanceof Error) {
|
let messageUserPosition = this.hydrateMessageReceive(message);
|
||||||
return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message}))
|
if (messageUserPosition instanceof Error) {
|
||||||
|
return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message}))
|
||||||
|
}
|
||||||
|
|
||||||
|
let Client = (socket as ExSocketInterface);
|
||||||
|
|
||||||
|
if(Client.roomId === messageUserPosition.roomId){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//leave previous room
|
||||||
|
this.leaveRoom(Client);
|
||||||
|
|
||||||
|
//join new previous room
|
||||||
|
this.joinRoom(Client, messageUserPosition);
|
||||||
|
|
||||||
|
// sending to all clients in room except sender
|
||||||
|
this.saveUserInformation(Client, messageUserPosition);
|
||||||
|
|
||||||
|
//add function to refresh position user in real time.
|
||||||
|
this.refreshUserPosition(Client);
|
||||||
|
|
||||||
|
socket.to(messageUserPosition.roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserPosition.toString());
|
||||||
|
} catch (e) {
|
||||||
|
console.error('An error occurred on "join_room" event');
|
||||||
|
console.error(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
let Client = (socket as ExSocketInterface);
|
|
||||||
|
|
||||||
if(Client.roomId === messageUserPosition.roomId){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//leave previous room
|
|
||||||
this.leaveRoom(Client);
|
|
||||||
|
|
||||||
//join new previous room
|
|
||||||
this.joinRoom(Client, messageUserPosition);
|
|
||||||
|
|
||||||
// sending to all clients in room except sender
|
|
||||||
this.saveUserInformation(Client, messageUserPosition);
|
|
||||||
|
|
||||||
//add function to refresh position user in real time.
|
|
||||||
this.refreshUserPosition(Client);
|
|
||||||
|
|
||||||
socket.to(messageUserPosition.roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserPosition.toString());
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on(SockerIoEvent.USER_POSITION, (message: string) => {
|
socket.on(SockerIoEvent.USER_POSITION, (message: string) => {
|
||||||
let messageUserPosition = this.hydrateMessageReceive(message);
|
try {
|
||||||
if (messageUserPosition instanceof Error) {
|
let messageUserPosition = this.hydrateMessageReceive(message);
|
||||||
return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message}));
|
if (messageUserPosition instanceof Error) {
|
||||||
|
return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message}));
|
||||||
|
}
|
||||||
|
|
||||||
|
let Client = (socket as ExSocketInterface);
|
||||||
|
|
||||||
|
// sending to all clients in room except sender
|
||||||
|
this.saveUserInformation(Client, messageUserPosition);
|
||||||
|
|
||||||
|
//refresh position of all user in all rooms in real time
|
||||||
|
this.refreshUserPosition(Client);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('An error occurred on "user_position" event');
|
||||||
|
console.error(e);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
let Client = (socket as ExSocketInterface);
|
|
||||||
|
|
||||||
// sending to all clients in room except sender
|
|
||||||
this.saveUserInformation(Client, messageUserPosition);
|
|
||||||
|
|
||||||
//refresh position of all user in all rooms in real time
|
|
||||||
this.refreshUserPosition(Client);
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on(SockerIoEvent.WEBRTC_SIGNAL, (message: string) => {
|
socket.on(SockerIoEvent.WEBRTC_SIGNAL, (message: string) => {
|
||||||
let data: any = JSON.parse(message);
|
let data: any = JSON.parse(message);
|
||||||
@ -153,24 +163,29 @@ export class IoSocketController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on(SockerIoEvent.DISCONNECT, () => {
|
socket.on(SockerIoEvent.DISCONNECT, () => {
|
||||||
let Client = (socket as ExSocketInterface);
|
try {
|
||||||
this.sendDisconnectedEvent(Client);
|
let Client = (socket as ExSocketInterface);
|
||||||
|
this.sendDisconnectedEvent(Client);
|
||||||
|
|
||||||
//refresh position of all user in all rooms in real time
|
//refresh position of all user in all rooms in real time
|
||||||
this.refreshUserPosition(Client);
|
this.refreshUserPosition(Client);
|
||||||
|
|
||||||
//leave room
|
//leave room
|
||||||
this.leaveRoom(Client);
|
this.leaveRoom(Client);
|
||||||
|
|
||||||
//leave webrtc room
|
//leave webrtc room
|
||||||
socket.leave(Client.webRtcRoomId);
|
socket.leave(Client.webRtcRoomId);
|
||||||
|
|
||||||
//delete all socket information
|
//delete all socket information
|
||||||
delete Client.userId;
|
delete Client.userId;
|
||||||
delete Client.webRtcRoomId;
|
delete Client.webRtcRoomId;
|
||||||
delete Client.roomId;
|
delete Client.roomId;
|
||||||
delete Client.token;
|
delete Client.token;
|
||||||
delete Client.position;
|
delete Client.position;
|
||||||
|
} catch (e) {
|
||||||
|
console.error('An error occurred on "disconnect"');
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,8 @@ export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite {
|
|||||||
this.setSize(16, 16); //edit the hitbox to better match the character model
|
this.setSize(16, 16); //edit the hitbox to better match the character model
|
||||||
this.setOffset(8, 16);
|
this.setOffset(8, 16);
|
||||||
this.setDepth(-1);
|
this.setDepth(-1);
|
||||||
|
|
||||||
|
this.scene.events.on('postupdate', this.postupdate.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
move(x: number, y: number) {
|
move(x: number, y: number) {
|
||||||
@ -69,14 +71,14 @@ export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite {
|
|||||||
if (this.bubble) {
|
if (this.bubble) {
|
||||||
this.bubble.moveBubble(this.x, this.y);
|
this.bubble.moveBubble(this.x, this.y);
|
||||||
}
|
}
|
||||||
this.updatePlayerNamePosition(this.x, this.y);
|
|
||||||
|
|
||||||
//update depth user
|
//update depth user
|
||||||
this.setDepth(this.y);
|
this.setDepth(this.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
updatePlayerNamePosition(x: number, y: number){
|
postupdate(time: number, delta: number) {
|
||||||
this.playerName.setPosition(x, y - 25);
|
//super.update(delta);
|
||||||
|
this.playerName.setPosition(this.x, this.y - 25);
|
||||||
}
|
}
|
||||||
|
|
||||||
stop(){
|
stop(){
|
||||||
@ -95,6 +97,9 @@ export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
destroy(fromScene?: boolean): void {
|
destroy(fromScene?: boolean): void {
|
||||||
|
if (this.scene) {
|
||||||
|
this.scene.events.removeListener('postupdate', this.postupdate.bind(this));
|
||||||
|
}
|
||||||
super.destroy(fromScene);
|
super.destroy(fromScene);
|
||||||
this.playerName.destroy();
|
this.playerName.destroy();
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
|
|||||||
|
|
||||||
let activeEvents = this.userInputManager.getEventListForGameTick();
|
let activeEvents = this.userInputManager.getEventListForGameTick();
|
||||||
let speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
|
let speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
|
||||||
let moveAmount = speedMultiplier * delta;
|
let moveAmount = speedMultiplier * 20;
|
||||||
|
|
||||||
let x = 0;
|
let x = 0;
|
||||||
let y = 0;
|
let y = 0;
|
||||||
@ -102,6 +102,5 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
|
|||||||
this.setX(MessageUserPosition.position.x);
|
this.setX(MessageUserPosition.position.x);
|
||||||
this.setY(MessageUserPosition.position.y);
|
this.setY(MessageUserPosition.position.y);
|
||||||
this.setDepth(MessageUserPosition.position.y);
|
this.setDepth(MessageUserPosition.position.y);
|
||||||
this.updatePlayerNamePosition(MessageUserPosition.position.x, MessageUserPosition.position.y);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user