-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
227 lines (189 loc) · 6 KB
/
client.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const { Bitmap } = require('./bitmap.js');
const WebSocket = require('ws')
let PROTOCOL_VERSION = 1
let CHUNK_SIZE = 64 * 64 * 64
let CHUNK_SIZE_BYTES = CHUNK_SIZE / 8
let CHUNK_COUNT = 64 * 64
let BITMAP_SIZE = CHUNK_SIZE * CHUNK_COUNT
let UPDATE_CHUNK_SIZE = 32
exports.PROTOCOL_VERSION = PROTOCOL_VERSION
exports.CHUNK_SIZE = CHUNK_SIZE
exports.CHUNK_SIZE_BYTES = CHUNK_SIZE_BYTES
exports.CHUNK_COUNT = CHUNK_COUNT
exports.BITMAP_SIZE = CHUNK_SIZE * CHUNK_COUNT
exports.UPDATE_CHUNK_SIZE = UPDATE_CHUNK_SIZE
let MessageType;
exports.MessageType = MessageType;
;(function(MessageType) {
MessageType[(MessageType["Hello"] = 0)] = "Hello"
MessageType[(MessageType["Stats"] = 1)] = "Stats"
MessageType[(MessageType["ChunkFullStateRequest"] = 16)] =
"ChunkFullStateRequest"
MessageType[(MessageType["ChunkFullStateResponse"] = 17)] =
"ChunkFullStateResponse"
MessageType[(MessageType["PartialStateUpdate"] = 18)] = "PartialStateUpdate"
MessageType[(MessageType["ToggleBit"] = 19)] = "ToggleBit"
MessageType[(MessageType["PartialStateSubscription"] = 20)] =
"PartialStateSubscription"
})(MessageType || (MessageType = {}))
exports.BitmapClient = class BitmapClient {
goToCheckboxCallback = () => {}
loadingCallback = () => {}
highlightedIndex = -1
websocketOpen = false
websocket = null
currentChunkIndex = 0
chunkLoaded = false
constructor() {
this.bitmap = new Bitmap(CHUNK_SIZE)
this.openWebSocket()
}
isChecked(globalIndex) {
const localIndex = globalIndex % CHUNK_SIZE
return this.bitmap.get(localIndex)
}
toggle(globalIndex) {
const localIndex = globalIndex % CHUNK_SIZE
// console.log("Toggling", globalIndex);
this.send({ msg: MessageType.ToggleBit, index: globalIndex })
this.bitmap.set(localIndex, !this.bitmap.get(localIndex))
}
get chunkIndex() {
return this.currentChunkIndex
}
setChunkIndex(chunkIndex) {
this.currentChunkIndex = chunkIndex
this.chunkLoaded = false
this.loadingCallback(true)
this.send({ msg: MessageType.PartialStateSubscription, chunkIndex })
this.send({ msg: MessageType.ChunkFullStateRequest, chunkIndex })
}
getUint8Array() {
return this.bitmap.bytes
}
openWebSocket() {
console.log("Connecting to server")
if (this.websocket) {
this.websocketOpen = false
this.websocket.close()
}
const ws = new WebSocket("wss://bitmap-ws.alula.me/")
ws.binaryType = "arraybuffer"
this.websocket = ws
ws.addEventListener("open", () => {
this.websocketOpen = true
console.log("Connected to server")
this.onOpen()
})
ws.addEventListener("message", message => {
if (message.data instanceof ArrayBuffer) {
const msg = this.deserialize(message.data)
if (msg) this.onMessage(msg)
}
})
ws.addEventListener("close", () => {
console.log("Disconnected from server")
this.websocketOpen = false
this.websocket = null
setTimeout(() => this.openWebSocket(), 5000)
})
ws.addEventListener("error", err => {
this.websocketOpen = false
console.error(err)
})
}
onOpen() {}
onMessage(msg) {
// console.log("Received message", msg);
if (msg.msg === MessageType.Hello) {
if (msg.versionMajor !== PROTOCOL_VERSION) {
this.websocket?.close()
alert("Incompatible protocol version")
}
const chunkIndex = this.chunkIndex
this.send({ msg: MessageType.PartialStateSubscription, chunkIndex })
this.send({ msg: MessageType.ChunkFullStateRequest, chunkIndex })
} else if (msg.msg === MessageType.ChunkFullStateResponse) {
const fullState = msg
if (fullState.chunkIndex !== this.chunkIndex) return
this.bitmap.fullStateUpdate(fullState.bitmap)
this.chunkLoaded = true
this.loadingCallback(false)
} else if (msg.msg === MessageType.PartialStateUpdate) {
const partialState = msg
// console.log("Partial state update", partialState);
const chunkIndex = Math.floor(partialState.offset / CHUNK_SIZE_BYTES)
if (chunkIndex !== this.chunkIndex) return
const byteOffset = partialState.offset % CHUNK_SIZE_BYTES
this.bitmap.partialStateUpdate(byteOffset, partialState.chunk)
}
}
deserialize(data) {
const payload = new Uint8Array(data)
const dataView = new DataView(data)
const msg = payload[0]
if (msg === MessageType.Hello) {
const versionMajor = dataView.getUint16(1, true)
const versionMinor = dataView.getUint16(3, true)
return {
msg,
versionMajor,
versionMinor
}
} else if (msg === MessageType.Stats) {
const currentClients = dataView.getUint32(1, true)
return {
msg,
currentClients
}
} else if (msg === MessageType.ChunkFullStateResponse) {
const chunkIndex = dataView.getUint16(1, true)
const bitmap = payload.slice(3)
return {
msg,
chunkIndex,
bitmap
}
} else if (msg === MessageType.PartialStateUpdate) {
const offset = dataView.getUint32(1, true)
const chunk = payload.slice(5)
return {
msg,
offset,
chunk
}
} else {
return undefined
}
}
send(msg) {
if (!this.websocket) return
const data = this.serialize(msg)
try {
this.websocket.send(data)
} catch (err) {
if (err.toString().includes("readyState 0")) return
console.log(err)
}
}
serialize(msg) {
if (
msg.msg === MessageType.ChunkFullStateRequest ||
msg.msg === MessageType.PartialStateSubscription
) {
const data = new Uint8Array(3)
data[0] = msg.msg
const view = new DataView(data.buffer)
view.setUint16(1, msg.chunkIndex, true)
return data
} else if (msg.msg === MessageType.ToggleBit) {
const data = new Uint8Array(5)
data[0] = msg.msg
const view = new DataView(data.buffer)
view.setUint32(1, msg.index, true)
return data
} else {
throw new Error("Invalid message type")
}
}
}