From 0c4c43f88c55e4bbd22fdbc96bc5ef73d5d0ffa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 24 Sep 2020 16:11:47 +0200 Subject: [PATCH] Adding USER_LEFT message to protobuf --- back/src/Controller/IoSocketController.ts | 16 +++++++++++++--- front/src/Connection.ts | 9 +++++++-- messages/messages.proto | 5 +++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index 3e0eeb2e..9110702e 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -27,7 +27,7 @@ import { SetPlayerDetailsMessage, SubMessage, UserMovedMessage, - BatchMessage, GroupUpdateMessage, PointMessage, GroupDeleteMessage, UserJoinedMessage + BatchMessage, GroupUpdateMessage, PointMessage, GroupDeleteMessage, UserJoinedMessage, UserLeftMessage } from "../Messages/generated/messages_pb"; import {UserMovesMessage} from "../Messages/generated/messages_pb"; import Direction = PositionMessage.Direction; @@ -580,8 +580,7 @@ export class IoSocketController { const clientListener = this.searchClientByIdOrFail(listener.id); if (thing instanceof User) { const clientUser = this.searchClientByIdOrFail(thing.id); - clientListener.emit(SocketIoEvent.USER_LEFT, clientUser.userId); - //console.log("Sending USER_LEFT event"); + this.emitUserLeftEvent(clientListener, clientUser.userId); } else if (thing instanceof Group) { this.emitDeleteGroupEvent(clientListener, thing.getId()); } else { @@ -629,6 +628,17 @@ export class IoSocketController { emitInBatch(client, SocketIoEvent.GROUP_DELETE, subMessage); } + private emitUserLeftEvent(socket: Socket, userId: number): void { + const userLeftMessage = new UserLeftMessage(); + userLeftMessage.setUserid(userId); + + const subMessage = new SubMessage(); + subMessage.setUserleftmessage(userLeftMessage); + + const client : ExSocketInterface = socket as ExSocketInterface; + emitInBatch(client, SocketIoEvent.USER_LEFT, subMessage); + } + /** * * @param socket diff --git a/front/src/Connection.ts b/front/src/Connection.ts index d64f4643..faf080f0 100644 --- a/front/src/Connection.ts +++ b/front/src/Connection.ts @@ -4,7 +4,7 @@ import {MessageUI} from "./Logger/MessageUI"; import { BatchMessage, GroupDeleteMessage, GroupUpdateMessage, PositionMessage, - SetPlayerDetailsMessage, UserJoinedMessage, UserMovedMessage, + SetPlayerDetailsMessage, UserJoinedMessage, UserLeftMessage, UserMovedMessage, UserMovesMessage, ViewportMessage } from "./Messages/generated/messages_pb" @@ -170,6 +170,9 @@ export class Connection implements Connection { } else if (message.hasUserjoinedmessage()) { event = EventMessage.JOIN_ROOM; payload = message.getUserjoinedmessage(); + } else if (message.hasUserleftmessage()) { + event = EventMessage.USER_LEFT; + payload = message.getUserleftmessage(); } else { throw new Error('Unexpected batch message type'); } @@ -320,7 +323,9 @@ export class Connection implements Connection { } public onUserLeft(callback: (userId: number) => void): void { - this.socket.on(EventMessage.USER_LEFT, callback); + this.onBatchMessage(EventMessage.USER_LEFT, (message: UserLeftMessage) => { + callback(message.getUserid()); + }); } public onGroupUpdatedOrCreated(callback: (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => void): void { diff --git a/messages/messages.proto b/messages/messages.proto index 50a0aa5d..01782e57 100644 --- a/messages/messages.proto +++ b/messages/messages.proto @@ -53,6 +53,7 @@ message SubMessage { GroupUpdateMessage groupUpdateMessage = 2; GroupDeleteMessage groupDeleteMessage = 3; UserJoinedMessage userJoinedMessage = 4; + UserLeftMessage userLeftMessage = 5; } } @@ -76,3 +77,7 @@ message UserJoinedMessage { repeated string characterLayers = 3; PositionMessage position = 4; } + +message UserLeftMessage { + int32 userId = 1; +}