-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.ts
90 lines (71 loc) · 2.09 KB
/
client.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Board, Dimension, Piece } from './ttt';
const ws_url = 'ws://app.r26.me:8869';
export type ClientBoardUpdate = (ClientBoard) => void;
export class ClientBoard extends Board {
public url: string;
public side: Piece = null;
private ws: WebSocket = null;
private update: ClientBoardUpdate;
private should_reconnect = true;
constructor(dimension: Dimension, code: string, update?: ClientBoardUpdate) {
super(dimension);
this.url = ws_url + '/' + code;
this.update = update;
this.connect();
}
connect() {
if(this.ws != null) {
this.ws.close();
this.ws = null;
}
this.should_reconnect = true;
this.ws = new WebSocket(this.url);
this.ws.addEventListener('message', (e: MessageEvent) => this.onmessage(e));
this.ws.addEventListener('close', () => this.reconnect());
}
reconnect() {
if(this.should_reconnect) {
this.ws?.close();
this.ws = null;;
setTimeout(() => this.connect(), 1000);
}
}
disconnect() {
this.should_reconnect = false;
this.ws.close();
}
connected() : boolean {
return this.ws?.readyState == WebSocket.OPEN;
}
onmessage(e: MessageEvent) {
const data = JSON.parse(e.data);
switch(data[0]) {
case 'first':
this.reset();
break;
case 'side':
this.side = data[1];
break;
case 'board':
this.deserialize(data[1]);
break;
}
this.check_winners();
if(this.update != null) {
this.update(this)
}
}
send(...args: any[]) {
if(this.connected())
this.ws.send(JSON.stringify(args));
}
reset(dimension: Dimension = this.dimension ?? Dimension.FOUR) {
if(this.ws != null)
this.send('reset', dimension);
else
super.reset(dimension);
}
put_piece() {
this.send('put_piece', this.select);
}
}