I got an issue with 2.1.0 socket version in android

Viewed 73

How can make Socket.IO works on android client side

I wrote this code in build.gradle

implementation('io.socket:socket.io-client:2.1.0') {
    exclude group: 'org.json', module: 'json'
}

and here's my code in fragment, this code not work for me, how can I resolve it

val opts = IO.Options()
opts.forceNew = true
opts.reconnection = false
val mSocket = IO.socket(SOCKET_URL, opts).connect()
mSocket?.connect()

mSocket?.emit("join", Gson().toJson(data))
mSocket?.on("newuser", newUser)
1 Answers

I am also using this library but at my end its working fine. Below is the code that I used:

implementation ('io.socket:socket.io-client:2.0.0') {
    // excluding org.json which is provided by Android
    exclude group: 'org.json', module: 'json'
}

val options = IO.Options()
options.forceNew = false
options.reconnection = true
options.transports = arrayOf(WebSocket.NAME)
options.query = "userId=" + 
    Objects.requireNonNull(userRepository.getUser()?.user?.id)

try {
    mSocket = IO.socket(Config.BASE_URL_DEV, options)
} catch (e: URISyntaxException) {
    e.printStackTrace()
}

connect()

mSocket?.on(Socket.EVENT_CONNECT, onConnect)

mSocket?.on(Socket.EVENT_CONNECT_ERROR, onError)

fun connect() {
    mSocket?.let {
        if (!it.connected()) it.connect()
    }
}

private val onConnect = Emitter.Listener {
    Timber.e("AppSocket - onConnect called")
}

If its not working at your end check url in any online socket related website to ensure whether problem is at your end or not

Related