-
Notifications
You must be signed in to change notification settings - Fork 1
/
SocketRelay.js
62 lines (49 loc) · 1.68 KB
/
SocketRelay.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
const WebSocket = require('ws')
const EventEmitter = require('events')
const pipe = (...fs) => (x) => fs.reduce((a, f) => f(a), x)
const trace = (x) => console.log(x) || x
module.exports = class SocketRelay {
constructor () {
this.emitter = new EventEmitter()
}
init (app) {
app.ws('/ws', (conn) => this.handleConnection(conn))
}
handleConnection (conn) {
conn.on('message', pipe(trace, JSON.parse, this.handleMessage.bind(this, conn)))
}
handleMessage (conn, action) {
console.log('Received action:', action)
if (action.type === 'register') {
this.emitter.on(action.issueID + '.steps-receive', (action) => {
this.send(conn, { type: 'steps-reply', ...action.payload })
})
this.emitter.on(action.issueID + '.cursor-receive', (action) => {
this.send(conn, { type: 'cursor-reply', ...action.payload })
})
this.emitter.on(action.issueID + '.disconnected', (payload) => {
this.send(conn, { type: 'cursor-disconnected', ...payload })
})
conn.on('close', () => {
this.emitter.emit(action.issueID + '.disconnected', { [action.cursorID]: {} })
})
}
if (action.type === 'steps-receive') {
this.emitter.emit(action.issueID + '.steps-receive', action)
}
if (action.type === 'cursor-receive') {
this.emitter.emit(action.issueID + '.cursor-receive', action)
}
}
send (conn, data) {
this.sendRaw(conn, JSON.stringify(data))
}
sendRaw (conn, msg) {
if (conn.readyState === WebSocket.OPEN) {
console.log('=>', msg)
conn.send(msg)
} else if (conn.readyState === WebSocket.OPENING) {
conn.on('open', () => this.sendRaw(conn, msg))
}
}
}