binance ping pong

I’m using websocket market streams in a node js application with ws library 1. It receives data all right but connection gets closed every 10 minutes. I’ve tried listening to ping events with ws.onping but it never gets triggered which makes me thing that the server never sends the ping frame. How can I send a ping message to the server? I’ve tried ws.send(‘ping’) but the server responses with { error: { code: 3, msg: ‘Invalid JSON: expected value at line 1 column 1’ } }. What is the correct format for the ping message?

Alright so the code I provided you with is for a node js application as well and it works since I’m using this exact code. It specifies directly to listen to the ping event instead of calling the wrapper, but it’s essentially doing the same, only the syntax for its usage differs. My guess would be that you’re using the wrong syntax for your callback upon receival of the ping request which then leads you to think you’re not receiving a ping from the server.
So now for completeness, here are the two syntaxes.

Specifying the event to listen to:

ws.on('ping', (e) => { //Defines callback for ping event
    ws.pong(); //send pong frame
});

Using the wrapper like you:

ws.onping = () => { //Defines event handler for ping event
    ws.pong(); //send pong frame
};

Now that it’s out of the way, once again, you DO NOT need to send a pingBUT a pong request instead to the server. (Server sends ping, you answer pong… Like ping-pong 2, you know…).
Finally, to send that pong request, as stated in my previous response, you simply need to call ws.pong()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值