KokoBand
KokoBand

Reputation: 229

How to use Socket.IO in android kotlin

In my application i want use Socket.io and for this i add below library and write below codes.
But when run application and click on button not show me any event!
I used kotlin for write android application.

After click on button, should show me socket state in textView, but not show any state!

Socket library :

compile 'com.github.nkzawa:socket.io-client:0.5.2'

My Codes:

class SocketActivity : AppCompatActivity() {

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

        val opts = IO.Options()
        opts.query = "token=${Constants.BIDZILA_TOKEN}"
        var socket = IO.socket(Constants.BIDZILA_SOCKET, opts)

        btnSend.setOnClickListener {
            socket.connect()

            Handler(Looper.getMainLooper()).postDelayed(
                { socket?.on(Socket.EVENT_CONNECT) {
                    Log.d("SocketLog", "==============================CONNECTED")
                    socket_stateTxt.text = socket.connected().toString()
                }?.on(Socket.EVENT_DISCONNECT) {
                    Log.d("SocketLog", "==============================OFF")
                    socket_stateTxt.text = socket.connected().toString()
                } },
                2000
            )
        }
    }
}

How can i fix it?

Upvotes: 1

Views: 6822

Answers (1)

FedeH
FedeH

Reputation: 1363

I'm establishing the connection like this:

private var socket = IO.socket("<YOUR_URL>")
                socket.let {
                    it!!.connect()
                    .on(Socket.EVENT_CONNECT) {
                        Log.d("SignallingClient", "Socket connected!!!!!")
                    }
}

Upvotes: 4

Related Questions