-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
218 lines (193 loc) · 5.81 KB
/
Copy pathserver.js
File metadata and controls
218 lines (193 loc) · 5.81 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const express = require('express');
const WebSocket = require('ws');
const http = require('http');
const cors = require('cors');
const path = require('path');
const moment = require('moment');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
// 生成K线图历史数据
function generateKLineData(basePrice, days = 60) {
const data = [];
let currentPrice = basePrice;
for (let i = days; i >= 0; i--) {
const date = moment().subtract(i, 'days').format('YYYY-MM-DD');
const open = currentPrice;
const change = (Math.random() - 0.5) * basePrice * 0.05;
const close = open + change;
const high = Math.max(open, close) + Math.random() * Math.abs(change) * 0.5;
const low = Math.min(open, close) - Math.random() * Math.abs(change) * 0.5;
const volume = Math.floor(Math.random() * 10000000) + 1000000;
data.push({
date,
open: Number(open.toFixed(2)),
high: Number(high.toFixed(2)),
low: Number(low.toFixed(2)),
close: Number(close.toFixed(2)),
volume
});
currentPrice = close;
}
return data;
}
// 模拟股票数据
const stockData = {
'AAPL': {
name: '苹果',
price: 182.52,
change: 2.15,
changePercent: 1.19,
klineData: generateKLineData(180.37)
},
'GOOGL': {
name: '谷歌',
price: 141.80,
change: -0.92,
changePercent: -0.64,
klineData: generateKLineData(142.72)
},
'MSFT': {
name: '微软',
price: 378.91,
change: 5.23,
changePercent: 1.40,
klineData: generateKLineData(373.68)
},
'TSLA': {
name: '特斯拉',
price: 242.84,
change: -3.67,
changePercent: -1.49,
klineData: generateKLineData(246.51)
},
'AMZN': {
name: '亚马逊',
price: 155.33,
change: 1.85,
changePercent: 1.21,
klineData: generateKLineData(153.48)
},
'BABA': {
name: '阿里巴巴',
price: 87.23,
change: -1.45,
changePercent: -1.63,
klineData: generateKLineData(88.68)
},
'TCEHY': {
name: '腾讯',
price: 48.92,
change: 0.87,
changePercent: 1.81,
klineData: generateKLineData(48.05)
},
'JD': {
name: '京东',
price: 35.67,
change: -0.23,
changePercent: -0.64,
klineData: generateKLineData(35.90)
}
};
// 随机更新股票价格
function updateStockPrices() {
Object.keys(stockData).forEach(symbol => {
const stock = stockData[symbol];
const changeAmount = (Math.random() - 0.5) * 2;
const newPrice = stock.price + changeAmount;
const change = newPrice - (stock.price - stock.change);
const changePercent = (change / (stock.price - stock.change)) * 100;
// 更新当前价格
stock.price = newPrice;
stock.change = change;
stock.changePercent = changePercent;
// 更新K线数据最后一根K线
const lastCandle = stock.klineData[stock.klineData.length - 1];
const today = moment().format('YYYY-MM-DD');
if (lastCandle.date === today) {
// 更新今天的K线数据
lastCandle.close = Number(newPrice.toFixed(2));
lastCandle.high = Math.max(lastCandle.high, lastCandle.close);
lastCandle.low = Math.min(lastCandle.low, lastCandle.close);
lastCandle.volume += Math.floor(Math.random() * 100000);
} else {
// 创建新的K线数据
const newCandle = {
date: today,
open: stock.price - change,
high: Number((newPrice + Math.random() * 2).toFixed(2)),
low: Number((newPrice - Math.random() * 2).toFixed(2)),
close: Number(newPrice.toFixed(2)),
volume: Math.floor(Math.random() * 10000000) + 1000000
};
stock.klineData.push(newCandle);
// 保持最近60天的数据
if (stock.klineData.length > 61) {
stock.klineData.shift();
}
}
});
}
// WebSocket连接处理
wss.on('connection', (ws) => {
console.log('新的WebSocket连接');
// 发送初始数据
ws.send(JSON.stringify({ type: 'stocks', data: stockData }));
// 定期发送更新数据
const interval = setInterval(() => {
updateStockPrices();
ws.send(JSON.stringify({ type: 'stocks', data: stockData }));
}, 2000);
ws.on('close', () => {
console.log('WebSocket连接关闭');
clearInterval(interval);
});
});
// API路由
app.get('/api/stocks', (req, res) => {
res.json(stockData);
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Twitter风格界面路由
app.get('/twitter', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'twitter-style.html'));
});
// 移动端Twitter风格界面
app.get('/mobile', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'mobile-twitter.html'));
});
// 社交化API端点
app.get('/api/social-stats', (req, res) => {
const socialStats = {};
Object.keys(stockData).forEach(symbol => {
socialStats[symbol] = {
likes: Math.floor(Math.random() * 1000 + 100),
retweets: Math.floor(Math.random() * 500 + 50),
comments: Math.floor(Math.random() * 200 + 20),
mentions: Math.floor(Math.random() * 100 + 10)
};
});
res.json(socialStats);
});
// 热门话题API
app.get('/api/trending', (req, res) => {
const trending = Object.keys(stockData)
.sort((a, b) => Math.abs(stockData[b].changePercent) - Math.abs(stockData[a].changePercent))
.slice(0, 10)
.map(symbol => ({
symbol,
name: stockData[symbol].name,
changePercent: stockData[symbol].changePercent,
volume: stockData[symbol].klineData[stockData[symbol].klineData.length - 1].volume
}));
res.json(trending);
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`股票行情服务器运行在 http://localhost:${PORT}`);
});