【发布时间】:2021-10-01 00:07:29
【问题描述】:
我正在尝试从火币 USDT 掉期传输比特币价格,但我的 ping/pong 机制无法正常工作,因此火币总是在 30 秒后关闭连接。
这就是文档所说的https://huobiapi.github.io/docs/usdt_swap/v1/en/#market-heartbeat:
WebSocket API 支持双向心跳。 Server 和 Client 都可以发送 ping 消息,对方可以用 pong 消息返回。
WebSocket Server 发送心跳: {“平”:18212558000}
WebSocket 客户端应该响应: {“乒乓”:18212558000}
注意:一旦 WebSocket 客户端和 WebSocket 服务器建立连接,服务器将每 5 秒发送一次心跳(频率可能会改变)。如果 WebSocket 客户端 5 次忽略心跳消息,连接将自动断开。如果 WebSocket 客户端在最近的 2 个心跳消息中响应一个“ping”值,服务器将保持连接。
using System;
using System.IO;
using System.IO.Compression;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
namespace HuobiMarketWebSocketTest
{
class HuobiMarketWebSocketTest
{
public static void Main(string[] args)
{
int pings = 0;
ClientWebSocket ClientWebSocket = new ClientWebSocket();
string symbol = "btcusdt";
ClientWebSocket.ConnectAsync(new Uri("wss://api.hbdm.com/linear-swap-ws"), CancellationToken.None).Wait();
try
{
// Subscribe to BTC
string subscribeTrade = $"{{ \"sub\": \"market.{symbol}.trade.detail\", \"id\": \"{symbol} trade\"}}";
byte[] subscribeTradeAsBytes = Encoding.UTF8.GetBytes(subscribeTrade);
ClientWebSocket.SendAsync(subscribeTradeAsBytes, WebSocketMessageType.Text, true, CancellationToken.None).Wait();
Console.WriteLine($"{subscribeTrade} sent.");
string subscribeQuote = $"{{ \"sub\": \"market.{symbol}.bbo\", \"id\": \"{symbol} bbo\"}}";
byte[] subscribeQuoteAsBytes = Encoding.UTF8.GetBytes(subscribeQuote);
ClientWebSocket.SendAsync(subscribeQuoteAsBytes, WebSocketMessageType.Text, true, CancellationToken.None).Wait();
Console.WriteLine($"{subscribeQuote} sent.");
ArraySegment<byte> bytesReceived = new ArraySegment<byte>(new byte[819200]);
//Read from WebSocket
while (ClientWebSocket.State == WebSocketState.Open)
{
WebSocketReceiveResult receiveResult = ClientWebSocket.ReceiveAsync(bytesReceived, CancellationToken.None).Result;
byte[] compressedBytes = bytesReceived.Array[..receiveResult.Count];
byte[] decompressedBytes;
//Decompress json
using (MemoryStream compressedStream = new MemoryStream(compressedBytes))
{
using (GZipStream decompressionStream = new GZipStream(compressedStream, CompressionMode.Decompress))
{
using (MemoryStream decompressedStream = new MemoryStream())
{
decompressionStream.CopyTo(decompressedStream);
decompressedBytes = decompressedStream.ToArray();
}
}
}
string json = Encoding.UTF8.GetString(decompressedBytes);
// Handle ping
if (json.Contains("ping"))
{
pings++;
string pong = json.Replace("ping", "pong");
Console.WriteLine($"{json} received");
byte[] pongAsBytes = Encoding.UTF8.GetBytes(pong);
ClientWebSocket.SendAsync(pongAsBytes, WebSocketMessageType.Text, true, CancellationToken.None).Wait();
Console.WriteLine($"{pong} sent");
Console.WriteLine($"{pings} pings received");
}
else
{
// Uncomment to see that subscription worked
//Console.WriteLine(json);
}
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
Console.WriteLine($"Huobi closed WebSocket after {pings} pings.");
}
}
}
}
这是我的程序的输出:
{ "sub": "market.btcusdt.trade.detail", "id": "btcusdt trade"} sent.
{ "sub": "market.btcusdt.bbo", "id": "btcusdt bbo"} sent.
{"ping":1627090272242} received
{"pong":1627090272242} sent
1 pings received
{"ping":1627090277234} received
{"pong":1627090277234} sent
2 pings received
{"ping":1627090282238} received
{"pong":1627090282238} sent
3 pings received
{"ping":1627090287239} received
{"pong":1627090287239} sent
4 pings received
{"ping":1627090292238} received
{"pong":1627090292238} sent
5 pings received
{"ping":1627090297241} received
{"pong":1627090297241} sent
6 pings received
System.AggregateException: One or more errors occurred. (The remote party closed the WebSocket connection without completing the close handshake.)
---> System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.
at System.Net.WebSockets.ManagedWebSocket.ThrowIfEOFUnexpected(Boolean throwOnPrematureClosure)
at System.Net.WebSockets.ManagedWebSocket.EnsureBufferContainsAsync(Int32 minimumRequiredBytes, CancellationToken cancellationToken, Boolean throwOnPrematureClosure)
at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1 payloadBuffer, CancellationToken cancellationToken, TWebSocketReceiveResultGetter resultGetter)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at HuobiMarketWebSocketTest.HuobiMarketWebSocketTest.Main(String[] args) in C:\home\source\csharp\prog\HuobiMarketWebSocketTest - pong not recieved\HuobiMarketWebSocketTest\HuobiMarketWebSocketTest\HuobiMarketWebSocketTest.cs:line 156
Huobi closed WebSocket after 6 pings.
【问题讨论】: