/
mandelbrot-ws-hive.js
55 lines (43 loc) · 1.05 KB
/
mandelbrot-ws-hive.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
/* eslint-env mocha */
'use strict'
const assert = require('assert')
const Ws = require('../lib/ws-connect')
const Wock = require('./ws-testhelper.js')
describe('websockets', () => {
it('connects and disconnects, sends messages', (done) => {
// tests basic functions:
// 1. connect to server
// 2. send message
// 3. receive response
// 4. close connection
const wss = new Wock({
port: 9999
})
wss.messageHook = (ws, msg) => {
assert.strictEqual(msg.event, 'subscribe')
wss.send(ws, { ok: true })
}
wss.closeHook = (ws) => {
wss.close()
}
const conf = { url: 'ws://localhost:9999' }
const ws = new Ws(conf)
ws.on('open', () => {
const msg = {
event: 'subscribe',
channel: 'book',
symbol: 'BTCUSD'
}
ws.send(msg)
})
ws.on('close', () => {
assert.strictEqual(ws.connected, false)
done()
})
ws.on('message', (msg) => {
assert.deepStrictEqual(msg, { ok: true })
ws.close()
})
ws.open()
})
})