socket.io-android-chat icon indicating copy to clipboard operation
socket.io-android-chat copied to clipboard

Message can send but not receive

Open Slake07 opened this issue 7 years ago • 3 comments

I am working on chat app using this library.

its working fine with two browsers but when i connect one browser and one android device. that time, i can send message from android to browser and browser also receive it. but i can't receive message from browser to android. i checked all the events and all are correct. but still not working.below is my code for that.

fun initializeSocket() {
    try {
        val application = application as Application
        mSocket = application.getSocket()
        mSocket?.on(Socket.EVENT_CONNECT, onConnect)
        mSocket?.on(Socket.EVENT_DISCONNECT, onDisconnect)
        mSocket?.on(Socket.EVENT_CONNECT_ERROR, onConnectError)
        mSocket?.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError)
        mSocket?.on(Constant.KEY_SOCKET_JOIN_ROOM, onJoinRoom)
        mSocket?.on(Constant.KEY_SOCKET_REC_MSG, onReceivemsg)
        mSocket?.on(Constant.KEY_SOCKET_READ_MSG, onReadmsg)

        mSocket?.connect()
    } catch (e: NullPointerException) {
        e.printStackTrace()
    } catch (e: Exception) {
        e.printStackTrace()
    }

}


private val onConnect = object : Emitter.Listener {
    override fun call(vararg args: Any) {
        runOnUiThread(Runnable {
            Log.e("FAQ", "Connected")
            if (isConnected == false) {

                joinRoom()
                isConnected = true
            }
        })

    }
}

private val onDisconnect = object : Emitter.Listener {
    override fun call(vararg args: Any) {
        runOnUiThread(Runnable {
            Log.e("FAQ", "Disconnected")
            isConnected = false
        })

    }
}

private val onConnectError = object : Emitter.Listener {
    override fun call(vararg args: Any) {
        runOnUiThread(Runnable {
            Log.e("FAQ", "Error connecting: ")
            isConnected = false
        })

    }
}

private val onJoinRoom = object : Emitter.Listener {
    override fun call(vararg args: Any) {
        try {
            val data = args[0] as JSONObject
            Log.e("Join Room res", data.toString())
        } catch (e: JSONException) {
            e.printStackTrace()
        }

    }
}

private val onReceivemsg = object : Emitter.Listener {
    override fun call(vararg args: Any) {
        Log.e("Receive Msg res", "OnReceive")
        runOnUiThread(Runnable {
            try {
                val data = args[0] as JSONObject
                Log.e("Receive Msg res", data.toString())

            } catch (e: JSONException) {
                e.printStackTrace()
            }
        })


    }
}

override fun onDestroy() {
    super.onDestroy()

    mSocket?.disconnect()

    mSocket?.off(Socket.EVENT_CONNECT, onConnect)
    mSocket?.off(Socket.EVENT_DISCONNECT, onDisconnect)
    mSocket?.off(Socket.EVENT_CONNECT_ERROR, onConnectError)
    mSocket?.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError)
    mSocket?.off(Constant.KEY_SOCKET_JOIN_ROOM, onJoinRoom)
    mSocket?.off(Constant.KEY_SOCKET_REC_MSG, onReceivemsg)
    mSocket?.off(Constant.KEY_SOCKET_READ_MSG, onReadmsg)

}

Slake07 avatar Sep 27 '17 04:09 Slake07

I have the same issue. I could connect to server, could emit data, but can't handle server events on Android. Socket io and all callbacks are setup properly. Any help here?

olmur avatar Sep 27 '17 12:09 olmur

@olmur i solved my issue with removing joinRoom from connect listener to initializeSocket method.

Slake07 avatar Sep 30 '17 02:09 Slake07

@olmur Same problem !!! I moved to another lib: https://github.com/socketio/socket.io-client-java

decodedmrq avatar Jul 10 '19 09:07 decodedmrq