简体   繁体   English

从高速公路Android Websocket客户端发送ping消息

[英]Send ping message from autobahn android websocket client

I'm using autobahn android web socket library to build my web socket client application. 我正在使用autobahn android网络套接字库来构建我的网络套接字客户端应用程序。 In order to keep the web socket connection alive for a long time, I need to periodically send ping messages from the client. 为了使Web套接字连接长时间保持活动状态,我需要定期从客户端发送ping消息。

How can I do that with auotbahn android web socket library? 如何使用auotbahn android Web socket库做到这一点?

AutobahnAndroid's WebSocketWriter supports client pong messages, and AutobahnPython claims the following: AutobahnAndroid的WebSocketWriter 支持客户端Pong消息,而AutobahnPython 声明以下内容:

explicit processing of Pings/Pongs is unnecessary normally - AutobahnPython will do the right thing under the hood. 通常,不需要对Pings / Pongs进行显式处理-AutobahnPython会在后台做正确的事情。

However, during execution of a simple chat client-server setup, I observe no such ping/pong heartbeat messages. 但是,在执行简单的聊天客户端-服务器设置期间,我没有看到这种ping / pong心跳消息。

From what I have read, it is unclear from both the documentation and the source, what the full requirements are for enabling implicit Autobahn* heartbeats. 根据我的阅读,从文档和资料来源都不清楚启用隐式Autobahn *心跳的全部要求是什么。

My personal solution involves issuing a ping from the server during onConnection and the appropriate onMesssage , and something similar to the following client code onMessage : 我的个人解决方案涉及在onConnection和适当的onMesssage期间从服务器发出ping 命令 ,并且类似于以下客户端代码onMessage

if (payload.equals("ping from the server"))) {
    handler.postDelayed(new Runnable() {
        @Override
            public void run() {
                myWebSocket.sendTextMessage("pong from the client");
            }
    }, 1000L);
}

... which schedules a pong response to be sent by the client, 1 second after receiving a ping from the server. ...计划从客户端接收到ping后1秒钟由客户端发送的pong响应。

You can use the pull request that I just submitted 您可以使用我刚刚提交的拉取请求

https://github.com/tavendo/AutobahnAndroid/pull/67 https://github.com/tavendo/AutobahnAndroid/pull/67

which adds sendPingMessage(byte[] payload) and onPongMessage(byte[] payl). 其中添加了sendPingMessage(byte []有效负载)和onPongMessage(byte [] payl)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM