android如何实现websocket 服务端实现,该选择哪个库

AndroidAsync :一个基于nio的异步socket ,http(客户端服务器端),websocket,socket.io库
AndroidAsync :一个基于nio的异步socket ,http(客户端服务器端),websocket,socket.io库
AndroidAsync&&是一个基于nio的异步socket ,http(客户端服务器端),websocket,socket.io库,AndroidAsync 是一个底层的网络协议库,如果你想要一个容易使用,高级的,http请求库,请使用Ion(它是基于AndroidAsync 的),正常来说开发者更倾向于使用 &Ion。
如果你需要一个未被封装的Android的raw Socket,&HTTP client/server, WebSocket, and Socket.IO,&AndroidAsync 正适合你。
基于NIO,一个线程,回调驱动,高效
&所有的操作返回一个Future,而且可以取消
All operations return a Future that can be cancelled
Socket client + socket server
HTTP client + server
WebSocket client + server
Socket.IO 客户端
12345&dependency&&&&&&groupId&com.koushikdutta.async&/groupId&&&&&&artifactId&androidasync&/artifactId&&&&&&version&(insert latest version)&/version&&/dependency&
下载一个字符串
123456789101112// url is the URL to download.AsyncHttpClient.getDefaultInstance().getString(url, new AsyncHttpClient.StringCallback() {&&&&// Callback is invoked with any exceptions/errors, and the result, if available.&&&&@Override&&&&public void onCompleted(Exception e, AsyncHttpResponse response, String result) {&&&&&&&&if (e != null) {&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&return;&&&&&&&&}&&&&&&&&System.out.println("I got a string: " + result);&&&&}});
下载一个JSON
123456789101112// url is the URL to download.AsyncHttpClient.getDefaultInstance().getJSONObject(url, new AsyncHttpClient.JSONObjectCallback() {&&&&// Callback is invoked with any exceptions/errors, and the result, if available.&&&&@Override&&&&public void onCompleted(Exception e, AsyncHttpResponse response, JSONObject result) {&&&&&&&&if (e != null) {&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&return;&&&&&&&&}&&&&&&&&System.out.println("I got a JSONObject: " + result);&&&&}});
下载到一个文件
12345678910AsyncHttpClient.getDefaultInstance().getFile(url, filename, new AsyncHttpClient.FileCallback() {&&&&@Override&&&&public void onCompleted(Exception e, AsyncHttpResponse response, File result) {&&&&&&&&if (e != null) {&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&return;&&&&&&&&}&&&&&&&&System.out.println("my file is available at: " + result.getAbsolutePath());&&&&}});
1234// arguments are the http client, the directory to store cache files, and the size of the cache in bytesResponseCacheMiddleware.addCache(AsyncHttpClient.getDefaultInstance(),&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&getFileStreamPath("asynccache"),&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&1024 * 1024 * 10);
创建一个socket
1234567891011121314151617181920212223AsyncHttpClient.getDefaultInstance().websocket(get, "my-protocol", new WebSocketConnectCallback() {&&&&@Override&&&&public void onCompleted(Exception ex, WebSocket webSocket) {&&&&&&&&if (ex != null) {&&&&&&&&&&&&ex.printStackTrace();&&&&&&&&&&&&return;&&&&&&&&}&&&&&&&&webSocket.send("a string");&&&&&&&&webSocket.send(new byte[10]);&&&&&&&&webSocket.setStringCallback(new StringCallback() {&&&&&&&&&&&&public void onStringAvailable(String s) {&&&&&&&&&&&&&&&&System.out.println("I got a string: " + s);&&&&&&&&&&&&}&&&&&&&&});&&&&&&&&webSocket.setDataCallback(new DataCallback() {&&&&&&&&&&&&public void onDataAvailable(ByteBufferList byteBufferList) {&&&&&&&&&&&&&&&&System.out.println("I got some bytes!");&&&&&&&&&&&&&&&&// note that this data has been read&&&&&&&&&&&&&&&&byteBufferList.recycle();&&&&&&&&&&&&}&&&&&&&&});&&&&}});
支持socket io
123456789101112131415161718192021222324252627SocketIOClient.connect(AsyncHttpClient.getDefaultInstance(), "http://192.168.1.2:3000", new ConnectCallback() {&&&&@Override&&&&public void onConnectCompleted(Exception ex, SocketIOClient client) {&&&&&&&&if (ex != null) {&&&&&&&&&&&&ex.printStackTrace();&&&&&&&&&&&&return;&&&&&&&&}&&&&&&&&client.setStringCallback(new StringCallback() {&&&&&&&&&&&&@Override&&&&&&&&&&&&public void onString(String string) {&&&&&&&&&&&&&&&&System.out.println(string);&&&&&&&&&&&&}&&&&&&&&});&&&&&&&&client.on("someEvent", new EventCallback() {&&&&&&&&&&&&@Override&&&&&&&&&&&&public void onEvent(JSONArray argument, Acknowledge acknowledge) {&&&&&&&&&&&&&&&&System.out.println("args: " + arguments.toString());&&&&&&&&&&&&}&&&&&&&&});&&&&&&&&client.setJSONCallback(new JSONCallback() {&&&&&&&&&&&&@Override&&&&&&&&&&&&public void onJSON(JSONObject json) {&&&&&&&&&&&&&&&&System.out.println("json: " + json.toString());&&&&&&&&&&&&}&&&&&&&&});&&&&}});
123456789101112131415AsyncHttpPost post = new AsyncHttpPost("http://myservercom/postform.html");MultipartFormDataBody body = new MultipartFormDataBody();body.addFilePart("my-file", new File("/path/to/file.txt");body.addStringPart("foo", "bar");post.setBody(body);AsyncHttpClient.getDefaultInstance().execute(post, new StringCallback() {&&&&@Override&&&&public void onCompleted(Exception e, AsyncHttpResponse source, String result) {&&&&&&&&if (e != null) {&&&&&&&&&&&&ex.printStackTrace();&&&&&&&&&&&&return;&&&&&&&&}&&&&&&&&System.out.println("Server says: " + result);&&&&}});
创建一个http &server
1234567891011121314AsyncHttpServer server = new AsyncHttpServer();List&WebSocket& _sockets = new ArrayList&WebSocket&();server.get("/", new HttpServerRequestCallback() {&&&&@Override&&&&public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {&&&&&&&&response.send("Hello!!!");&&&&}});// listen on port 5000server.listen(5000);// browsing http://localhost:5000 will return Hello!!!
websocket &server
1234567891011121314151617181920212223242526272829303132server.websocket("/live", new WebSocketRequestCallback() {&&&&@Override&&&&public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) {&&&&&&&&_sockets.add(webSocket);&&&&&&&&//Use this to clean up any references to your websocket&&&&&&&&websocket.setClosedCallback(new CompletedCallback() {&&&&&&&&&&&&@Override&&&&&&&&&&&&public void onCompleted(Exception ex) {&&&&&&&&&&&&&&&&try {&&&&&&&&&&&&&&&&&&&&if (ex != null)&&&&&&&&&&&&&&&&&&&&&&&&Log.e("WebSocket", "Error");&&&&&&&&&&&&&&&&} finally {&&&&&&&&&&&&&&&&&&&&_sockets.remove(webSocket);&&&&&&&&&&&&&&&&}&&&&&&&&&&&&}&&&&&&&&});&&&&&&&&webSocket.setStringCallback(new StringCallback() {&&&&&&&&&&&&@Override&&&&&&&&&&&&public void onStringAvailable(String s) {&&&&&&&&&&&&&&&&if ("Hello Server".equals(s))&&&&&&&&&&&&&&&&&&&&webSocket.send("Welcome Client!");&&&&&&&&&&&&}&&&&&&&&});&&&&}});//..Sometime later, broadcast!for (WebSocket socket : _sockets)&&&&socket.send("Fireball!");
支持Future
123Future&String& string = client.getString("/hello.txt");// this will block, and may also throw if there was an error!String value = string.get();
发表评论:
TA的最新馆藏有啥靠谱的 android 的 websocket 库啊?求推荐。另外,android websocket + restify 方案谁玩过? - CNode技术社区
这家伙很懒,什么个性签名都没有留下。
WebSocket对android的支持不太好啊,目前各个浏览器的支持情况:
。Android的4.2+的webkit才支持貌似。
这篇文章的里面推荐用websocket-android-phonegap方案:
可是我clone后一用,发现构造函数还需要webview,我不想搞个webview在我的app里面,我就像纯后台的websoket协议支持。
后来看到了http://autobahn.ws/ ,但是我github上的android版本fork的人不多啊。
目前打算去试试另外一个koush/AndroidAsync 了,貌似fork的人还比较多一些。
纠结啊,有经验的说说,推荐一个啊。
另外,后端用的是nodejs+restify,,而socketio又是支持websocket的,绕个大圈,所以也顺道问问,这个方案是否靠谱,有坑否?
nodejs+restify,,而socketio又是支持websocket的 ,好像,这样也可以吧,坐等大神 来回复。
为何不用webview?
我除非放个隐藏的webview,没啥意义,我理解websocket只是协议而已,没必要非要搅合着webview啊
websocket好像挺火,呵呵
移动端可以用mqtt
AndroidAsync 这个好像不错,我们公司安卓用的就是这个。 nodejs socket io 做server。目前为止没什么问题
,你们公司的AndroidAysnc我推测使用的Socket.io方式连接的,可是Socket.io升级到1.0后,AndroidAsync貌似就不支持Socektio方式了:
koush commented 13 days ago
Use another library, or reimplement with the new protocol. Unfortunately I do not have the time to do so...
koush就是此项目的作者,他说木有时间,木有时间实现!!!唉
貌似原生的WebSocket确实有不少问题,怪不得一堆人写Webcoket的增强客户端呢?我看了好几个开源项目,大家都不用原生的WebSocket调用自己实现的号称支持WebSocket的服务器,而都推荐用自己的客户端,虽然没有去细看代码,看来真的不太靠谱,这个标准的html5的WebSocket API:
算了,忍了,还是去找个靠谱的Socket.io的Android库吧,我其实本意是为了要个好用的WebSocket的Android库,可是 推荐的AndroidAsync只支持到socket.io的0.9,而且websocket方式也是超时(自己测试的结果)。
罢了罢了,重新去找个Android的Socket.IO的客户端了,谁让Socket.io比较成熟呢。
有同样痛楚的同学们欢迎跟帖呻吟
so,我们没有升级到1.0.呵呵
嗯,我弄个0.9了,打算也
到0.9后,使用AndroidAsync的socketio方式终于通了,这个蛋疼的过程啊,了解了很多东西
所以还是 AndroidAsync +
CNode 社区为国内最专业的 Node.js 开源技术社区,致力于 Node.js 的技术研究。
服务器赞助商为
,存储赞助商为
,由提供应用性能服务。
新手搭建 Node.js 服务器,推荐使用无需备案的Android中使用WebSocket会有什么问题?_百度知道
Android中使用WebSocket会有什么问题?
在Android中使用WebSocket会不会出现什么问题,或者说WebSocket用在Android应用中会出现什么问题?现在的WebSocket在Android聊天系统中使用成不成熟?
您的回答被采纳后将获得:
系统奖励20(财富值+经验值)+难题奖励10(财富值+经验值)+提问者悬赏10(财富值+经验值)
我有更好的答案
完全不知道你要问什么?说清楚些
其他类似问题
为您推荐:
您可能关注的推广
websocket的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁求助:关于android 使用websocket遇到的棘手问题(用过websocket-android-phonegap的进)
[问题点数:80分,结帖人tf110012]
求助:关于android 使用websocket遇到的棘手问题(用过websocket-android-phonegap的进)
[问题点数:80分,结帖人tf110012]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
本帖子已过去太久远了,不再提供回复功能。Android &&&&最新内容
Android &&&&随机内容

我要回帖

更多关于 c语言实现websocket 的文章

 

随机推荐