communicating with socket.io from android
Problem Summary
I am trying to bundle a simple socket.io chat app into a native app to run on Android 2.2 and above using phonegap.
socket.io web client --> socket.io server works
I have a simple server running socket.io which successfully communicates with a socket.io client when its run in a web browser.
socket.io mobile client in mobile browser --> socket.io server works
Note that because websockets arent supported on Android 2.2 it defaults back to xhr polling, sample messages from server when connecting through the web browser on Android 2.2
[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
socket.io from phonegap --> socket.io server fails
However when I try to connect with socket.io in phonegap the connection is opened but quickly closed.
In the android log it has an error about requirin flash player >v10 but my phone has 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
The log of events on the server is
[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
The message from the server saying "Welcome stranger!" is received by the phone. On the phone the dialog saying Application Error, The connection to the server was unsuccessful. (file:///android_asset/www/index.html)
phone gap websockets client --> socket.io server fails
I then read that websockets wasn't supported in phonegap yet, and came across this project for WebSocket support in Android's Phonegap apps. When I got this running and tried to connect on the socket.io server and I got the error
[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. I tested it with just a node.js server and it worked
Questions
You're going to need to look at your HTTP headers. My guess is the client side is either not sending keep alive, or it's being ignored by the server. Look for 'Connection: keep-alive' in the XHR request sent by the client. Also look at the responses from the server side. Another indicator might be the http version sent.. 1.0 will imply that the connection is to be closed after the request.
1 - Socket.io adds another layer on top of websocket (it adds heartbeats for example). Connecting with a third party lib won't be accepted by the server (The 'destroying non-socket.io upgrade' message indicates that). I haven't found/used a solution yet, and I may well modify phonegap ObjectiveC code to load an external url instead of using a local resource to avoid this problem : Socket.io + PhoneGap .
2 - The polling is a trick where the client connects to the server as if it wanted to load a page. But the server doesn't provide a page, it writes the messages when they arrive, simulating a socket connection. However, the protocol stack (browser side http if I understood all ?) does not allow a infinite connection. It will break the connection, making the timeout. The socket.io client will then reinit a new connection, until it is timed out again. So having periodic timeouts around every 30s is a normal behavior. If the connection is broken instantly, it may be an acces origin problem, but I'm unsure.
链接地址: http://www.djcxy.com/p/94590.html上一篇: s3中的HTTPS错误“数据长度太长”