套接字io客户端Android中的打开握手响应为空

时间:2017-09-18 10:01:35

标签: android node.js websocket socket.io java-websocket

我正在为Android实施 nv-websocket-client 。我在Log中遇到错误。如何成功连接?

com.neovisionaries.ws.client.WebSocketException: The status line of the opening handshake response is empty.
09-18 15:37:59.532 14225-14248/info.androidhive.firebasenotifications W/System.err:     at com.neovisionaries.ws.client.HandshakeReader.readStatusLine(HandshakeReader.java:99)
09-18 15:37:59.532 14225-14248/info.androidhive.firebasenotifications W/System.err:     at com.neovisionaries.ws.client.HandshakeReader.readHandshake(HandshakeReader.java:48)
09-18 15:37:59.532 14225-14248/info.androidhive.firebasenotifications W/System.err:     at com.neovisionaries.ws.client.WebSocket.readHandshake(WebSocket.java:3244)
09-18 15:37:59.532 14225-14248/info.androidhive.firebasenotifications W/System.err:     at com.neovisionaries.ws.client.WebSocket.shakeHands(WebSocket.java:3123)
09-18 15:37:59.532 14225-14248/info.androidhive.firebasenotifications W/System.err:     at com.neovisionaries.ws.client.WebSocket.connect(WebSocket.java:2155)

我的服务器端代码在Socket.js中。有什么我想念的吗?

var app = require('express')();
var http = require('http').Server(app);

var io = require('socket.io')(http);


io.on('connection', function(socket){
console.log('client connected');
    let jsonObject = {
        name: 'shihab',
        age : 28
    };

    io.emit('test-event', jsonObject);
    console.log(jsonObject);
});


http.listen(3000, function(){
    console.log('listenting on port 3000');
});

&安培;我正在我的Activity中实现这个。

    @Override
    protected void onResume() {

        super.onResume();
//        CommonTasks.handleSocket();
        initSocket();
    }


    private void initSocket() {

        try {
            // Connect to the server and perform an opening handshake.
            // This method blocks until the opening handshake is finished.
            WebSocketFactory webSocketFactory = new WebSocketFactory();

            ws = webSocketFactory.setConnectionTimeout(5000).
                    createSocket(SOCKET_URL, 5000);

            ws.addListener(new WebSocketAdapter() {
                @Override
                public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception {


                }

                @Override
                public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {

                    Log.e("onConnected", "onConnected");
                }

                @Override
                public void onConnectError(WebSocket websocket, WebSocketException cause) throws Exception {
                    Log.e("onConnectError", "onConnectError");

                }

                @Override
                public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
                    Log.e("onDisconnected", "onDisconnected");

                }

                @Override
                public void onTextMessage(WebSocket websocket, String text) throws Exception {

                }


                @Override
                public void onError(WebSocket websocket, WebSocketException cause) throws Exception {

                }


                @Override
                public void onMessageError(WebSocket websocket, WebSocketException cause, List<WebSocketFrame> frames) throws Exception {

                }


            });
            new SocketTask().execute("");
            Log.e("called", "called");

        } catch (IOException e) {

        }

    } 


    class SocketTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            try {
                ws.connect();

            } catch (OpeningHandshakeException e) {
                // Get the status code.
                int statusCode = e.getStatusLine().getStatusCode();
                Log.e("OpeningHandshakeExcep", "Status code..." + statusCode);

                // If the status code is in the range of 300 to 399.
                if (300 <= statusCode && statusCode <= 399) {
                    // Location header should hold the redirection URL.
                    String location = e.getHeaders().get("Location").get(0);
                }
            } catch (HostnameUnverifiedException e) {
                e.printStackTrace();

                // The certificate of the peer does not match the expected hostname.
            } catch (WebSocketException e) {
                e.printStackTrace();
                // Failed to establish a WebSocket connection.
            } catch (Exception e6) {
                showLog(CommonConstraints.SOCKET_TAG, e6.getMessage());
                Application.getInstance().trackException(e6);
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

        }

    }

有人这样做吗?可以帮我解决这个问题。

0 个答案:

没有答案