OlivierArgentieri
OlivierArgentieri

Reputation: 73

Unable to connect to a node.js socket.io 3.x server from Android socket.io 2 client

I'm trying to use socket.io client on android/kotlin with a node js socket io server

MainActivity:

import io.socket.emitter.Emitter
import io.socket.engineio.client.Socket


override fun onCreate(savedInstanceState: Bundle?) {
    //...
    val mSocket =  Socket("http://192.168.1.15:3000")
    mSocket.on(Socket.EVENT_ERROR, { print("error")})
    mSocket.on(Socket.EVENT_OPEN, Emitter.Listener() {
        mSocket.emit("mayaCommand", "cmds.polyCube()");
        mSocket.close()
        print("ok")
     )}
    mSocket.open()
    
    

Socketio library:

 implementation 'io.socket:engine.io-client:2.0.0'.

on github page of this library (here), version 2.0.0 is for 3.x server, so my nodejs socket.io server is on 3.0.3

"socket.io": "3.0.3",
"socket.io-client": "3.0.3"

nothing in the log, just not working, I have checked the network packet with Wireshark and I can see some packet.

I have create a network_security_config.xml :

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">http://192.168.1.15</domain>
    </domain-config>
</network-security-config> 

Link in my manifest :

 <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:networkSecurityConfig="@xml/network_security_config"
        android:usesCleartextTraffic="true"
       >

And not working, nothing is happening.

Thank you

Upvotes: 3

Views: 3604

Answers (1)

OlivierArgentieri
OlivierArgentieri

Reputation: 73

Ok, So today i have recreate the entire project, step by step with wireshark running on my other screen.

And spoil : it's working. i will detail all steps, in case of another person got the same issue.

Add this in gradle module file :

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

(2.0.0 is for socket.io 3.x server)

Edit the manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.socketdccs">
    <uses-permission android:name="android.permission.INTERNET"  /> <=== add perms
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true" <==== this is important
/...  

And my MainActivity :

package com.example.myapp
// ...
import io.socket.client.IO
import io.socket.client.Socket
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {



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

        val mSocket = IO.socket("http://192.168.1.15:3000")
        mSocket.on(Socket.EVENT_CONNECT, {println("connected")})
        testBtn.setOnClickListener {

            mSocket.connect()
            mSocket.emit("mayaCommand", "cmds.polyCube()")
            println("ok")
        }

    }
}

And now, it's working fine.

Thank you.

Upvotes: 4

Related Questions