-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.browser.js
144 lines (117 loc) · 3.64 KB
/
client.browser.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
class WebSocketStream
{
constructor(options)
{
this.onConnectedActions = []
this.observers = {}
this.options =
{
protocol : 'protocol' in options ? options.protocol : 'ws',
host : 'host' in options ? options.host : '127.0.0.1',
port : 'port' in options ? options.port : 80,
silent : 'silent' in options ? options.debug : false,
keepalive : 'keepalive' in options ? options.debug : 25000,
reconnect : 'reconnect' in options ? options.reconnect : true,
onClose : 'onClose' in options ? options.onClose : false,
onConnect : 'onConnect' in options ? options.onConnect : false,
onReconnect : 'onReconnect' in options ? options.onReconnect : false
}
}
connect(reconnecting = false)
{
const url = this.options.protocol + '://' + this.options.host + ':' + this.options.port
this.websocket = new WebSocket(url)
this.log('websocket instantiated', url)
this.websocket.onopen = (event) =>
{
this.connected = true
this.log('websocket opened', event)
this.keepaliveInterval = setInterval(() => this.emit('ping'), this.options.keepalive)
this.log('websocket keepalive is set', Math.round(this.options.keepalive / 1e3) + 's')
reconnecting
? this.options.onReconnect && this.options.onReconnect(event)
: this.options.onConnect && this.options.onConnect(event)
for(const action of this.onConnectedActions)
{
action(event)
}
}
this.websocket.onerror = (event) =>
{
this.log('websocket error', event)
}
this.websocket.onclose = (event) =>
{
this.connected = false
this.log('websocket closed', event)
clearInterval(this.keepaliveInterval)
this.log('websocket keepalive interval stopped')
this.options.onClose && this.options.onClose()
this.options.reconnect && setTimeout(this.connect.bind(this), 100, true)
}
this.websocket.onmessage = (event) =>
{
this.log('websocket message recieved', event)
const dto = JSON.parse(event.data)
this.log('websocket parsed message data', dto)
this.trigger(dto.event, dto.data)
this.log('websocket triggered event', dto.event)
}
}
log(...args)
{
this.options.silent == false
&& window.console
&& window.console.log
&& window.console.log(Date.now(), ...args)
}
onConnectedAction(action)
{
this.onConnectedActions.push(action)
this.log('websocket added an "on connected action"')
this.connected && action()
}
emit(eventname, data)
{
const dto =
{
event : eventname,
data : data
}
this.websocket.send(JSON.stringify(dto))
this.log('websocket emitted message', dto)
}
on(eventname, observer)
{
if(this.observers[eventname] === undefined)
{
this.observers[eventname] = []
}
this.observers[eventname].push(observer)
this.log('websocket added observer', eventname)
}
removeObserver(eventname, observer)
{
this.log('websocket removing observer', eventname)
if(this.observers[eventname] === undefined)
{
return
}
const index = this.observers[eventname].indexOf(observer)
~index && this.observers[eventname].splice(index, 1)
}
trigger(eventname, data)
{
for(const observer of this.observers[eventname] || [])
{
observer(data)
}
}
close()
{
this.options.reconnect = false
this.log('websocket reconnect disabled')
this.log('websocket requested to close connection')
this.websocket.close()
}
}