与android的socket.io通信

问题摘要

我试图将一个简单的socket.io聊天应用程序捆绑到一个本机应用程序运行Android 2.2及以上使用phonegap。

socket.ionetworking客户端 – > socket.io服务器的作品

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

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

请注意, 由于Android 2.2不支持websockets,因此默认返回到xhr轮询,在通过Android 2.2上的Web浏览器连接时从服务器接收消息

  [root @ webnode-2 nodechat]#节点server.js
   信息 -  socket.io启动
   debugging - 客户端授权
   信息 - 握手授权6036976111002307981
   debugging - 设置请求GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184313758
   debugging - 设置轮询超时
   debugging - 客户端授权
   debugging - 清除轮询超时
   debugging -  xhr-polling写作1 ::
   debugging - 为客户端6036976111002307981设置closures超时
 6036976111002307981
   debugging - 设置请求GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184313839
   debugging - 设置轮询超时
   debugging - 清除轮询超时
    debug  -  xhr-polling写作5 ::: {“name”:“message”,“args”:[“欢迎陌生人!”]}
   debugging - 为客户端6036976111002307981设置closures超时
   debugging - 丢弃传输
   debugging - 为客户端6036976111002307981清除closures超时
   debugging - 设置请求GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184313928
   debugging - 设置轮询超时
   debugging - 丢弃传输
   debugging - 为客户端6036976111002307981清除closures超时
   debugging - 清除轮询超时
   debugging -  xhr-polling写作8 ::
   debugging - 为客户端6036976111002307981设置closures超时
    debug  -  xhr-polling由于超过持续时间而closures
   debugging - 设置请求GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184334139
   debugging - 设置轮询超时
   debugging - 丢弃传输
   debugging - 为客户端6036976111002307981清除closures超时
    debug  -  xhr-polling收到的数据包5 ::: {“name”:“connect_friends”,“args”:[{“user_id”:“2”,“friends_list”:[1,2,3,4,5, 6,7,8,9,10]}]} 


注册用户


{user_id:'2',
friends_list:[1,2,3,4,5,6,7,8,9,10]}
10
将user_id:2分配给套接字6036976111002307981

来自phonegap的socket.io – > socket.io服务器失败

但是,当我尝试在phonegap连接socket.io连接打开,但很快closures。

在Android日志中,它有一个关于需求Flash Player> V10的错误,但我的手机有Flash Player V 11.1

 03-19 11:38:46.847:I / PhoneGapLog(18469):find日志级别DEBUG
 03-19 11:38:46.847:I / PhoneGapLog(18469):将日志级别更改为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在68ms中释放了6234个对象/ 618952个字节
 03-19 11:38:46.968:D / SoftKeyboardDetect(18469):忽略此事件
 03-19 11:38:47.007:D / SoftKeyboardDetect(18469):忽略此事件
 03-19 11:38:47.058:D / SoftKeyboardDetect(18469):忽略此事件 

03-19 11:38:47.241:D / PhoneGapLog(18469):Flash Player> = 10.0.0是必需的。
03-19 11:38:47.241:D / PhoneGapLog(18469):file:///android_asset/www/js/socket.io/socket.io.js:2622行:Flash Player> = 10.0.0是必需的。
03-19 11:38:47.241:E / Web控制台(18469):Flash Player> = 10.0.0是必需的。 在file:///android_asset/www/js/socket.io/socket.io.js:2622

03-19 11:38:47.796:D / PhoneGapLog(18469):接收欢迎陌生人!
03-19 11:38:47.796:D / PhoneGapLog(18469):file:///android_asset/www/index.html:第8行:接收欢迎陌生人!
03-19 11:38:47.796:I / Web控制台(18469):接收欢迎陌生人! 在file:///android_asset/www/index.html:8
03-19 11:39:07.007:E / DroidGap(18469):DroidGap:TIMEOUT错误! – 调用webViewClient
03-19 11:39:07.007:D / Cordova(18469):DroidGap:GapViewClient.onReceivedError:错误代码= -6说明=连接到服务器不成功。 URL =文件:///android_asset/www/index.html
03-19 11:39:07.167:D / SoftKeyboardDetect(18469):忽略此事件
03-19 11:39:16.323:D / dalvikvm(18469):GC_EXPLICIT在156ms中释放了6130个对象/ 505920个字节

服务器上的事件日志是

 [root @ webnode-2 nodechat]#节点server.js
   信息 -  socket.io启动
   debugging - 客户端授权
   信息 - 握手授权1009349893764580916
   debugging - 设置请求GET /socket.io/1/xhr-polling/1009349893764580916?t=1332182327502
   debugging - 设置轮询超时
   debugging - 客户端授权
   debugging - 清除轮询超时
   debugging -  xhr-polling写作1 ::
   debugging - 为客户端1009349893764580916设置closures超时
   debugging - 设置请求GET /socket.io/1/xhr-polling/1009349893764580916?t=1332182327602
   debugging - 设置轮询超时
   debugging - 清除轮询超时
    debug  -  xhr-polling写作5 ::: {“name”:“message”,“args”:[“欢迎陌生人!”]}
   debugging - 为客户端1009349893764580916设置closures超时
   debugging - 丢弃传输
   debugging - 为客户端1009349893764580916清除closures超时
   debugging - 设置请求GET /socket.io/1/xhr-polling/1009349893764580916?t=1332182327802
   debugging - 设置轮询超时
   debugging - 丢弃传输
   debugging - 为客户端1009349893764580916清除closures超时
   debugging - 清除轮询超时
   信息传输结束
   debugging - 为客户端1009349893764580916设置closures超时
   debugging - 为客户端1009349893764580916清除closures超时 


DISCONNECTED事件


debugging – 丢弃传输
debugging – 客户端授权
信息 – 握手授权2044675477593417130
debugging – 设置请求GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182347938
debugging – 设置轮询超时
debugging – 客户端授权
debugging – 清除轮询超时
debugging – xhr-polling写作1 ::
debugging – 为客户端2044675477593417130设置closures超时
debugging – 设置请求GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182348112
debugging – 设置轮询超时
debugging – 清除轮询超时
debug – xhr-polling写作5 ::: {“name”:“message”,“args”:[“欢迎陌生人!”]}
debugging – 为客户端2044675477593417130设置closures超时
debugging – 丢弃传输
debugging – 为客户端2044675477593417130清除closures超时
debugging – 设置请求GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182348308
debugging – 设置轮询超时
debugging – 丢弃传输
debugging – 为客户端2044675477593417130清除closures超时
debugging – 清除轮询超时
信息传输结束
debugging – 为客户端2044675477593417130设置closures超时
debugging – 为客户端2044675477593417130清除closures超时


DISCONNECTED事件


debugging – 丢弃传输

来自服务器的消息说:“欢迎陌生人!” 被电话接收。 在电话上说出应用程序错误的对话框,与服务器的连接不成功。 (文件:///android_asset/www/index.html)

手机间隙websockets客户端 – > socket.io服务器失败

然后我读到了websockets在phonegap中不被支持,并且在Android的Phonegap应用程序中遇到了这个WebSocket支持的项目。 当我得到这个运行,并试图连接到socket.io服务器,我得到了错误

  [root @ webnode-2 nodechat]#节点server.js
   信息 -  socket.io启动
   debugging - 销毁nonsocket.io升级

我相信是由于试图连接到一个socket.io服务器使用websockets而不使用socket.io客户端js 。 我用一个node.js服务器testing了它,它工作

问题

  1. 有没有办法告诉socket.io有关在Android的Phonegap应用程序中使用WebSocket支持创build的websocket?
  2. 为什么xhr轮询超时?

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

1 – Socket.io在websocket上添加另一个图层(例如添加了心跳)。 连接到第三方lib将不会被服务器接受('destroying nonsocket.io upgrade'消息表明)。 我还没有find/使用一个解决scheme,我可以修改phonegap ObjectiveC代码来加载一个外部的URL,而不是使用本地资源,以避免这个问题: Socket.io + PhoneGap 。

2 – 轮询是客户端连接到服务器的一个技巧,就好像它想要加载一个页面一样。 但是服务器不提供页面,它在到达时写入消息,模拟套接字连接。 但是,协议栈(浏览器端http,如果我理解所有?)不允许无限的连接。 它会中断连接,使超时。 然后socket.io客户端将重新build立一个新的连接,直到它再次超时。 所以每30秒周期性的超时是一个正常的行为。 如果连接瞬间断开,可能是一个访问原点问题,但我不确定。