socketio / engine.io-client-java

Engine.IO Client Library for Java
http://socketio.github.io/engine.io-client-java
Other
360 stars 167 forks source link

Can i use this client to connect Engine.IO Java Server #102

Closed matrixcloud closed 5 years ago

matrixcloud commented 5 years ago

Hi,

I tried to use this client to connect Engine.IO Java Server but got failed. The following is my server version. I wonder whether this client implementation is not compatible with the latest engine.io server.

  <dependency>
    <groupId>io.socket</groupId>
    <artifactId>engine.io-server</artifactId>
    <version>1.3.1</version>
  </dependency>

Thanks

matrixcloud commented 5 years ago

Since I use SSL and the option is complex. I did another test, it connected. I share my code below.

/**
 * Created by atom on 5/6/2019.
 */
public class Main {
    public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException {
        Socket.Options opts = new Socket.Options();
        OkHttpClient client = createHttpClient();
        opts.callFactory = client;
        opts.webSocketFactory = client;
        opts.host = "localhost";
        opts.port = 8443;
        opts.path = "/engine.io";
        opts.secure = true;
        Socket socket = new Socket(opts);

        socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                System.out.println("Connected");
            }
        });
        socket.on(Socket.EVENT_ERROR, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                System.out.println("errors: " + args[0]);
            }
        });

        socket.open();
    }

    public static OkHttpClient createHttpClient() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = SSLContext.getInstance("TLS");
        TrustManager[] trustAllCerts = new TrustManager[]{new DefaultTruststoreManger()};
        sslContext.init(null, trustAllCerts, null);

        OkHttpClient client = new OkHttpClient.Builder()
                .hostnameVerifier(new AllHostAcceptedVerifier())
                .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0])
                .build();
        return client;
    }

    /** The X509TrustManager implementation.
     */
    public static class DefaultTruststoreManger implements X509TrustManager {
        public static final String DISABLE_CERT_VALIDATION = "ignoreServerCertValidation";

        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];  //To change body of implemented methods use File | Settings | File Templates.
        }
    }

    /**
     * Disables host name verfication (used in case of NAT networks)
     */
    public static class AllHostAcceptedVerifier implements HostnameVerifier {
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }
    }
}