从android与socket.io通信

Posted

技术标签:

【中文标题】从android与socket.io通信【英文标题】:communicating with socket.io from android 【发布时间】:2012-04-04 09:28:59 【问题描述】:

问题总结

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

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

我有一个运行 socket.io 的简单服务器,当它在 Web 浏览器中运行时,它成功地与 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]]


注册用户


user_id: '2', 朋友列表:[1、2、3、4、5、6、7、8、9、10] 10 分配 user_id: 2 到套接字 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。 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。在文件:///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):接待欢迎陌生人!在文件:///android_asset/www/index.html:8 03-19 11:39:07.007:E/DroidGap(18469):DroidGap:超时错误! - 调用 webViewClient 03-19 11:39:07.007: D/Cordova(18469): DroidGap: GapViewClient.onReceivedError: 错误代码=-6 描述=与服务器的连接不成功。 URL=file:///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 在 156 毫秒内释放了 6130 个对象/505920 个字节

服务器上的事件日志是

[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


断开连接的事件


调试 - 丢弃传输 调试 - 客户端授权 信息 - 握手授权 2044675477593417130 调试 - 设置请求 GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182347938 调试 - 设置轮询超时 调试 - 客户端授权 调试 - 清除轮询超时 调试 - xhr-polling 写入 1:: 调试 - 为客户端 2044675477593417130 设置关闭超时 调试 - 设置请求 GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182348112 调试 - 设置轮询超时 调试 - 清除轮询超时 调试 - xhr-polling 写作 5:::"name":"message","args":["欢迎陌生人!"] 调试 - 为客户端 2044675477593417130 设置关闭超时 调试 - 丢弃传输 调试 - 清除客户端 2044675477593417130 的关闭超时 调试 - 设置请求 GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182348308 调试 - 设置轮询超时 调试 - 丢弃传输 调试 - 清除客户端 2044675477593417130 的关闭超时 调试 - 清除轮询超时 信息 - 运输端 调试 - 为客户端 2044675477593417130 设置关闭超时 调试 - 清除客户端 2044675477593417130 的关闭超时


断开连接的事件


调试 - 丢弃传输

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

phone gap 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 服务器对其进行了测试,它工作正常

问题

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

【问题讨论】:

只是一个建议...尝试禁用一些 Socket.IO 传输。尝试限制 XHR 看看会发生什么? 检查您的访问来源 (***.com/questions/5911255/…) 【参考方案1】:

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

【讨论】:

【参考方案2】:

1 - Socket.io 在 websocket 之上添加了另一层(例如,它添加了心跳)。服务器不接受与第三方库的连接(“破坏非socket.io升级”消息表明)。我还没有找到/使用解决方案,我可能会修改 phonegap ObjectiveC 代码以加载外部 url 而不是使用本地资源来避免这个问题:Socket.io + PhoneGap。

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

【讨论】:

以上是关于从android与socket.io通信的主要内容,如果未能解决你的问题,请参考以下文章

手机端与网页通过websocket通信

没有http服务器的Socket.IO?

是否可以将 UDP 与 socket.io 一起使用?

Socket.io 延伸

socket.io客户端API

无法在 Android 客户端和 Node.js socket.io 服务器之间进行通信