与android的socket.io进行通信
问题摘要
我正在尝试将一个简单的socket.io聊天应用程序捆绑到一个原生应用程序中,以使用phonegap在Android 2.2及更高版本上运行。
socket.io Web客户端 - > socket.io服务器工作
我有一个运行socket.io的简单服务器,它在一个web浏览器中运行时与一个socket.io客户端成功通信。
移动浏览器中的socket.io移动客户端 - > socket.io服务器工作
请注意,由于Android 2.2不支持websockets,因此它默认返回到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日志中,它有一个关于需要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 event03-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,并且在Android的Phonegap应用程序中遇到了此项目以支持WebSocket。 当我得到这个运行并试图连接到socket.io服务器,我得到了错误
[root@webnode-2 nodechat]# node server.js info - socket.io started debug - destroying non-socket.io upgrade
我相信这是由于试图连接到使用websockets而不使用socket.io客户端js的socket.io服务器。 我用一个node.js服务器测试了它,它工作正常
问题
你将需要看看你的HTTP头。 我的猜测是,客户端不是发送保持活动状态,就是被服务器忽略。 在客户端发送的XHR请求中查找“Connection:keep-alive”。 另请看一下服务器端的响应。 另一个指标可能是发送的http版本.. 1.0将暗示连接将在请求后关闭。
1 - Socket.io在websocket上添加另一个图层(例如添加了心跳)。 连接到第三方库不会被服务器接受('destroying nonsocket.io upgrade'消息表明这一点)。 我还没有找到/使用过解决方案,我可以修改phonegap ObjectiveC代码来加载外部URL而不是使用本地资源来避免此问题:Socket.io + PhoneGap。
2 - 轮询是客户端连接到服务器的一种技巧,就好像它想要加载页面一样。 但服务器不提供页面,它在到达时写入消息,模拟套接字连接。 但是,协议栈(浏览器端http,如果我理解所有?)不允许无限连接。 它会破坏连接,造成超时。 然后socket.io客户端将重新建立一个新的连接,直到它再次超时。 所以每30秒周期性的超时是一个正常的行为。 如果连接瞬间断开,这可能是一个访问原点问题,但我不确定。
链接地址: http://www.djcxy.com/p/94589.html