-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
217 lines (189 loc) · 6.64 KB
/
index.html
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>有为加密货币价格测试</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
background-image: url('https://images.unsplash.com/photo-1470252649378-9c29740c9fa8?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80');
background-size: cover;
background-position: center;
background-attachment: fixed;
color: #fff;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
}
.content {
background-color: rgba(0, 0, 0, 0.7);
padding: 30px;
border-radius: 15px;
max-width: 1000px;
width: 90%;
box-sizing: border-box;
margin-top: 50px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}
h1 {
text-align: center;
color: #f0f0f0;
font-size: 2.5em;
margin-bottom: 30px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
#clock {
font-size: 3em;
text-align: center;
margin: 20px 0;
color: #f0f0f0;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
#status {
text-align: center;
font-style: italic;
margin-bottom: 30px;
color: #bdc3c7;
font-size: 1.2em;
}
#prices {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 30px;
}
.price-card {
background-color: rgba(255, 255, 255, 0.1);
border-radius: 12px;
padding: 25px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.price-card:hover {
transform: translateY(-10px);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}
.price-up {
color: #2ecc71;
}
.price-down {
color: #e74c3c;
}
.symbol {
font-weight: bold;
font-size: 1.4em;
margin-bottom: 15px;
}
.price {
font-size: 1.8em;
margin-bottom: 10px;
}
.change {
font-size: 1.1em;
}
</style>
</head>
<body>
<div class="content">
<h1>有为加密货币价格测试</h1>
<div id="clock"></div>
<div id="status">正在连接...</div>
<div id="prices"></div>
</div>
<script>
const wsUrl = 'wss://ws.bitget.com/spot/v1/stream';
let ws;
let lastPrices = {};
let connectionAttempts = 0;
const maxAttempts = 5;
const symbols = ['BTC', 'ETH', 'SOL', 'ETC', 'GMT', 'BIGTIME'];
function updateClock() {
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
document.getElementById('clock').textContent = `${hours}:${minutes}:${seconds}`;
}
setInterval(updateClock, 1000);
updateClock();
function connect() {
ws = new WebSocket(wsUrl);
ws.onopen = function () {
console.log('WebSocket 连接已建立');
document.getElementById('status').textContent = '已连接';
connectionAttempts = 0;
subscribe();
};
ws.onmessage = function (event) {
const data = JSON.parse(event.data);
if (data.data && Array.isArray(data.data)) {
data.data.forEach(updatePrice);
}
};
ws.onclose = function (event) {
console.log('WebSocket 连接已关闭', event);
document.getElementById('status').textContent = '连接已断开,正在尝试重新连接...';
if (connectionAttempts < maxAttempts) {
connectionAttempts++;
setTimeout(connect, 5000);
} else {
document.getElementById('status').textContent = '无法建立连接,请刷新页面重试。';
}
};
ws.onerror = function (error) {
console.error('WebSocket 错误:', error);
document.getElementById('status').textContent = 'WebSocket 错误,请查看控制台';
};
}
function subscribe() {
const subscribeMessage = {
"op": "subscribe",
"args": symbols.map(symbol => ({
"instType": "SP",
"channel": "ticker",
"instId": `${symbol}USDT`
}))
};
ws.send(JSON.stringify(subscribeMessage));
console.log('已发送订阅消息');
}
function updatePrice(data) {
const symbol = data.instId.replace('USDT', '');
const price = parseFloat(data.last).toFixed(4);
const change24h = parseFloat(data.chgUTC).toFixed(2);
let priceClass = '';
if (lastPrices[symbol]) {
if (price > lastPrices[symbol]) {
priceClass = 'price-up';
} else if (price < lastPrices[symbol]) {
priceClass = 'price-down';
}
}
lastPrices[symbol] = price;
let priceElement = document.getElementById(`${symbol.toLowerCase()}-price`);
if (!priceElement) {
priceElement = document.createElement('div');
priceElement.id = `${symbol.toLowerCase()}-price`;
priceElement.className = 'price-card';
document.getElementById('prices').appendChild(priceElement);
}
const changeClass = parseFloat(change24h) >= 0 ? 'price-up' : 'price-down';
priceElement.innerHTML = `
<div class="symbol">${symbol}</div>
<div class="price ${priceClass}">$${price}</div>
<div class="change ${changeClass}">24小时变化: ${change24h}%</div>
`;
}
connect();
setInterval(() => {
if (ws.readyState !== WebSocket.OPEN) {
document.getElementById('status').textContent = '连接已断开,正在尝试重新连接...';
connect();
}
}, 2000);
</script>
</body>
</html>