Add support for backlog
This commit is contained in:
parent
d0ecfde2b0
commit
4a1d6c6f1c
2 changed files with 18 additions and 4 deletions
|
@ -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 => {
|
||||
|
|
|
@ -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<RoomState> = {
|
|||
state.client = ws;
|
||||
state.in_room = true;
|
||||
state.room = ws.info;
|
||||
state.messages = ws.backlog;
|
||||
},
|
||||
messageReceived(state: RoomState, msg: RoomServerMessage) {
|
||||
state.messages.push(msg);
|
||||
|
|
Loading…
Reference in a new issue