-
Notifications
You must be signed in to change notification settings - Fork 17
/
P2PClient.js
42 lines (36 loc) · 1.02 KB
/
P2PClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { DepictItClient } from "../game/DepictIt.js";
export class P2PClient {
constructor(identity, uniqueId, ably) {
this.identity = identity;
this.uniqueId = uniqueId;
this.ably = ably;
this.depictIt = null;
this.serverState = null;
this.state = {
status: "disconnected",
instructionHistory: [],
lastInstruction: null
};
}
async connect() {
await this.ably.connect(this.identity, this.uniqueId);
this.ably.sendMessage({ kind: "connected" });
this.state.status = "awaiting-acknowledgement";
this.depictIt = new DepictItClient(this.uniqueId, this.ably);
}
onReceiveMessage(message) {
if (message.serverState) {
this.serverState = message.serverState;
}
switch (message.kind) {
case "connection-acknowledged":
this.state.status = "acknowledged";
break;
case "instruction":
this.state.instructionHistory.push(message);
this.state.lastInstruction = message;
break;
default: { };
}
}
}