AndroidAsync是一个用于Android应用的异步Socket,http(client+server),websocket和socket.io的类库。基于NIO,没有线程。它使用java.nio来管理连接。因此,所有连接都在一个单一的线程管理,而不是每一个线程。 NIO是极其有效的。
创建web sockets:
01 |
AsyncHttpClient.getDefaultInstance().websocket(get, "my-protocol" , new WebSocketConnectCallback() {
|
03 |
public void onCompleted(Exception ex, WebSocket webSocket) {
|
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);
|
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();
|
项目主页:http://www.open-open.com/lib/view/home/1371461635921