Migrating user position messages to protobuf
This commit is contained in:
parent
e9ca8721a6
commit
df0636c513
10 changed files with 202 additions and 46 deletions
|
@ -10,7 +10,6 @@ import {Group} from "../Model/Group";
|
|||
import {User} from "../Model/User";
|
||||
import {isSetPlayerDetailsMessage,} from "../Model/Websocket/SetPlayerDetailsMessage";
|
||||
import {MessageUserJoined} from "../Model/Websocket/MessageUserJoined";
|
||||
import {MessageUserMoved} from "../Model/Websocket/MessageUserMoved";
|
||||
import si from "systeminformation";
|
||||
import {Gauge} from "prom-client";
|
||||
import {TokenInterface} from "../Controller/AuthenticateController";
|
||||
|
@ -23,9 +22,17 @@ import {uuid} from 'uuidv4';
|
|||
import {isViewport} from "../Model/Websocket/ViewportMessage";
|
||||
import {GroupUpdateInterface} from "_Model/Websocket/GroupUpdateInterface";
|
||||
import {Movable} from "../Model/Movable";
|
||||
import {PositionMessage, SetPlayerDetailsMessage} from "../../../messages/generated/messages_pb";
|
||||
import {
|
||||
PositionMessage,
|
||||
SetPlayerDetailsMessage,
|
||||
SubMessage,
|
||||
UserMovedMessage,
|
||||
BatchMessage
|
||||
} from "../../../messages/generated/messages_pb";
|
||||
import {UserMovesMessage} from "../../../messages/generated/messages_pb";
|
||||
import Direction = PositionMessage.Direction;
|
||||
import {ProtobufUtils} from "../Model/Websocket/ProtobufUtils";
|
||||
import toPositionMessage = ProtobufUtils.toPositionMessage;
|
||||
|
||||
enum SocketIoEvent {
|
||||
CONNECTION = "connection",
|
||||
|
@ -48,13 +55,13 @@ enum SocketIoEvent {
|
|||
BATCH = "batch",
|
||||
}
|
||||
|
||||
function emitInBatch(socket: ExSocketInterface, event: string | symbol, payload: unknown): void {
|
||||
socket.batchedMessages.push({ event, payload});
|
||||
function emitInBatch(socket: ExSocketInterface, event: string, payload: SubMessage): void {
|
||||
socket.batchedMessages.addPayload(payload);
|
||||
|
||||
if (socket.batchTimeout === null) {
|
||||
socket.batchTimeout = setTimeout(() => {
|
||||
socket.emit(SocketIoEvent.BATCH, socket.batchedMessages);
|
||||
socket.batchedMessages = [];
|
||||
socket.binary(true).emit(SocketIoEvent.BATCH, socket.batchedMessages.serializeBinary().buffer);
|
||||
socket.batchedMessages = new BatchMessage();
|
||||
socket.batchTimeout = null;
|
||||
}, 100);
|
||||
}
|
||||
|
@ -159,9 +166,9 @@ export class IoSocketController {
|
|||
ioConnection() {
|
||||
this.Io.on(SocketIoEvent.CONNECTION, (socket: Socket) => {
|
||||
const client : ExSocketInterface = socket as ExSocketInterface;
|
||||
client.batchedMessages = [];
|
||||
client.batchedMessages = new BatchMessage();
|
||||
client.batchTimeout = null;
|
||||
client.emitInBatch = (event: string | symbol, payload: unknown): void => {
|
||||
client.emitInBatch = (event: string, payload: SubMessage): void => {
|
||||
emitInBatch(client, event, payload);
|
||||
}
|
||||
this.sockets.set(client.userId, client);
|
||||
|
@ -538,7 +545,14 @@ export class IoSocketController {
|
|||
if (thing instanceof User) {
|
||||
const clientUser = this.searchClientByIdOrFail(thing.id);
|
||||
|
||||
clientListener.emitInBatch(SocketIoEvent.USER_MOVED, new MessageUserMoved(clientUser.userId, clientUser.position));
|
||||
const userMovedMessage = new UserMovedMessage();
|
||||
userMovedMessage.setUserid(clientUser.userId);
|
||||
userMovedMessage.setPosition(toPositionMessage(clientUser.position));
|
||||
|
||||
const subMessage = new SubMessage();
|
||||
subMessage.setUsermovedmessage(userMovedMessage);
|
||||
|
||||
clientListener.emitInBatch(SocketIoEvent.USER_MOVED, subMessage);
|
||||
//console.log("Sending USER_MOVED event");
|
||||
} else if (thing instanceof Group) {
|
||||
clientListener.emit(SocketIoEvent.GROUP_CREATE_UPDATE, {
|
||||
|
|
|
@ -3,6 +3,7 @@ import {PointInterface} from "./PointInterface";
|
|||
import {Identificable} from "./Identificable";
|
||||
import {TokenInterface} from "../../Controller/AuthenticateController";
|
||||
import {ViewportInterface} from "_Model/Websocket/ViewportMessage";
|
||||
import {BatchMessage, SubMessage} from "../../../../messages/generated/messages_pb";
|
||||
|
||||
export interface ExSocketInterface extends Socket, Identificable {
|
||||
token: string;
|
||||
|
@ -18,7 +19,7 @@ export interface ExSocketInterface extends Socket, Identificable {
|
|||
/**
|
||||
* Pushes an event that will be sent in the next batch of events
|
||||
*/
|
||||
emitInBatch: (event: string | symbol, payload: unknown) => void;
|
||||
batchedMessages: Array<{ event: string | symbol, payload: unknown }>;
|
||||
emitInBatch: (event: string, payload: SubMessage) => void;
|
||||
batchedMessages: BatchMessage;
|
||||
batchTimeout: NodeJS.Timeout|null;
|
||||
}
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
import {PointInterface} from "./PointInterface";
|
||||
|
||||
export class MessageUserMoved {
|
||||
constructor(public userId: number, public position: PointInterface) {
|
||||
}
|
||||
}
|
35
back/src/Model/Websocket/ProtobufUtils.ts
Normal file
35
back/src/Model/Websocket/ProtobufUtils.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import {PointInterface} from "./PointInterface";
|
||||
import {PositionMessage} from "../../../../messages/generated/messages_pb";
|
||||
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||
|
||||
export namespace ProtobufUtils {
|
||||
import Direction = PositionMessage.Direction;
|
||||
|
||||
export function toPositionMessage(point: PointInterface): PositionMessage {
|
||||
let direction: PositionMessage.DirectionMap[keyof PositionMessage.DirectionMap];
|
||||
switch (point.direction) {
|
||||
case 'up':
|
||||
direction = Direction.UP;
|
||||
break;
|
||||
case 'down':
|
||||
direction = Direction.DOWN;
|
||||
break;
|
||||
case 'left':
|
||||
direction = Direction.LEFT;
|
||||
break;
|
||||
case 'right':
|
||||
direction = Direction.RIGHT;
|
||||
break;
|
||||
default:
|
||||
throw new Error('unexpected direction');
|
||||
}
|
||||
|
||||
const position = new PositionMessage();
|
||||
position.setX(point.x);
|
||||
position.setY(point.y);
|
||||
position.setMoving(point.moving);
|
||||
position.setDirection(direction);
|
||||
|
||||
return position;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue