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
43 lines (40 loc) · 1.04 KB
/
Socket.js
File metadata and controls
43 lines (40 loc) · 1.04 KB
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
import Proxy from './Proxy.worker.js'
class Socket {
constructor (game) {
this.game = game
this.worker = new Proxy()
this.handlers = new Map()
const hostname = this.game.proxy.hostname === 'local' ? document.location.hostname : this.game.proxy.hostname
const port = this.game.proxy.port === 'local' ? document.location.port : this.game.proxy.port
this.emit('init', {
hostname: hostname,
port: 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 }