Refactoring messages

Socket.io can stringify JSON messages itself, so there is no need to pass a string to "emit". You can pass a serializable object!

This commit removes all the useless toJson() methods, JSON.serialize and JSON.parse!

Woot!
This commit is contained in:
David Négrier 2020-05-15 22:04:49 +02:00
parent 2411a3f85a
commit 5a3668a12e
3 changed files with 25 additions and 55 deletions

View file

@ -34,22 +34,12 @@ class Message {
this.name = name;
this.character = character;
}
toJson() {
return {
userId: this.userId,
roomId: this.roomId,
name: this.name,
character: this.character
}
}
}
export interface PointInterface {
x: number;
y: number;
direction : string;
toJson() : object;
}
class Point implements PointInterface{
@ -65,14 +55,6 @@ class Point implements PointInterface{
this.y = y;
this.direction = direction;
}
toJson(){
return {
x : this.x,
y: this.y,
direction: this.direction
}
}
}
export interface MessageUserPositionInterface {
@ -90,16 +72,6 @@ class MessageUserPosition extends Message implements MessageUserPositionInterfac
super(userId, roomId, name, character);
this.position = point;
}
toString() {
return JSON.stringify(
Object.assign(
super.toJson(),
{
position: this.position.toJson()
})
);
}
}
export interface ListMessageUserPositionInterface {
@ -274,7 +246,7 @@ export class Connexion implements ConnexionInterface {
this.email,
character
);
this.socket.emit(EventMessage.JOIN_ROOM, messageUserPosition.toString());
this.socket.emit(EventMessage.JOIN_ROOM, messageUserPosition);
}
/**
@ -297,7 +269,7 @@ export class Connexion implements ConnexionInterface {
character
);
this.lastPositionShared = messageUserPosition;
this.socket.emit(EventMessage.USER_POSITION, messageUserPosition.toString());
this.socket.emit(EventMessage.USER_POSITION, messageUserPosition);
}
attributeUserId(): void {
@ -326,7 +298,7 @@ export class Connexion implements ConnexionInterface {
**/
positionOfAllUser(): void {
this.socket.on(EventMessage.USER_POSITION, (message: string) => {
let dataList = JSON.parse(message);
let dataList = message;
let UserPositions : Array<any> = Object.values(dataList);
let listMessageUserPosition = new ListMessageUserPosition(UserPositions[0], UserPositions[1]);
this.GameManager.shareUserPosition(listMessageUserPosition);
@ -347,12 +319,12 @@ export class Connexion implements ConnexionInterface {
}
sendWebrtcSignal(signal: any, roomId: string, userId? : string, receiverId? : string) {
return this.socket.emit(EventMessage.WEBRTC_SIGNAL, JSON.stringify({
return this.socket.emit(EventMessage.WEBRTC_SIGNAL, {
userId: userId ? userId : this.userId,
receiverId: receiverId ? receiverId : this.userId,
roomId: roomId,
signal: signal
}));
});
}
receiveWebrtcStart(callback: Function) {

View file

@ -32,7 +32,7 @@ export class SimplePeer implements SimplePeerInterface{
private initialise() {
//receive signal by gemer
this.Connexion.receiveWebrtcSignal((message: string) => {
this.Connexion.receiveWebrtcSignal((message: any) => {
this.receiveWebrtcSignal(message);
});
@ -40,7 +40,7 @@ export class SimplePeer implements SimplePeerInterface{
this.MediaManager.getCamera().then(() => {
//receive message start
this.Connexion.receiveWebrtcStart((message: string) => {
this.Connexion.receiveWebrtcStart((message: any) => {
this.receiveWebrtcStart(message);
});
@ -49,8 +49,8 @@ export class SimplePeer implements SimplePeerInterface{
});
//receive signal by gemer
this.Connexion.disconnectMessage((message: string) => {
let data = JSON.parse(message);
this.Connexion.disconnectMessage((message: any) => {
let data = message;
this.closeConnexion(data.userId);
});
}
@ -59,8 +59,8 @@ export class SimplePeer implements SimplePeerInterface{
*
* @param message
*/
private receiveWebrtcStart(message: string) {
let data = JSON.parse(message);
private receiveWebrtcStart(message: any) {
let data = message;
this.WebRtcRoomId = data.roomId;
this.Users = data.clients;
@ -197,8 +197,8 @@ export class SimplePeer implements SimplePeerInterface{
*
* @param message
*/
private receiveWebrtcSignal(message: string) {
let data = JSON.parse(message);
private receiveWebrtcSignal(message: any) {
let data = message;
try {
//if offer type, create peer connexion
if(data.signal.type === "offer"){