Administrator
2 days ago 39dc91a2cd323c8c211000dfb357cf9cb5907687
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OKX WebSocket 频道连接</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .channel {
            margin-bottom: 20px;
            padding: 15px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .channel h3 {
            margin-top: 0;
        }
        .status {
            font-weight: bold;
        }
        .connected {
            color: green;
        }
        .disconnected {
            color: red;
        }
        .messages {
            height: 200px;
            overflow-y: scroll;
            border: 1px solid #eee;
            padding: 10px;
            margin-top: 10px;
            background-color: #f9f9f9;
        }
        .chart-container {
            width: 100%;
            height: 300px;
            margin-top: 20px;
        }
        .mode-toggle {
            margin: 10px 0;
            padding: 10px;
            background-color: #f0f0f0;
            border-radius: 5px;
        }
        .mode-toggle button {
            margin: 0 5px;
            padding: 5px 10px;
            border: 1px solid #ccc;
            background-color: #fff;
            cursor: pointer;
        }
        .mode-toggle button.active {
            background-color: #007bff;
            color: white;
        }
    </style>
</head>
<body>
    <h1>OKX WebSocket 频道连接</h1>
    
    <div class="mode-toggle">
        <span>当前模式:</span>
        <button id="demoModeBtn" class="active" onclick="switchMode('demo')">模拟盘 (wspap)</button>
        <button id="liveModeBtn" onclick="switchMode('live')">实盘 (ws)</button>
    </div>
    
    <div class="channel" id="publicChannel">
        <h3>公共频道</h3>
        <p>URL: <input type="text" id="publicUrl" value="wss://wspap.okx.com:8443/ws/v5/public" style="width: 300px;"></p>
        <p>状态: <span class="status disconnected" id="publicStatus">未连接</span></p>
        <button onclick="toggleConnection('public')">连接/断开</button>
        <button onclick="clearMessages('public')">清除消息</button>
        <div class="chart-container">
            <canvas id="ethUsdtChart"></canvas>
        </div>
        <div class="messages" id="publicMessages"></div>
    </div>
    
    <div class="channel" id="privateChannel">
        <h3>私有频道</h3>
        <p>URL: <input type="text" id="privateUrl" value="wss://wspap.okx.com:8443/ws/v5/private" style="width: 300px;"></p>
        <p>状态: <span class="status disconnected" id="privateStatus">未连接</span></p>
        <button onclick="toggleConnection('private')">连接/断开</button>
        <div class="messages" id="privateMessages"></div>
    </div>
    
    <div class="channel" id="businessChannel">
        <h3>业务频道</h3>
        <p>URL: <input type="text" id="businessUrl" value="wss://wspap.okx.com:8443/ws/v5/business" style="width: 300px;"></p>
        <p>状态: <span class="status disconnected" id="businessStatus">未连接</span></p>
        <button onclick="toggleConnection('business')">连接/断开</button>
        <div class="messages" id="businessMessages"></div>
    </div>
    
    <div class="channel" id="markPriceCandleChannel">
        <h3>标记价格K线频道</h3>
        <p>URL: <input type="text" id="markPriceCandleUrl" value="wss://wspap.okx.com:8443/ws/v5/business" style="width: 300px;"></p>
        <p>状态: <span class="status disconnected" id="markPriceCandleStatus">未连接</span></p>
        <button onclick="toggleConnection('markPriceCandle')">连接/断开</button>
        <div class="messages" id="markPriceCandleMessages"></div>
    </div>
 
    <script>
        // 存储WebSocket连接对象
        const connections = {
            public: null,
            private: null,
            business: null,
            markPriceCandle: null
        };
        
        // 存储各频道的URL
        const urls = {
            demo: {
                public: 'wss://wspap.okx.com:8443/ws/v5/public',
                private: 'wss://wspap.okx.com:8443/ws/v5/private',
                business: 'wss://wspap.okx.com:8443/ws/v5/business'
            },
            live: {
                public: 'wss://ws.okx.com:8443/ws/v5/public',
                private: 'wss://ws.okx.com:8443/ws/v5/private',
                business: 'wss://ws.okx.com:8443/ws/v5/business'
            }
        };
        
        // 为markPriceCandle频道添加URL配置
        urls.demo.markPriceCandle = urls.demo.business;
        urls.live.markPriceCandle = urls.live.business;
        
        // 当前模式,默认为模拟盘
        let currentMode = 'demo';
        
        // ETH-USDT 价格数据
        const ethUsdtPrices = [];
        const ethUsdtTimes = [];
        let ethUsdtChart = null;
        
        // 初始化图表
        function initChart() {
            const ctx = document.getElementById('ethUsdtChart').getContext('2d');
            ethUsdtChart = new Chart(ctx, {
                type: 'line',
                data: {
                    labels: ethUsdtTimes,
                    datasets: [{
                        label: 'ETH-USDT Price',
                        data: ethUsdtPrices,
                        borderColor: 'rgb(75, 192, 192)',
                        backgroundColor: 'rgba(75, 192, 192, 0.2)',
                        tension: 0.1,
                        fill: false
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    scales: {
                        x: {
                            type: 'time',
                            time: {
                                unit: 'minute'
                            }
                        },
                        y: {
                            beginAtZero: false
                        }
                    }
                }
            });
        }
        
        // 更新图表
        function updateChart() {
            if (ethUsdtChart) {
                // 对于折线图,我们需要重新设置labels和data并调用update
                ethUsdtChart.data.labels = ethUsdtTimes;
                ethUsdtChart.data.datasets[0].data = ethUsdtPrices;
                ethUsdtChart.update();
            }
        }
        
        // 连接/断开WebSocket连接
        function toggleConnection(channel) {
            if (connections[channel]) {
                // 如果已经连接,则断开连接
                connections[channel].close();
                connections[channel] = null;
                document.getElementById(`${channel}Status`).textContent = '未连接';
                document.getElementById(`${channel}Status`).className = 'status disconnected';
            } else {
                // 如果未连接,则建立连接
                connectToChannel(channel);
            }
        }
        
        // 清除指定频道的消息
        function clearMessages(channel) {
            const messagesDiv = document.getElementById(`${channel}Messages`);
            messagesDiv.innerHTML = '';
            
            // 清除图表数据
            if (channel === 'public') {
                ethUsdtPrices.length = 0;
                ethUsdtTimes.length = 0;
                updateChart();
            }
        }
        
        // 切换模式
        function switchMode(mode) {
            // 更新当前模式
            currentMode = mode;
            
            // 更新按钮状态
            document.getElementById('demoModeBtn').classList.toggle('active', mode === 'demo');
            document.getElementById('liveModeBtn').classList.toggle('active', mode === 'live');
            
            // 更新所有频道的URL输入框的值
            document.querySelectorAll('.channel').forEach(channelDiv => {
                const channelId = channelDiv.id.replace('Channel', '');
                // 更新输入框的值而不是替换整个元素
                const urlInput = document.getElementById(`${channelId}Url`);
                if (urlInput && urls[mode] && urls[mode][channelId]) {
                    urlInput.value = urls[mode][channelId];
                }
            });
        }
        
        // 连接到指定频道
        function connectToChannel(channel) {
            // 获取用户输入的URL
            let url;
            switch(channel) {
                case 'public':
                    url = document.getElementById('publicUrl').value;
                    break;
                case 'private':
                    url = document.getElementById('privateUrl').value;
                    break;
                case 'business':
                    url = document.getElementById('businessUrl').value;
                    break;
                case 'markPriceCandle':
                    url = document.getElementById('markPriceCandleUrl').value;
                    break;
                default:
                    url = urls[currentMode][channel];
            }
            
            const ws = new WebSocket(url);
            
            ws.onopen = function() {
                console.log(`${channel}频道连接成功`);
                document.getElementById(`${channel}Status`).textContent = '已连接';
                document.getElementById(`${channel}Status`).className = 'status connected';
                
                // 发送订阅消息
                if (channel === 'public') {
                    const subscribeMsg = {
                        op: 'subscribe',
                        args: [
                            {
                                channel: 'tickers',
                                instId: 'ETH-USDT'
                            }
                        ]
                    };
                    ws.send(JSON.stringify(subscribeMsg));
                    
                    // 初始化图表
                    if (!ethUsdtChart) {
                        initChart();
                    }
                } else if (channel === 'markPriceCandle') {
                    // 订阅标记价格K线数据 (BTC-USD-190628, 1分钟K线)
                    const subscribeMsg = {
                        op: 'subscribe',
                        args: [
                            {
                                channel: 'mark-price-candle1m',
                                instId: 'ETH-USDT'
                            }
                        ]
                    };
                    ws.send(JSON.stringify(subscribeMsg));
                }
            };
            
            ws.onmessage = function(event) {
                const message = event.data;
                console.log(`${channel}频道收到消息:`, message);
                
                // 在页面上显示消息
                const messagesDiv = document.getElementById(`${channel}Messages`);
                const messageElement = document.createElement('div');
                
                // 尝试解析JSON消息并格式化显示
                try {
                    const jsonData = JSON.parse(message);
                    
                    // 检查是否为ETH-USDT的ticker数据
                    if (jsonData.arg && jsonData.arg.instId === 'ETH-USDT' && jsonData.data && jsonData.data.length > 0) {
                        const price = parseFloat(jsonData.data[0].last);
                        const time = new Date();
                        
                        // 添加价格和时间到数组
                        ethUsdtPrices.push(price);
                        ethUsdtTimes.push(time);
                        
                        // 限制数据点数量,只保留最近的1000个
                        // 为了保持图表连续性,我们采用滑动窗口的方式
                        if (ethUsdtPrices.length > 1000) {
                            // 移除最旧的数据点
                            ethUsdtPrices.shift();
                            ethUsdtTimes.shift();
                            
                            // 为了保持图表连续性,我们需要保留最后一个数据点作为下一个数据段的起始点
                            // 这样可以避免图表出现断裂
                            // 当数据点超过1000个时,我们重新构建数据集,确保连续性
                            if (ethUsdtPrices.length > 1) {
                                // 保留最后一个数据点作为新数据段的起始点
                                const lastPrice = ethUsdtPrices[ethUsdtPrices.length - 1];
                                const lastTime = ethUsdtTimes[ethUsdtTimes.length - 1];
                                
                                // 清空数组,但保留最后一个数据点
                                ethUsdtPrices.length = 0;
                                ethUsdtTimes.length = 0;
                                
                                // 将最后一个数据点作为新数据段的起始点
                                ethUsdtPrices.push(lastPrice);
                                ethUsdtTimes.push(lastTime);
                            }
                        }
                        
                        // 更新图表
                        updateChart();
                    }
                    
                    // 检查是否为标记价格K线数据
                    if (jsonData.arg && jsonData.arg.channel && jsonData.arg.channel.startsWith('mark-price-candle') && 
                        jsonData.arg.instId === 'BTC-USD-190628' && jsonData.data && jsonData.data.length > 0) {
                        // 这里可以处理标记价格K线数据
                        // 标记价格K线数据格式: [ts, o, h, l, c]
                        // ts: 开始时间, o: 开盘价, h: 最高价, l: 最低价, c: 收盘价
                        console.log('标记价格K线数据:', jsonData.data[0]);
                    }
                    
                    messageElement.innerHTML = `<strong>[${new Date().toLocaleTimeString()}]</strong> ${JSON.stringify(jsonData, null, 2)}`;
                } catch (e) {
                    // 如果不是JSON格式,则直接显示
                    messageElement.innerHTML = `<strong>[${new Date().toLocaleTimeString()}]</strong> ${message}`;
                }
                
                messagesDiv.appendChild(messageElement);
                messagesDiv.scrollTop = messagesDiv.scrollHeight;
            };
            
            ws.onerror = function(error) {
                console.error(`${channel}频道连接错误:`, error);
                document.getElementById(`${channel}Status`).textContent = '连接错误';
                document.getElementById(`${channel}Status`).className = 'status disconnected';
            };
            
            ws.onclose = function() {
                console.log(`${channel}频道连接已关闭`);
                document.getElementById(`${channel}Status`).textContent = '连接已关闭';
                document.getElementById(`${channel}Status`).className = 'status disconnected';
                connections[channel] = null;
            };
            
            connections[channel] = ws;
        }
    </script>
</body>
</html>