forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocket.js
More file actions
41 lines (38 loc) · 893 Bytes
/
Socket.js
File metadata and controls
41 lines (38 loc) · 893 Bytes
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
import Proxy from './Proxy.worker.js'
class Socket {
constructor (game) {
this.game = game
this.worker = new Proxy()
this.handlers = new Map()
this.emit('init', {
hostname: document.location.hostname,
port: document.location.port,
nick: this.game.nick,
server: this.game.server,
serverPort: this.game.serverPort,
password: this.game.password
})
this.worker.onmessage = (msg) => {
const type = msg.data.type
const data = msg.data.params
const handler = this.handlers.get(type)
if (handler !== undefined) {
if (data === undefined) {
handler()
} else {
handler(...data)
}
}
}
}
emit (type, ...data) {
this.worker.postMessage({
type,
data
})
}
on (type, handler) {
this.handlers.set(type, handler)
}
}
export { Socket }