Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io client in android connection with HTTPS protocol failed?

io on server and client on android.

It results in connection error on android as long as I enable HTTP(S) SSL(works fine if disable it however)

I've tried to implement HTTPS connection on Android, took reference from sample on Github as following:

opts = new IO.Options();
opts.sslContext = mySSLContext;
opts.hostnameVerifier = myHostnameVerifier;
socket = IO.socket("https://mychat.url", opts);

also this

SSLContext mySSLContext = SSLContext.getInstance("TLS");
mySSLContext.init(null, null, null);

and this

myHostnameVerifier = new HostnameVerifier() {
    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
});

Still got error message during socket transport

io.socket.engineio.client.EngineIOException: xhr poll error

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

What does it take to fulfill socket io connection with HTTPS protocol?

like image 894
Dayo Choul Avatar asked Jun 15 '16 05:06

Dayo Choul


People also ask

Does Socket.IO work on Android?

Now we can use Socket.IO on Android!

Is Socket.IO over HTTP?

Even when websockets can be used, the initial connection setup it done over HTTP. Also, a socket.io server will attach to an HTTP server so it can serve its own client code through /socket.io/socket.io.js .

Why can't I connect to the socket Io server?

Please make sure the Socket.IO server is actually reachable at the given URL. You can test it with: If that's not the case, please check that the Socket.IO server is running, and that there is nothing in between that prevents the connection. You can test it with rejectUnauthorized set to false.

What is the HTTP response status of socket Io?

The Socket.IO server may return the following HTTP status: 200 OK: when in HTTP long-polling mode ( GET for reading, POST for writing) In case of an HTTP 400 response, the response payload will be one of the following: The transport query parameter is missing or invalid.

Can I use the socket Io client as a background service?

The Socket.IO client is not meant to be used in a background service, as it will keep an open TCP connection to the server and quickly drain the battery of your users. It is totally usable in the foreground though.

What is the issue number for socketio client?

· Issue #1233 · socketio/socket.io-client · GitHub Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.


1 Answers

Thanks to @Apurva I resolve it on my own.

Seems that TrustManager is necessary to initialize sslContext

so I added

mySSLContext.init(null, trustAllCerts, null);

with parameter trustAllCerts refer to

private final TrustManager[] trustAllCerts= new TrustManager[] { new X509TrustManager() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return new java.security.cert.X509Certificate[] {};
    }

    public void checkClientTrusted(X509Certificate[] chain,
                                   String authType) throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] chain,
                                   String authType) throws CertificateException {
    }
} };

And finally connected to chat server successfully.

like image 185
Dayo Choul Avatar answered Sep 20 '22 19:09

Dayo Choul