简体   繁体   English

Android Websocket 客户端超时时间过长

[英]Android Websocket Client taking too long to Timeout

Hi all I have to create an application that needs to stay connected as much as possible to a websocket server to listen for events.大家好,我必须创建一个应用程序,该应用程序需要尽可能多地与 websocket 服务器保持连接以侦听事件。 Since its a security app, resources such as battery and radio usage is not a concern.由于它是一个安全应用程序,因此无需担心电池和无线电使用等资源。

I am using standard org.java_websocket with the Draft_17 spec.我使用标准的 org.java_websocket 和 Draft_17 规范。 The application is able to communicate using websockets.该应用程序能够使用 websockets 进行通信。 The issue is that when the phone loses connection to the server.问题是当手机失去与服务器的连接时。 It takes a few min for the websocket to realise that it's disconnected before calling the onClose() callback with a code 1006. I need to reduce this time as much as possible.在使用代码 1006 调用 onClose() 回调之前,websocket 需要几分钟才能意识到它已断开连接。我需要尽可能减少这个时间。 When using HTTP clients on android this is no issue, we just simply set the socket timeout.在 android 上使用 HTTP 客户端时,这没有问题,我们只需设置套接字超时。 However using org.java_websocket I am unable to figure out how to set this timeout.但是使用 org.java_websocket 我无法弄清楚如何设置这个超时。

I am constantly sending/receiving data from the server.我不断地从服务器发送/接收数据。 Sending does not speed up the socket timeout in any way.发送不会以任何方式加速套接字超时。

There is a connection timeout parameter on the WebSocket constructor. WebSocket 构造函数上有一个连接超时参数。

return new WebSocketClient(hostURI, new Draft_17(), null, 5000)

But it has no effect as I think this is only when trying to connect to the server initially.但它没有效果,因为我认为这只是在最初尝试连接到服务器时。

Any help or guidance will be appreciated任何帮助或指导将不胜感激

如果查看 WebSocketClient 源代码,会提示从不使用 timeout 参数

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

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