-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect.js
More file actions
134 lines (104 loc) · 3.68 KB
/
connect.js
File metadata and controls
134 lines (104 loc) · 3.68 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
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
const Ws = require('./check')
class WS {
constructor(...args) {
this.client = undefined
this.___args = args
this.___checkArgs = undefined
this.___events = {}
this.___state = 'disconnected' // [ 'disconnected', 'connecting', 'closing', 'connected']
this.methods()
}
set state(current) {
let dis = this
let previous = dis.___state
dis.___state = current
return this.emit('state', { previous: previous, current: current })
}
get state() {
return this.___state
}
___onOpen = () => {
this.state = 'connected'
}
___onClose = () => {
this.state = 'disconnected'
}
get binaryType() { if (this.client) return this.client.binaryType }
get bufferedamount() { if (this.client) return this.client.bufferedAmount }
get extensions() { if (this.client) return this.client.extensions }
get protocol() { if (this.client) return this.client.protocol }
get readyState() { if (this.client) return this.client.readyState }
get url() { return this.client.url }
connect = () => {
this.state = 'connecting'
this.client = new Ws(...this.___args)
for (let e in this.___events) {
for (let i = 0; i < this.___events[e].length; i++) {
this.client.on(e, this.___events[e][i])
}
}
if (this.___checkArgs) this.client.check(...this.___checkArgs)
this.client.prependListener('open', this.___onOpen)
this.client.prependListener('close', this.___onClose)
}
on = (event, handler) => {
if (!this.___events.hasOwnProperty(event)) this.___events[event] = []
this.___events[event].push(handler)
if (this.client) return this.client.on(event, handler)
}
emit = (event, ...args) => {
if (this.client) this.client.emit(event, ...args)
else {
for (let i in this.___events[event]) {
this.___events[event][i](...args)
}
}
}
prependListener = (event, handler) => {
if (!this.___events.hasOwnProperty(event)) this.___events[event] = []
this.___events[event].unshift(handler)
if (this.client) return this.client.prependListener(event, handler)
}
removeListener = (event, handler) => {
if (this.___events.hasOwnProperty(event)) {
remove(this.___events[event], handler)
if (this.client) this.client.removeListener(event, handler)
}
}
removeAllListeners = (event) => {
if (this.___events.hasOwnProperty(event)) {
delete this.___events[event]
if (this.client) this.client.removeAllListeners(event)
}
}
methods = () => {
let methods = ['ping', 'pong', 'send', 'close', 'terminate']
let dis = this
for (let method of methods) {
this[method] = (...args) => {
if (dis.client) return dis.client[method](...args)
else throw new Error('client does not exists yet')
}
}
}
check = (...args) => {
this.___checkArgs = args
if (this.client) {
if (this.client.checking) throw 'client is already checking connectivity'
else this.client.check(...args)
}
}
stopChecking = () => {
this.___checkArgs = undefined
if (this.client) {
if (!this.client.checking) throw 'client is not checking connectivity'
else this.client.stopChecking()
}
}
}
module.exports = WS
function remove(array, element) {
let index = array.indexOf(element)
if (index != -1) return array.splice(index, 1)
else return -1
}