感谢阅读!
背景
我正在为服务器开发一个Android客户端,其中需求是应用程序,需要与基于 WebSockets
的服务器来回连续交换消息。
实施:对于客户端,我使用适用于Android的weberknecht's WebSocket
客户端库,而服务器基于Tornado
。
问题:
目前,我通过生成initWebSocketClient
在onCreate
中调用AsyncTask
。但是,我一直收到客户端超时异常。
Android客户端:
private void initWebSocketClient() {
Log.e(TAG, "initWebSocketClient");
try {
URI url = new URI("ws://192.168.207.84:8080/");
WebSocket websocket = new WebSocketConnection(url);
// Register Event Handlers
websocket.setEventHandler(new WebSocketEventHandler() {
public void onOpen()
{
Log.e(TAG, "--open");
}
public void onMessage(WebSocketMessage message)
{
Log.e(TAG, "--received message: " + message.getText());
}
public void onClose()
{
Log.e(TAG, "--close");
}
});
// Establish WebSocket Connection
websocket.connect();
// Send UTF-8 Text
websocket.send("hello world");
// Close WebSocket Connection
websocket.close();
}
catch (WebSocketException wse) {
wse.printStackTrace();
}
catch (URISyntaxException use) {
use.printStackTrace();
}
}
龙卷风服务器:
#!/usr/bin/env python
import tornado.ioloop
import tornado.web
import tornado.websocket
class EchoWebSocketHandler(tornado.websocket.WebSocketHandler):
def open(self):
print "WebSocket opened"
def on_message(self, message):
self.write_message(u"You said: " + message)
def on_close(self):
print "WebSocket closed"
application = tornado.web.Application([
(r"/", EchoWebSocketHandler),
])
if __name__ == "__main__":
application.listen(8080)
tornado.ioloop.IOLoop.instance().start()
客户超时例外:
01-31 19:28:01.367: W/System.err(5668): de.roderick.weberknecht.WebSocketException: error while creating socket to ws://192.168.207.84:8080/
01-31 19:28:01.386: W/System.err(5668): at de.roderick.weberknecht.WebSocketConnection.createSocket(WebSocketConnection.java:244)
01-31 19:28:01.386: W/System.err(5668): at de.roderick.weberknecht.WebSocketConnection.connect(WebSocketConnection.java:83)
01-31 19:28:01.386: W/System.err(5668): at com.sagar.websockclient.MainActivity.initWebSocketClient(MainActivity.java:55)
01-31 19:28:01.390: W/System.err(5668): at com.sagar.websockclient.MainActivity.access$0(MainActivity.java:30)
01-31 19:28:01.390: W/System.err(5668): at com.sagar.websockclient.MainActivity$WebSocketTask.doInBackground(MainActivity.java:75)
01-31 19:28:01.390: W/System.err(5668): at com.sagar.websockclient.MainActivity$WebSocketTask.doInBackground(MainActivity.java:1)
01-31 19:28:01.390: W/System.err(5668): at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-31 19:28:01.390: W/System.err(5668): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-31 19:28:01.390: W/System.err(5668): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-31 19:28:01.390: W/System.err(5668): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-31 19:28:01.390: W/System.err(5668): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-31 19:28:01.390: W/System.err(5668): at java.lang.Thread.run(Thread.java:856)
01-31 19:28:01.390: W/System.err(5668): Caused by: java.net.ConnectException: failed to connect to /192.168.207.84 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
01-31 19:28:01.390: W/System.err(5668): at libcore.io.IoBridge.connect(IoBridge.java:114)
01-31 19:28:01.394: W/System.err(5668): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-31 19:28:01.394: W/System.err(5668): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
01-31 19:28:01.394: W/System.err(5668): at java.net.Socket.startupSocket(Socket.java:566)
01-31 19:28:01.394: W/System.err(5668): at java.net.Socket.tryAllAddresses(Socket.java:127)
01-31 19:28:01.394: W/System.err(5668): at java.net.Socket.(Socket.java:177)
01-31 19:28:01.394: W/System.err(5668): at java.net.Socket.(Socket.java:149)
01-31 19:28:01.394: W/System.err(5668): at de.roderick.weberknecht.WebSocketConnection.createSocket(WebSocketConnection.java:238)
01-31 19:28:01.394: W/System.err(5668): ... 11 more
01-31 19:28:01.394: W/System.err(5668): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
01-31 19:28:01.398: W/System.err(5668): at libcore.io.Posix.connect(Native Method)
01-31 19:28:01.398: W/System.err(5668): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
01-31 19:28:01.398: W/System.err(5668): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-31 19:28:01.398: W/System.err(5668): at libcore.io.IoBridge.connect(IoBridge.java:112)
01-31 19:28:01.398: W/System.err(5668): ... 18 more
有人可以帮助我吗?
更新:添加AutoBahn(适用于Android的WebSocket客户端库)实施细节:
AutoBahn客户端
private void connect2() {
mConnection = new WebSocketConnection();
final String wsuri = "ws://192.168.0.137:8888/";
String TAG = "ArticlesListActivity";
try {
mConnection.connect(wsuri, new WebSocketHandler() {
@Override
public void onOpen() {
Log.e(TAG, "Connected to: " + wsuri);
}
@Override
public void onTextMessage(String payload) {
Log.e(TAG, "Message recieved = " + payload);
}
@Override
public void onClose(int code, String reason) {
Log.e(TAG, "Connection Lost.");
}
});
} catch (WebSocketException e) {
Log.d(TAG, e.toString());
}
}
使用此库 - 我能够立即连接到服务器,连接丢失。 (请参阅下面的“AutoBahn客户端日志”。切换回weberknecht工作正常。我没有明确地在任何地方调用mConnection.disconnect()
。所以我不知道连接是如何终止的。
AutoBahn客户端日志
02-27 14:27:36.605: D/de.tavendo.autobahn.WebSocketConnection(27701): created
02-27 14:27:36.656: D/dalvikvm(27701): GC_FOR_ALLOC freed 164K, 4% free 9247K/9543K, paused 17ms
02-27 14:27:36.671: D/dalvikvm(27701): GC_FOR_ALLOC freed 7K, 4% free 9368K/9735K, paused 14ms
02-27 14:27:36.671: D/de.tavendo.autobahn.WebSocketReader(27701): created
02-27 14:27:36.675: D/de.tavendo.autobahn.WebSocketConnection(27701): WS reader created and started
02-27 14:27:36.675: D/de.tavendo.autobahn.WebSocketReader(27701): running
02-27 14:27:36.675: D/de.tavendo.autobahn.WebSocketWriter(27701): created
02-27 14:27:36.675: D/de.tavendo.autobahn.WebSocketConnection(27701): WS writer created and started
02-27 14:27:36.769: D/de.tavendo.autobahn.WebSocketReader(27701): run() : ConnectionLost
02-27 14:27:36.773: D/de.tavendo.autobahn.WebSocketReader(27701): ended
02-27 14:27:36.777: D/de.tavendo.autobahn.WebSocketConnection(27701): opening handshake received
02-27 14:27:36.777: E/ArticlesListActivity(27701): Connected to: ws://192.168.0.137:8888/
02-27 14:27:36.781: D/de.tavendo.autobahn.WebSocketConnection(27701): fail connection [code = 3, reason = WebSockets connection lost
02-27 14:27:36.784: D/de.tavendo.autobahn.WebSocketReader(27701): quit
02-27 14:27:36.788: D/de.tavendo.autobahn.WebSocketWriter(27701): ended
02-27 14:27:36.792: E/ArticlesListActivity(27701): Connection Lost.
02-27 14:27:36.792: D/de.tavendo.autobahn.WebSocketConnection(27701): worker threads stopped
答案 0 :(得分:5)
以下是我将如何开始排除故障的方法:
您是否尝试在手机(设备)或模拟器上执行此操作?如果您尝试在手机上执行此操作和,则通过3G连接,则不会发生这种情况。在这种情况下,您尝试从WAN连接到内部 IP地址,但这不起作用。
再次假设您在设备上执行此操作,如果您通过Wifi连接,那么这是另一个故事。确保192.168.207.84:8080
处的服务器到达。一种快速测试连接的方法(来自您的PC):
telnet 192.168.207.84 8080
如果超时,则表示您的服务器没有响应。当然,这假设您的PC与您的Wifi网络位于相同的子网。
如果您尝试从模拟器执行此操作,请按照[2]中的步骤确保服务器可访问,并确保您的模拟器处于(IP)桥接模式。
< / LI> 醇>总而言之,主原因超时通常是由于缺乏服务器可用性(即缺乏连接性)。您没有提到网络的详细信息,因此我根据您尝试连接的IP(192.168.207.84)做了一些假设。
答案 1 :(得分:5)
据我所知,Weberknecht只支持Hybi-10,而不是最终的RFC6455规范,更有问题的是它是否在主(UI)线程上建立网络。这通常是个坏主意,并且在Android&gt; 2上会失败。
对于Android原生应用,有适用于Android的高速公路WebSockets
https://github.com/oberstet/AutobahnAndroid
它支持最终的RFC6455,与UI和服务应用程序很好地集成,通过WebSockets提供RPC和PubSub等等。在GitHub上查看项目README。
免责声明:我是高速公路的作者。