-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClient.ts
154 lines (132 loc) · 4.57 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as WebSocket from 'ws';
import Log from './Log';
import JsonProxy, { ComposedSerialSnapshot, SerialSnapshot, WhiteList } from './shared/JsonProxy';
import { BackofficeStatus } from './shared/BackOfficeStatus';
import ClientRequest from './ClientRequest';
const logger = Log.logger(__filename);
const clients: {[id:string]:Client} = {};
const pingDelay = 60000;
export default class Client {
public readonly uid: string;
private pendingWrites = 0;
private writes = 0;
private pendingDiffs = 0;
readonly socket: WebSocket;
private disposed: boolean;
private requests: ClientRequest[];
private jsonProxy: JsonProxy<BackofficeStatus>;
private jsonSerial: ComposedSerialSnapshot;
private jsonListenerId: string;
private sendingTimer: NodeJS.Timeout|undefined;
private whiteList: WhiteList;
private pingTo: undefined|NodeJS.Timeout;
constructor(socket:WebSocket, jsonProxy: JsonProxy<BackofficeStatus>, serverId: string, clientUid: string, whiteList: WhiteList)
{
this.uid = clientUid;
clients[this.uid] = this;
logger.info('Client connected', {...this.logContext(), whiteList});
this.whiteList = whiteList;
this.socket = socket;
this.disposed = false;
this.requests = [];
this.jsonListener = this.jsonListener.bind(this);
this.jsonProxy = jsonProxy;
const initialState = this.jsonProxy.fork(whiteList);
this.jsonSerial = initialState.serial;
this.sendingTimer = undefined;
this.notify({type: 'welcome', status: "ok", serverId: serverId, clientId: this.uid, data: initialState.data});
this.jsonListenerId = this.jsonProxy.addListener(this.jsonListener);
}
private logContext(): object {
return {uid: this.uid}
}
public attachRequest(c: ClientRequest) {
if (!this.disposed) {
this.requests.push(c);
}
}
private sendDiff=()=>{
if (this.sendingTimer !== undefined) {
clearTimeout(this.sendingTimer);
this.sendingTimer = undefined;
}
this.pendingDiffs = 0;
var patch = this.jsonProxy.diff(this.jsonSerial, this.whiteList);
if (patch !== undefined) {
this.notify({type: 'update', status: "ok", diff: patch});
}
}
private jsonListener=()=>{
this.pendingDiffs++;
if (this.sendingTimer === undefined) {
this.sendingTimer = setTimeout(()=> {
this.sendingTimer = undefined;
this.sendDiff();
}, 40);
}
}
public dispose=()=>{
if (!this.disposed) {
this.disposed = true;
logger.info('Closed notification channel', {uid: this.uid});
if (this.socket != undefined) {
try {
this.socket.close();
} catch(e) {
logger.error('Failed to close', {uid: this.uid}, e);
}
}
this.jsonProxy.removeListener(this.jsonListenerId);
delete clients[this.uid];
while(this.requests.length) {
const r = this.requests[0];
this.requests.splice(0, 1);
r.dettach();
}
}
}
public notify=(changeEvent:any)=>{
this.write(changeEvent);
}
private ping=()=>{
logger.info('pinging client', {uid: this.uid});
this.write({});
}
private restartPing = ()=> {
if (this.pingTo !== undefined) {
clearTimeout(this.pingTo);
this.pingTo = undefined;
}
if (!this.disposed) {
this.pingTo = setTimeout(this.ping, pingDelay * (0.75 + Math.random() / 2));
}
}
private write=(event:any)=>{
try {
if (this.disposed) {
return;
}
this.socket.send(JSON.stringify(event), (error)=> {
if (error !== undefined) {
logger.warn('Failed to send', this.logContext(), error);
this.dispose();
}
this.writes--;
this.restartPing();
});
} catch(e) {
logger.warn('Failed to send', this.logContext(), e);
this.dispose();
return;
}
this.writes++;
}
public reply=(data:any)=>{
// Ensure client view is up to date
this.sendDiff();
if (!this.disposed) {
logger.debug('Reply message', {...this.logContext(), data});
this.write(data);
}
}
}