socketio / socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
https://socketio.github.io/socket.io-client-java/installation.html
Other
5.34k stars 975 forks source link

Connection not established #625

Closed ntpanchal closed 2 years ago

ntpanchal commented 4 years ago

I have read the ReadMe file and according to that used library version 0.9.0

val opts = IO.Options()
opts.transports = arrayOf(WebSocket.NAME)
mSocket = IO.socket("Domain URL", opts)!!

I have also added Trust Manager to okhttpclient using the following code using this

val trustManagerFactory: TrustManagerFactory = TrustManagerFactory.getInstance(
                TrustManagerFactory.getDefaultAlgorithm()
            )
trustManagerFactory.init(null as KeyStore?)
val trustManagers: Array<TrustManager> = trustManagerFactory.trustManagers
val trustManager = trustManagers[0] as X509TrustManager

val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, arrayOf<TrustManager>(trustManager), null)
val sslSocketFactory = sslContext.socketFactory

val okHttpClient = OkHttpClient.Builder()
                .hostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier())
                .sslSocketFactory(
                    sslSocketFactory,
                    trustManager
                )
                .connectTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(1, TimeUnit.MINUTES)
                .readTimeout(1, TimeUnit.MINUTES)
                .build()
IO.setDefaultOkHttpCallFactory(okHttpClient)
IO.setDefaultOkHttpWebSocketFactory(okHttpClient)

How to overcome this issue any idea?

CoolMind commented 4 years ago

Use transports = arrayOf(Polling.NAME, PollingXHR.NAME, WebSocket.NAME).

Also in my case:

var socket: Socket? = null
    private set
private var hasError = false

// Better use in a background thread.
fun initSocket(): Socket? {
    if (socket == null && !hasError) {
        val options = getOptions()
        socket = try {
            IO.socket(SOCKET_URL, options)
        } catch (e: RuntimeException) {
            // java.lang.RuntimeException: java.net.MalformedURLException: For input string: "...".
            hasError = true
            null
        } catch (e: URISyntaxException) {
            hasError = true
            null
        }
    }
    return socket
}

fun connect() {
    socket?.apply {
        if (!connected()) {
            connect()
        }
    }
}

fun disconnect() {
    socket?.apply {
        if (connected()) {
            disconnect()
        }
    }
}

private fun getOptions(): IO.Options {
    // Fixing the error "io.socket.engineio.client.EngineIOException: xhr poll error".
    IO.setDefaultOkHttpWebSocketFactory(okHttpClient)
    IO.setDefaultOkHttpCallFactory(okHttpClient)
    return IO.Options().apply {
        forceNew = true
        reconnectionAttempts = Integer.MAX_VALUE;
        reconnection = true
        secure = true
        timeout = 1000
        transports = arrayOf(Polling.NAME, PollingXHR.NAME, WebSocket.NAME)
        query = "..." // For authorization.
    }
}

Don't forget to connect to the socket after initialization. To set okHttpClient see https://stackoverflow.com/a/60507560/2914140.

darrachequesne commented 2 years ago

Closed due to inactivity, please reopen if needed.