-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.js
139 lines (116 loc) · 3.9 KB
/
ws.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
const { map, mapObjIndexed } = require('ramda')
const { Spinner } = require('./helpers')
const WS = require('ws')
const decode = JSON.parse
const encode = JSON.stringify
const spinner = Spinner()
const connect = (host, events) => {
spinner.start()
// console.log({connect: host})
let ws = new WS(host, { rejectUnauthorized: false })
ws.on('error', error => {
if (error.code == 'ECONNREFUSED') {
spinner.text = 'no connection'
spinner.color = 'red'
} else
console.error(error)
ws.onclose()
})
ws.connected = () => ws.readyState < 2
const emit = x => ws.send(encode(x))
// const pingInterval = setInterval(() => ws.readyState == WS.OPEN && emit({status: new Date()}), 1000)
ws.onclose = once(() => {
// clearInterval(pingInterval)
// clearInterval(updateTimeout.timeout)
// console.log(`disconnected ${host}`)
events.disconnect && events.disconnect(emit)
setTimeout(() => connect(host, events), 1000)
})
// const updateTimeout = time => {
// updateTimeout.timeout && clearTimeout(updateTimeout.timeout)
// return updateTimeout.timeout = setTimeout(ws.onclose, time)
// }
// // updateTimeout.timeout = updateTimeout(60000)
ws.on('open', function open() {
// console.log(`connected ${host}`)
spinner.stop()
events.connect && events.connect(emit)
// updateTimeout(10000)
})
ws.on('message', function message(data) {
// updateTimeout(10000)
data = decode(data)
// console.log(data)
for (const k of Object.keys(data)) {
if (!events[k]) console.error(`no handler for ${k}`)
events[k](emit, data[k])
}
})
return ws
}
const initWSServer = app => {
const expressWs = require("express-ws")(app)
for (const name of ['Sync', 'Server']) {
global[name] = {
sockets: [],
events: {
connect: [],
disconnect: []
},
on: (e, f) => {
const [event, ...options] = e.split(' ')
const silent = includes('silent', options)
const permissions = without('silent', options)
global[name].events[event] = concat(defaultTo([], global[name].events[event]), [
(ws, ...args) => {
// for (const p of permissions)
// if (!includes(p, dotPath('user.permissions', ws)))
// return console.log('no permission', p, dotPath('user.permissions', ws))
if (!silent) console.log({[event]: args})
return f(ws, ...args)
}
])
},
onMessage: ws => x => {
const message = decode(x)
for (const event of Object.keys(message)) {
if (!global[name].events[event])
return console.error(`No ws handler for ${event}`)
for (const f of global[name].events[event]) {
Promise.resolve(f(ws, message[event]))
.then(data => {
if (!isNil(data)) ws._emit(event, data)
})
.catch(error => {
ws._emit(`${event}:error`, error)
console.error(event, error)
})
}
}
},
broadcast: (event, data) => {
//TODO: check permissions!
// console.log(event, data, pluck('user', global[name].sockets))
global[name].sockets.map(socket => socket._emit(event, data))
}
}
app.ws('/'+name, function(ws, req) {
ws._emit = (event, data) => {
try {
ws.send(encode({[event]: data}))
}catch (e) {
if (/Unrecognized object/.test(String(e)))
console.log(data)
else
console.log(e, {[event]: data})
}
}
ws.on('message', global[name].onMessage(ws))
ws.on('close', () => global[name].events.disconnect.map(x => x(ws)))
global[name].sockets.push(ws)
global[name].events.connect.map(x => x(ws))
});
global[name].on('browser:error', (_, x) => console.error(x))
}
}
module.exports = {connect, initWSServer}