Adding a Pusher container as a middleware/dispatcher between front and back

This commit is contained in:
David Négrier 2020-11-13 18:00:22 +01:00
parent 6c6d046891
commit 4c1e566a6c
86 changed files with 12172 additions and 983 deletions

View file

@ -2,9 +2,13 @@ import { Group } from "./Group";
import { PointInterface } from "./Websocket/PointInterface";
import {Zone} from "_Model/Zone";
import {Movable} from "_Model/Movable";
import {PositionInterface} from "_Model/PositionInterface";
import {PositionNotifier} from "_Model/PositionNotifier";
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
import {ServerDuplexStream, ServerWritableStream} from "grpc";
import {BatchMessage, PusherToBackMessage, ServerToClientMessage, SubMessage} from "../Messages/generated/messages_pb";
import {ProtobufUtils} from "_Model/Websocket/ProtobufUtils";
import {CharacterLayer, ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
export type UserSocket = ServerDuplexStream<PusherToBackMessage, ServerToClientMessage>;
export class User implements Movable {
public listenedZones: Set<Zone>;
@ -12,11 +16,14 @@ export class User implements Movable {
public constructor(
public id: number,
public uuid: string,
public readonly uuid: string,
private position: PointInterface,
public silent: boolean,
private positionNotifier: PositionNotifier,
public readonly socket: ExSocketInterface
public readonly socket: UserSocket,
public readonly tags: string[],
public readonly name: string,
public readonly characterLayers: CharacterLayer[]
) {
this.listenedZones = new Set<Zone>();
@ -32,4 +39,27 @@ export class User implements Movable {
this.position = position;
this.positionNotifier.updatePosition(this, position, oldPosition);
}
private batchedMessages: BatchMessage = new BatchMessage();
private batchTimeout: NodeJS.Timeout|null = null;
public emitInBatch(payload: SubMessage): void {
this.batchedMessages.addPayload(payload);
if (this.batchTimeout === null) {
this.batchTimeout = setTimeout(() => {
/*if (socket.disconnecting) {
return;
}*/
const serverToClientMessage = new ServerToClientMessage();
serverToClientMessage.setBatchmessage(this.batchedMessages);
this.socket.write(serverToClientMessage);
this.batchedMessages = new BatchMessage();
this.batchTimeout = null;
}, 100);
}
}
}