美文网首页
WebSocket 第三方框架

WebSocket 第三方框架

作者: 苍天霸气诀 | 来源:发表于2019-04-12 15:10 被阅读0次

    1. AndroidAsyn

    GitHub地址:https://github.com/koush/AndroidAsync

    使用:

    Gradle:

    dependencies { compile 'com.koushikdutta.async:androidasync:2.+' }
    

    使用方式:

    AsyncHttpClient.getDefaultInstance().websocket(
                "ws://192.168.250.38:8181",// webSocket地址
                "8181",// 端口
                new AsyncHttpClient.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 WebSocket.StringCallback() {
                            public void onStringAvailable(String s) {
                                Log.i(TAG, "onStringAvailable: " + s);
                            }
                        });
                        webSocket.setDataCallback(new DataCallback() {
                            public void onDataAvailable(DataEmitter emitter, ByteBufferList byteBufferList) {
                                System.out.println("I got some bytes!");
                                // note that this data has been read
                                byteBufferList.recycle();
                            }
                        });
                    }
                });
    

    2.autobahn

    官网:http://autobahn.ws/android/,下到jar包放项目里面就好了

    使用:

     private WebSocketConnection mConnect = new WebSocketConnection();
        String url = "ws://192.168.250.38:8181/";
        public void init() {
            try {
                mConnect.connect(url, new WebSocketHandler() {
                    @Override
                    public void onOpen() {
                        Log.i(TAG, "onOpen: ");
                    }
                    @Override
                    public void onTextMessage(String payload) {
                        Log.i(TAG, "onTextMessage: "+payload);
                    }
                    @Override
                    public void onClose(int code, String reason) {
                        Log.i(TAG, "onClose: " + code + "|" + reason);
                    }
                });
            } catch (WebSocketException e) {
                e.printStackTrace();
            }
        }
    
    

    3.java-webSocket

    GitHub地址:https://github.com/TooTallNate/Java-WebSocket

    使用:

    
      private String address = "ws://192.168.250.38:8181/";
        private URI uri;
        private static final String TAG = "JavaWebSocket";
    
        public void initSockect() {
            try {
                uri = new URI(address);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            if (null == mWebSocketClient) {
                mWebSocketClient = new WebSocketClient(uri) {
                    @Override
                    public void onOpen(ServerHandshake serverHandshake) {
                        Log.i(TAG, "onOpen: ");
                    }
                    @Override
                    public void onMessage(String s) {
                        Log.i(TAG, "onMessage: " + s);
                    }
                    @Override
                    public void onClose(int i, String s, boolean b) {
                        Log.i(TAG, "onClose: ");
                    }
                    @Override
                    public void onError(Exception e) {
                        Log.i(TAG, "onError: ");
                    }
                };
                mWebSocketClient.connect();
            }
        }
    

    4.Netty

    官网:http://netty.io/

    参考

    知乎WebSocket原理:https://www.zhihu.com/question/20215561

    阮一峰WebSocket教程:http://www.ruanyifeng.com/blog/2017/05/websocket.html

    Java-WebSocket:https://github.com/TooTallNate/Java-WebSocket

    AndroidAsync:https://github.com/koush/AndroidAsync

    相关文章

      网友评论

          本文标题:WebSocket 第三方框架

          本文链接:https://www.haomeiwen.com/subject/qdpcwqtx.html