-
Notifications
You must be signed in to change notification settings - Fork 11
/
wsOperation.js
130 lines (108 loc) · 2.63 KB
/
wsOperation.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
const utils = require('./utils');
const config = require('./config');
const defaultTime = new Date().getTime();
let Timer = null;
const dealMessage = (message, ws) => {
let data = JSON.parse(message);
let { type, period, from, to, baseCurrencyId, targetCurrencyId } = data;
console.log('data ->', data);
// 控制每次添加的时间数
// console.log('step ->', step);
if (type === "kline") {
let step = utils.transformTime(period) / config.constant.SPACE_TIME;
console.log('kline......')
const params = {
step,
length: config.constant.HISTORY_DATA_LENGTH,
type: "front",
startTime: from,
endTime: to
}
let result = utils.setRandomData(params);
// console.log('from->', from);
// console.log('to->', to);
let returnData = {}
let compareTime = defaultTime - 1 * step
let compareDay = new Date(compareTime);
// console.log('compareDay->', compareDay);
console.log('period->', period);
console.log('step->', step);
if (compareDay >= from) {
returnData = {
"code": 0,
"type": "kline",
"data": {
kLine: {
...result
}
}
}
} else {
returnData = {
"code": 0,
"type": "kline",
"data": {
kLine: {
t: [],
c: [],
o: [],
h: [],
l: [],
v: [],
s: "ok"
}
}
}
}
// console.log('send kline');
// 发送历史数据
console.log('发送K线历史数据...');
ws.send(JSON.stringify(returnData));
if (Timer) {
clearInterval(Timer);
}
console.log('建立定时器...');
Timer = startRealtime(ws, params);
}
}
// 定时发送实时数据
const startRealtime = (ws, params) => {
// console.log('startRealtime ->');
let { step, length, type, startTime, endTime } = params;
let increasingNum = 0;
let data = {}
return setInterval(() => {
increasingNum = ++increasingNum;
let returnData = {}
const _params = {
step,
length: 1,
type: "back",
increasingNum,
startTime,
endTime
}
result = utils.setRandomData(_params);
console.log('result ->', result);
let { t, c, o, h, l, v } = result;
returnData = {
"code": 0,
"type": "dealSuccess",
"data": {
kLine: {
t: t[0],
c: c[0],
o: o[0],
h: h[0],
l: l[0],
v: v[0],
s: "ok"
}
}
}
ws.send(JSON.stringify(returnData));
}, config.constant.INTERVAL_TIME)
}
module.exports = {
dealMessage,
}