Android Socket.io-client server error Connecting

464 views Asked by At

this is my first time creating a chat using socket in Android app. I have a nodejs back-end. I also a have a front-end app written with Reactjs and it's working as expected. However, it's not the case for my Android app. Please see below code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_chatroom)

    send.setOnClickListener(this)
    leave.setOnClickListener(this)

    // Get the nickname and roomName from entrance activity.
    try {
        userName = intent.getStringExtra("username")!!
        roomName = intent.getStringExtra("room")!!
    } catch (e: Exception) {
        e.printStackTrace()
    }

    // Set ChatRoom RecyclerView adapter
    chatRoomAdapter = ChatRoomAdapter(this, chatList)
    recyclerView.adapter = chatRoomAdapter

    val layoutManager = LinearLayoutManager(this)
    recyclerView.layoutManager = layoutManager

    // Let's connect to our Chat room! :D
    try {
        val opts = IO.Options()
        opts.forceNew = true;
        opts.reconnection = false
        opts.transports = arrayOf(Polling.NAME, PollingXHR.NAME, WebSocket.NAME)
        // This address is the way you can connect to localhost with AVD (Android Virtual Device)
        mSocket = IO.socket(URL, opts)
        Log.d(TAG, "Name $URL")
        Log.d(TAG, "Opts $opts")
    } catch (e: Exception) {
        e.printStackTrace()
        Log.d("Fail", "" + "Failed to connect")
    }

    mSocket.connect()
    mSocket.on(Socket.EVENT_CONNECT, Emitter.Listener {
        val data = initialData(userName, roomName)
        val jsonData = gson.toJson(data) // Gson changes data object to Json type.
        Log.d(TAG, "json >> $jsonData")

        Handler(Looper.getMainLooper()).postDelayed(object : Runnable {
            override fun run() {
                mSocket.emit("join", jsonData)
            }
        },200)
        Log.d("Success", "" + mSocket.id())
        mSocket.on("message", onUpdateChat)
    });

    mSocket.on(Socket.EVENT_CONNECT_ERROR, Emitter.Listener { args ->
        Log.e(TAG, "Andito na ba ko? ${args[0]} size: ${args.size}")
    });

    mSocket.on(Socket.EVENT_ERROR, Emitter.Listener {
        Log.e(TAG, "Error")
    });


}

But I keep getting io.socket.engineio.client.EngineIOException: server error

Anyone encountered this error? I would gladly appreciate any kind of help. Thanks.

0

There are 0 answers