连接使用的SocketChannel的Andr​​oid来的WebSocket [英] Connecting to websocket using SocketChannel Android

查看:857
本文介绍了连接使用的SocketChannel的Andr​​oid来的WebSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了Android应用它连接到一个WebSocket伺服器,服务器应用程序和高速公路的WebSocket库中实现Android应用程序。

I have written android app which connects to a websocket server, server app and android app implemented with Autobahn websocket library.

我可以连接并与服务器交换消息成功。但经过一段时间后(20后 - 30分钟)Android应用程序停止与服务器(它无法发送消息到服务器)进行通信。在logcat中显示的应用程序仍连接,即使它不能将消息发送到服务器的WebSocket。

I can connect and exchange the messages with server successfully. but after some time(after 20 - 30 mins) android app stop communicating with server(it cannot send messages to server). On logcat shows app still connect to websocket even though it cannot send messages to server.

我已经通过高速公路库的源$ C ​​$ C去了,发现它使用的 java.nio.channels.SocketChannel中连接到WebSocket的。是否有使用任何SocketChannels问题? (我发现从这里类似的问题,<一个href=\"http://stackoverflow.com/questions/9859744/android-how-to-keep-connection-with-server-for-a-long-time\">Android - 如何保持与服务器长时间连接)

I have went through the source code of Autobahn library and found that it using java.nio.channels.SocketChannel to connect to the websocket. Are there any issue in using SocketChannels? (I found similar issue from here Android - How to keep connection with server for a long time)

推荐答案

最后,我已经解决了通过使用webscoket 平/乒乓球消息的问题。

Finally I have solved the problem by using webscoket ping/pong message.

其实这个问题是不是与的SocketChannel 后,我曾尝试用普通的插槽而不是使用的SocketChannel。问题甚至不是在插槽解决。

Actually the problem is not with the SocketChannel, I have tried with plain Socket instead of using SocketChannel. Problem not solved even with the Socket.

这是我使用的方法...

This is the approach that I have used...

为了保持WebSocket连接活着,我们需要使用内置的支持websockt协议(详细信息的 WebSocket协议)。服务器需要在周期时间发送消息给客户。当平从服务器接收,客户端的乒乓消息进行回答。使用这种方式,我们可以永葆凌时间连接。

In order to keep websocket connection alive we need to use Ping/Pong messages that built in support from websockt protocol(more information websocket protocol ). Server need to send ping message in periodic time to clients. When ping receives from server, client replies it with pong message. Using this way we can keep the connection alive for ling time.

这是我用建立我webscoket客户端支持此平/乒乓消息的高速公路库。当ping消息通过WebSocket的接收,库自动发送消息乒乓球服务器。因此,我们没有必要担心手动发送傍消息。 <一个 - 你可以从这里( createHandler()方式找到平/乒乓球消息的高速公路实施href=\"https://github.com/tavendo/AutobahnAndroid/blob/master/Autobahn/src/de/tavendo/autobahn/WebSocketConnection.java\"相对=nofollow>高速公路平/乒乓实施)

The Autobahn library that I have used to build my webscoket client support to this ping/pong messages. When ping message receives via websocket, library automatically send pong message to server. So we no need to worry about manually sending pong messages. You can find the autobahn implementation of ping/pong message from here (in createHandler() method - autobahn ping/pong implementation)

您可以得到更多的细节有关从这个线程这个话题,以及<一个href=\"http://stackoverflow.com/questions/8758667/websockets-loss-of-internet-keep-alive-messages-app-architecture-etc\">Websockets.互联网的损失,保持活动的消息,应用架构等

You can get more details about this topic from this thread as well Websockets. Loss of internet, keep-alive messages, app architecture etc

这篇关于连接使用的SocketChannel的Andr​​oid来的WebSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆