From 4a1d6c6f1c91ff80313f29ef94aed6ecdbca9bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=AEittaG=20ordnasselA?= Date: Tue, 11 Jun 2019 11:58:16 +0200 Subject: [PATCH] Add support for backlog --- webclient/src/roomclient.ts | 16 +++++++++++++--- webclient/src/store/room.ts | 6 +++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/webclient/src/roomclient.ts b/webclient/src/roomclient.ts index 60cdc88..910659e 100644 --- a/webclient/src/roomclient.ts +++ b/webclient/src/roomclient.ts @@ -10,6 +10,7 @@ export type MessageHandler = (msg: RoomServerMessage) => void; export default class RoomClient { private ws: WebSocket; public info: Room; + public backlog: RoomServerMessage[]; private onMessage?: MessageHandler; private buffer: RoomServerMessage[]; @@ -17,6 +18,7 @@ export default class RoomClient { constructor(_ws: WebSocket, _info: Room) { this.info = _info; this.buffer = []; + this.backlog = []; this.ws = _ws; this.ws.addEventListener("message", this._received.bind(this)); } @@ -48,12 +50,20 @@ export default class RoomClient { // Unregister handler ws.removeEventListener("message", onMessage); // Parse message - let data = JSON.parse(ev.data); + let data = JSON.parse(ev.data) as RoomServerMessage; if ("error" in data) { reject(data.error); } - let client = new RoomClient(ws, data.event.room); - resolve(client); + if (data.room) { + let client = new RoomClient(ws, data.room); + // Check for backlog + if (data.backlog) { + client.backlog = data.backlog; + } + resolve(client); + } else { + reject("missing room info"); + } }; ws.addEventListener("message", onMessage); ws.addEventListener("open", ev => { diff --git a/webclient/src/store/room.ts b/webclient/src/store/room.ts index a8236a7..f3d242e 100644 --- a/webclient/src/store/room.ts +++ b/webclient/src/store/room.ts @@ -36,9 +36,12 @@ export interface RoomMessage { export interface RoomServerMessage { time: "string"; - type: "event" | "message"; + type: "event" | "message" | "welcome"; event?: RoomEvent; message?: RoomMessage; + error?: string; + backlog?: RoomServerMessage[]; + room?: Room; } export interface RoomState { @@ -60,6 +63,7 @@ const mutations: MutationTree = { state.client = ws; state.in_room = true; state.room = ws.info; + state.messages = ws.backlog; }, messageReceived(state: RoomState, msg: RoomServerMessage) { state.messages.push(msg);