AndroidAsync :异步Socket,http(client+server),websocket和socket.io的Android类库

时间:2022-08-26 05:07:34

AndroidAsync是一个用于Android应用的异步Socket,http(client+server),websocket和socket.io的类库。基于NIO,没有线程。它使用java.nio来管理连接。因此,所有连接都在一个单一的线程管理,而不是每一个线程。 NIO是极其有效的。

创建web sockets:

01 AsyncHttpClient.getDefaultInstance().websocket(get, "my-protocol", new WebSocketConnectCallback() {
02     @Override
03     public void onCompleted(Exception ex, WebSocket webSocket) {
04         if (ex != null) {
05             ex.printStackTrace();
06             return;
07         }
08         webSocket.send("a string");
09         webSocket.send(new byte[10]);
10         webSocket.setStringCallback(new StringCallback() {
11             public void onStringAvailable(String s) {
12                 System.out.println("I got a string: " + s);
13             }
14         });
15         webSocket.setDataCallback(new DataCallback() {
16             public void onDataAvailable(ByteBufferList byteBufferList) {
17                 System.out.println("I got some bytes!");
18                 // note that this data has been read
19                 byteBufferList.clear();
20             }
21         });
22     }
23 });

项目主页:http://www.open-open.com/lib/view/home/1371461635921