From ac80850335be06bd3fa0b391320a410fb46740db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 24 Sep 2020 17:36:10 +0200 Subject: [PATCH] Switching SetViewport to protobuf --- back/src/Controller/IoSocketController.ts | 15 ++++++++------- front/src/Connection.ts | 8 +++++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index 57e204f4..e7305d15 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -19,7 +19,6 @@ import {isWebRtcSignalMessageInterface} from "../Model/Websocket/WebRtcSignalMes import {UserInGroupInterface} from "../Model/Websocket/UserInGroupInterface"; import {isItemEventMessageInterface} from "../Model/Websocket/ItemEventMessage"; import {uuid} from 'uuidv4'; -import {isViewport} from "../Model/Websocket/ViewportMessage"; import {GroupUpdateInterface} from "_Model/Websocket/GroupUpdateInterface"; import {Movable} from "../Model/Movable"; import { @@ -33,7 +32,7 @@ import { GroupDeleteMessage, UserJoinedMessage, UserLeftMessage, - ItemEventMessage + ItemEventMessage, ViewportMessage } from "../Messages/generated/messages_pb"; import {UserMovesMessage} from "../Messages/generated/messages_pb"; import Direction = PositionMessage.Direction; @@ -264,15 +263,17 @@ export class IoSocketController { socket.on(SocketIoEvent.SET_VIEWPORT, (message: unknown): void => { try { - //console.log('SET_VIEWPORT') - if (!isViewport(message)) { - socket.emit(SocketIoEvent.MESSAGE_ERROR, {message: 'Invalid SET_VIEWPORT message.'}); - console.warn('Invalid SET_VIEWPORT message received: ', message); + if (!(message instanceof Buffer)) { + socket.emit(SocketIoEvent.MESSAGE_ERROR, {message: 'Invalid SET_VIEWPORT message. Expecting binary buffer.'}); + console.warn('Invalid SET_VIEWPORT message received (expecting binary buffer): ', message); return; } + const viewportMessage = ViewportMessage.deserializeBinary(new Uint8Array(message as ArrayBuffer)); + const viewport = viewportMessage.toObject(); + const Client = (socket as ExSocketInterface); - Client.viewport = message; + Client.viewport = viewport; const world = this.Worlds.get(Client.roomId); if (!world) { diff --git a/front/src/Connection.ts b/front/src/Connection.ts index 8201b5af..61b0c4e7 100644 --- a/front/src/Connection.ts +++ b/front/src/Connection.ts @@ -289,7 +289,13 @@ export class Connection implements Connection { } public setViewport(viewport: ViewportInterface): void { - this.socket.emit(EventMessage.SET_VIEWPORT, viewport); + const viewportMessage = new ViewportMessage(); + viewportMessage.setTop(Math.round(viewport.top)); + viewportMessage.setBottom(Math.round(viewport.bottom)); + viewportMessage.setLeft(Math.round(viewport.left)); + viewportMessage.setRight(Math.round(viewport.right)); + + this.socket.emit(EventMessage.SET_VIEWPORT, viewportMessage.serializeBinary().buffer); } public onUserJoins(callback: (message: MessageUserJoined) => void): void {