forked from BETAIL-BOYS/TradeFlow-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
106 lines (88 loc) · 2.74 KB
/
server.js
File metadata and controls
106 lines (88 loc) · 2.74 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
const WebSocket = require('ws');
// Create WebSocket server on port 3001 (IPv4 only)
const wss = new WebSocket.Server({
port: 3001,
host: '127.0.0.1'
});
console.log('WebSocket server started on ws://127.0.0.1:3001');
// Store subscriptions for each symbol
const subscriptions = new Map();
// Generate mock trade data
function generateTradeData(symbol) {
const now = Date.now();
const basePrice = 0.15 + Math.random() * 0.02; // Base price around 0.15-0.17
const volatility = 0.001;
return {
type: "trade",
symbol: symbol,
payload: {
timestamp: now,
open: basePrice,
high: basePrice + (Math.random() * volatility),
low: basePrice - (Math.random() * volatility),
close: basePrice + ((Math.random() - 0.5) * volatility),
volume: Math.floor(500 + Math.random() * 2000),
previousClose: basePrice - ((Math.random() - 0.5) * volatility)
}
};
}
// Handle new connections
wss.on('connection', (ws) => {
console.log('New client connected');
// Track subscriptions for this client
const clientSubscriptions = new Set();
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
if (data.type === 'subscribe' && data.payload.symbol) {
const symbol = data.payload.symbol;
clientSubscriptions.add(symbol);
// Add to global subscriptions
if (!subscriptions.has(symbol)) {
subscriptions.set(symbol, new Set());
}
subscriptions.get(symbol).add(ws);
console.log(`Client subscribed to ${symbol}`);
// Send initial data immediately
const initialData = generateTradeData(symbol);
ws.send(JSON.stringify(initialData));
}
} catch (error) {
console.error('Error parsing message:', error);
}
});
ws.on('close', () => {
console.log('Client disconnected');
// Remove client from all subscriptions
clientSubscriptions.forEach(symbol => {
if (subscriptions.has(symbol)) {
subscriptions.get(symbol).delete(ws);
if (subscriptions.get(symbol).size === 0) {
subscriptions.delete(symbol);
}
}
});
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
});
// Broadcast live data to subscribed clients
setInterval(() => {
subscriptions.forEach((clients, symbol) => {
const tradeData = generateTradeData(symbol);
clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(tradeData));
}
});
});
}, 2000); // Send new data every 2 seconds
// Handle server shutdown
process.on('SIGINT', () => {
console.log('Shutting down WebSocket server...');
wss.close(() => {
console.log('Server closed');
process.exit(0);
});
});