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
57 lines (54 loc) · 1.29 KB
/
Socket.js
File metadata and controls
57 lines (54 loc) · 1.29 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Proxy from './Proxy.worker.js'
class Socket {
constructor (game) {
this.game = game
this.worker = new Proxy()
this.handlers = new Map()
let connection, hostname, port
if (this.game.proxy === 'local') {
connection = document.location.protocol === 'https:' ? 'wss' : 'ws'
hostname = document.location.hostname
port = document.location.port
} else {
const pars = this.game.proxy.split(':')
connection = pars[0]
hostname = pars[1]
port = pars[2]
}
let server = this.game.server.split(':')
if (server.length === 1) {
server = [server[0], null]
}
this.emit('init', {
connection,
hostname,
port,
nick: this.game.nick,
server: server[0],
serverPort: server[1],
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 }