与android的socket.io通信

时间:2012-03-19 21:02:44

标签: android node.js cordova socket.io

问题摘要

我正在尝试将一个简单的socket.io聊天应用捆绑到一个原生应用中,以便在Android 2.2及更高版本上使用phonegap运行。

socket.io网络客户端 - > socket.io服务器工作

我有一个运行socket.io的简单服务器,当它在网络浏览器中运行时,它成功地与socket.io客户端通信。

移动浏览器中的socket.io移动客户端 - > socket.io服务器工作

请注意because websockets arent supported on Android 2.2默认返回xhr轮询,在Android 2.2上通过Web浏览器连接时从服务器采样消息

[root@webnode-2 nodechat]# node server.js
   info  - socket.io started
   debug - client authorized
   info  - handshake authorized 6036976111002307981
   debug - setting request GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184313758
   debug - setting poll timeout
   debug - client authorized for
   debug - clearing poll timeout
   debug - xhr-polling writing 1::
   debug - set close timeout for client 6036976111002307981
6036976111002307981
   debug - setting request GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184313839
   debug - setting poll timeout
   debug - clearing poll timeout
   debug - xhr-polling writing 5:::{"name":"message","args":["Welcome stranger!"]}
   debug - set close timeout for client 6036976111002307981
   debug - discarding transport
   debug - cleared close timeout for client 6036976111002307981
   debug - setting request GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184313928
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client 6036976111002307981
   debug - clearing poll timeout
   debug - xhr-polling writing 8::
   debug - set close timeout for client 6036976111002307981
   debug - xhr-polling closed due to exceeded duration
   debug - setting request GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184334139
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client 6036976111002307981
   debug - xhr-polling received data packet 5:::{"name":"connect_friends","args":[{"user_id":"2","friends_list":[1,2,3,4,5,6,7,8,9,10]}]}


REGISTER USER


{ user_id: '2', friends_list: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] } 10 assigned user_id: 2 to socket 6036976111002307981

来自phonegap的

socket.io - > socket.io服务器失败

但是,当我尝试连接phonegap中的socket.io时,连接已打开,但很快就关闭了。

在android日志中有关于requirin flash player> v10的错误,但我的手机有flash player v 11.1

03-19 11:38:46.847: I/PhoneGapLog(18469): Found log level DEBUG
03-19 11:38:46.847: I/PhoneGapLog(18469): Changing log level to DEBUG(3)
03-19 11:38:46.847: D/DroidGap(18469): DroidGap.onCreate()
03-19 11:38:46.847: D/DroidGap(18469): DroidGap.loadUrl(file:///android_asset/www/index.html)
03-19 11:38:46.847: D/DroidGap(18469): DroidGap: url=file:///android_asset/www/index.html baseUrl=file:///android_asset/www/
03-19 11:38:46.847: D/DroidGap(18469): DroidGap.init()
03-19 11:38:46.941: D/dalvikvm(18469): GC_FOR_MALLOC freed 6234 objects / 618952 bytes in 68ms
03-19 11:38:46.968: D/SoftKeyboardDetect(18469): Ignore this event
03-19 11:38:47.007: D/SoftKeyboardDetect(18469): Ignore this event
03-19 11:38:47.058: D/SoftKeyboardDetect(18469): Ignore this event

03-19 11:38:47.241: D/PhoneGapLog(18469): Flash Player >= 10.0.0 is required. 03-19 11:38:47.241: D/PhoneGapLog(18469): file:///android_asset/www/js/socket.io/socket.io.js: Line 2622 : Flash Player >= 10.0.0 is required. 03-19 11:38:47.241: E/Web Console(18469): Flash Player >= 10.0.0 is required. at file:///android_asset/www/js/socket.io/socket.io.js:2622

03-19 11:38:47.796: D/PhoneGapLog(18469): Receiving Welcome stranger! 03-19 11:38:47.796: D/PhoneGapLog(18469): file:///android_asset/www/index.html: Line 8 : Receiving Welcome stranger! 03-19 11:38:47.796: I/Web Console(18469): Receiving Welcome stranger! at file:///android_asset/www/index.html:8 03-19 11:39:07.007: E/DroidGap(18469): DroidGap: TIMEOUT ERROR! - calling webViewClient 03-19 11:39:07.007: D/Cordova(18469): DroidGap: GapViewClient.onReceivedError: Error code=-6 Description=The connection to the server was unsuccessful. URL=file:///android_asset/www/index.html 03-19 11:39:07.167: D/SoftKeyboardDetect(18469): Ignore this event 03-19 11:39:16.323: D/dalvikvm(18469): GC_EXPLICIT freed 6130 objects / 505920 bytes in 156ms

服务器上的事件日志是

[root@webnode-2 nodechat]# node server.js
   info  - socket.io started
   debug - client authorized
   info  - handshake authorized 1009349893764580916
   debug - setting request GET /socket.io/1/xhr-polling/1009349893764580916?t=1332182327502
   debug - setting poll timeout
   debug - client authorized for
   debug - clearing poll timeout
   debug - xhr-polling writing 1::
   debug - set close timeout for client 1009349893764580916
   debug - setting request GET /socket.io/1/xhr-polling/1009349893764580916?t=1332182327602
   debug - setting poll timeout
   debug - clearing poll timeout
   debug - xhr-polling writing 5:::{"name":"message","args":["Welcome stranger!"]}
   debug - set close timeout for client 1009349893764580916
   debug - discarding transport
   debug - cleared close timeout for client 1009349893764580916
   debug - setting request GET /socket.io/1/xhr-polling/1009349893764580916?t=1332182327802
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client 1009349893764580916
   debug - clearing poll timeout
   info  - transport end
   debug - set close timeout for client 1009349893764580916
   debug - cleared close timeout for client 1009349893764580916


DISCONNECTED EVENT


debug - discarding transport debug - client authorized info - handshake authorized 2044675477593417130 debug - setting request GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182347938 debug - setting poll timeout debug - client authorized for debug - clearing poll timeout debug - xhr-polling writing 1:: debug - set close timeout for client 2044675477593417130 debug - setting request GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182348112 debug - setting poll timeout debug - clearing poll timeout debug - xhr-polling writing 5:::{"name":"message","args":["Welcome stranger!"]} debug - set close timeout for client 2044675477593417130 debug - discarding transport debug - cleared close timeout for client 2044675477593417130 debug - setting request GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182348308 debug - setting poll timeout debug - discarding transport debug - cleared close timeout for client 2044675477593417130 debug - clearing poll timeout info - transport end debug - set close timeout for client 2044675477593417130 debug - cleared close timeout for client 2044675477593417130


DISCONNECTED EVENT


debug - discarding transport

来自服务器的消息说"欢迎陌生人!"是通过电话收到的。在电话上,对话框显示应用程序错误,与服务器的连接失败。 (文件:///android_asset/www/index.html)

电话间隙websockets客户端 - > socket.io服务器失败

然后我读到,在phonegap中还没有支持websockets,并且WebSocket support in Android’s Phonegap apps遇到了这个项目。当我运行并尝试连接socket.io服务器时,我收到了错误

[root@webnode-2 nodechat]# node server.js
   info  - socket.io started
   debug - destroying non-socket.io upgrade

which i believe is due to trying to connect to a socket.io server using websockets without using the socket.io client js。我只使用node.js服务器测试它并且它可以工作

问题

  1. 有没有办法告诉socket.io关于使用WebSocket support in Android’s Phonegap apps创建的websocket?
  2. 为什么xhr轮询超时?

2 个答案:

答案 0 :(得分:1)

您需要查看HTTP标头。我的猜测是客户端要么不发送保持活动,要么被服务器忽略。在客户端发送的XHR请求中查找“Connection:keep-alive”。另请查看服务器端的响应。另一个指标可能是发送的http版本.. 1.0将暗示在请求后要关闭连接。

答案 1 :(得分:1)

1 - Socket.io在websocket的顶部添加了另一层(例如,它添加了心跳)。连接第三方lib不会被服务器接受('销毁nonsocket.io upgrade'消息表明)。我还没有找到/使用过解决方案,我可能会修改phonegap ObjectiveC代码以加载外部网址而不是使用本地资源来避免此问题:Socket.io + PhoneGap

2 - 轮询是一种技巧,客户端连接到服务器就好像它想要加载页面一样。但是服务器不提供页面,它在消息到达时写入消息,模拟套接字连接。但是,协议栈(浏览器端http,如果我理解全部?)不允许无限连接。它将断开连接,使超时。然后,socket.io客户端将重新启动新连接,直到它再次超时。因此,每30秒左右定期超时是一种正常行为。如果连接立即中断,则可能是访问源问题,但我不确定。