如何在Android SocketIO中使用HTTPS?

时间:2016-01-22 05:55:25

标签: android https socket.io

我已使用iPhone开发了socket.io个应用程序,它与HTTPHTTPS一起正常运行。

我已经使用Android开发了socket.io个应用程序。我正在使用HTTP它工作正常。现在我正在尝试使用HTTPS。我做了一些更改,但它根本没有与server连接。我总是得到websocket error

I downloaded the library from https://github.com/socketio/ for iPhone and Android 

以下是我的示例代码


try {
            HostnameVerifier verifier = new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    // Not doing any validation for testing
                    Log.i(TAG, "Verifying host name ::: " + hostname);
                    return true;
                }
            };
            IO.setDefaultSSLContext(SSLContext.getDefault());
            IO.setDefaultHostnameVerifier(verifier);

            IO.Options opts = new IO.Options();
            opts.reconnection = false;
            opts.secure = true;
            opts.transports = new String [] {WebSocket.NAME};

            socket = IO.socket("https://website.com:6001", opts);
            Log.i(TAG, "Initialized socket io");
            socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    Log.i(TAG, "We are connected to server");
                }
            });
            socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    Log.i(TAG, "We are disconnected from server");
                    socket.off();
                }
            });
            socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    Log.i(TAG, "Connection Error");
                    for (Object obj : args) {
                        Log.e(TAG, "Errors :: " + obj);
                    }
                }
            });
            socket.on(Socket.EVENT_CONNECT_TIMEOUT, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    Log.i(TAG, "Connection Timeout Error");
                }
            });
            socket.on("res", new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    Log.i(TAG, "got response from server");
                }
            });
            socket.connect();
        } catch (Exception e) {
            Log.e(TAG, "Error :: " + e);
        }

有什么想法吗?

0 个答案:

没有答案