-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
62 lines (56 loc) · 1.97 KB
/
server.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 express = require('express');
const cors = require('cors');
const request = require('request');
const WebSocket = require('ws');
// let ws = new WebSocket('ws://192.168.16.49:8080/infoCenter/btc', [], {
// perMessageDeflate: true
// });
// ws.on('open', function() {
// ws.send('["market:add","btctrade:btc"]');
// ws.on('message', function(data) {
// console.log(data);
// });
// });
const app = express();
app.use(cors({
origin: ['http://localhost:8080', 'http://192.168.16.160:8080'],
methods: ['GET', 'PUT', 'POST', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization', 'token'],
credentials: true,
}));
app.post('/user', function(req, res) {
res.json({ success: true });
});
app.get('/data', function(req, res) {
request({
url: 'https://k.sosobtc.com/data/period?symbol=okcoinbtccny&step=60',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36',
'Content-Type': 'application/json',
},
}, function (erro, response, body) {
res.json(JSON.parse(body));
});
});
const io = require('socket.io').listen(app.listen(3000));
let wurl = 'wss://io.sosobtc.com/socket.io/?EIO=3&transport=websocket';
let ws = new WebSocket(wurl, [], {
perMessageDeflate: true
});
ws.on('open', function() {
ws.send('420["market.subscribe","btc:okcoin"]');
setInterval(function() {
ws.send('3');
}, 3600);
ws.on('message', function(data, flags) {
if (data.indexOf('update:trades') > -1) {
console.log(JSON.parse(data.slice(2, data.length))[1][0]);
io.emit('update', JSON.parse(data.slice(2, data.length))[1][0]);
}
if (data.indexOf('update:depth') > -1) {
console.log(JSON.parse(data.slice(2, data.length))[1]);
io.emit('depth', JSON.parse(data.slice(2, data.length))[1]);
}
});
});