-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
89 lines (83 loc) · 2.61 KB
/
app.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
/*
* liuzy <mostic1122@gmail.com>
* MIT Licensed
*/
import fs from 'fs'
import websocket from 'ws'
import { channels } from './channels'
import { logs } from './log'
!function WS() {
let ws = new websocket('wss://real.okcoin.cn:10440/websocket/okcoinapi')
const channelsArr = Object.keys(channels)
const saveToFile = (channel, data) => {
try {
// if you want to save data to a file
/*fs.appendFile(__dirname + '/data/' + channel + '.log', JSON.stringify(data) + '\n', (err) => {
if (err) throw new Error(err)
})*/
logs[channel].trace(JSON.stringify(data))
} catch (err) { console.log(err) }
}
ws.on('open', () => {
console.log('socket has connected')
// ltc
ws.send(channels.ok_sub_spotcny_ltc_ticker)
ws.send(channels.ok_sub_spot_ltc_depth)
ws.send(channels.ok_sub_spotcny_ltc_depth_20)
ws.send(channels.ok_sub_spotcny_ltc_trades)
ws.send(channels.ok_sub_spotcny_ltc_kline_1min)
// btc
ws.send(channels.ok_sub_spotcny_btc_ticker)
ws.send(channels.ok_sub_spot_btc_depth)
ws.send(channels.ok_sub_spotcny_btc_depth_20)
ws.send(channels.ok_sub_spotcny_btc_trades)
ws.send(channels.ok_sub_spotcny_btc_kline_1min)
})
ws.on('message', (data) => {
//console.log(data)
let res = JSON.parse(data)
res = res[0] || {}
if (!res) {
console.log('no data received')
return;
}
switch (res.channel) {
// ltc
case 'ok_sub_spotcny_ltc_ticker':
saveToFile('ok_sub_spotcny_ltc_ticker', res)
break;
case 'ok_sub_spot_ltc_depth':
saveToFile('ok_sub_spot_ltc_depth', res)
break;
case 'ok_sub_spotcny_ltc_depth_20':
saveToFile('ok_sub_spotcny_ltc_depth_20', res)
break;
case 'ok_sub_spotcny_ltc_trades':
saveToFile('ok_sub_spotcny_ltc_trades', res)
break;
case 'ok_sub_spotcny_ltc_kline_1min':
saveToFile('ok_sub_spotcny_ltc_kline_1min', res)
break;
// btc
case 'ok_sub_spotcny_btc_ticker':
saveToFile('ok_sub_spotcny_btc_ticker', res)
break;
case 'ok_sub_spot_btc_depth':
saveToFile('ok_sub_spot_btc_depth', res)
break;
case 'ok_sub_spotcny_btc_depth_20':
saveToFile('ok_sub_spotcny_btc_depth_20', res)
break;
case 'ok_sub_spotcny_btc_trades':
saveToFile('ok_sub_spotcny_btc_trades', res)
break;
case 'ok_sub_spotcny_btc_kline_1min':
saveToFile('ok_sub_spotcny_btc_kline_1min', res)
break;
}
})
ws.on('close', (code, reason) => {
console.log(`connection closed by ${code}, and ${reason}`)
WS()
})
}()