SadeQ digitALLife
SadeQ digitALLife

Reputation: 1444

how to use socket IO in kotlin?

I want to initialize socket IO in my kotlin app.

my problem is here :

    private var mSocket: Socket? = null
{
    try {
        mSocket = IO.socket("http://chat.socket.io")
    } catch (URISyntaxException e) {
    }
}

import com.github.nkzawa.socketio.client.IO

cant recognize

Upvotes: 7

Views: 17387

Answers (5)

Umesh Yadav
Umesh Yadav

Reputation: 1200

It's a static block in java But we can't wirte same as in Kotlin. We can use its like a companion object.

companion object{
    private var mSocket: Socket?=null
    init {
        try {
            mSocket = IO.socket(Constants.Chat_URl)
        }
        catch (e: Exception){
            throw RuntimeException(e)
        }
    }
}

Upvotes: 0

DreamyLynn
DreamyLynn

Reputation: 41

I searched for this some more and found this solution:

You connect your ws like this:

val opts = IO.Options()
opts.path = "/path/to/ws"
opts.transports = arrayOf(WebSocket.NAME) // Set the transfer to 'websocket' instead of 'polling'
val webSocket = IO.socket("http(s)://your.ip.here", opts)
webSocket.connect()
    .on(Socket.EVENT_CONNECT) {
         // Do your stuff here
    }
    .on("foo") { parameters -> // do something on recieving a 'foo' event
         // 'parameters' is an Array of all parameters you sent
         // Do your stuff here
     }

If you want to emit an event, you'll call:

webSocket.emit("foo", "bar") // Emits a 'foo' event with 'bar' as a parameter

You will need to use

import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;

so be sure to add the corresponding libraries to your build.gradle

dependencies {
    ...
    implementation 'com.github.nkzawa:socket.io-client:0.6.0'
}

Upvotes: 4

Mirella Rodriguez
Mirella Rodriguez

Reputation: 1

first import this

import com.github.nkzawa.socketio.client.IO;

import com.github.nkzawa.socketio.client.Socket;

and then initialize this one

val socket = IO.socket("http://localhost:4000/")

socket.on(Socket.EVENT_CONNECT, Emitter.Listener {
    socket.emit("messages", "hi")
});
socket.connect()

Upvotes: 0

Gstuntz
Gstuntz

Reputation: 453

The right syntax is below for anyone who is interested in the future

private lateinit var mSocket:Socket

fun socket(){
    try {
        mSocket=IO.socket("http://host:port")
    }
    catch(e: URISyntaxException){
        println("Exception"+e)
    }


}

Upvotes: -1

Hanny
Hanny

Reputation: 1342

In Kotlin you can make a Socket Client like the following. All the Exceptions are handled here too.

fun pingYourTCPServerWith(message: String): String{
    try {
        val socket = Socket("<YOUR IP ADDRESS>", <YOUR PORT HERE>)
        socket.use {

            var responseString : String? = null

            it.getOutputStream().write(message.toByteArray())
            val bufferReader = BufferedReader(InputStreamReader(it.inputStream))
            while (true) {
                val line = bufferReader.readLine() ?: break
                responseString += line
                if (line == "exit") break
            }
            println("Received: $responseString")
            bufferReader.close()
            it.close()
            return responseString!!
        }
    }catch (he: UnknownHostException){
        val exceptionString = "An exception occurred:\n ${he.printStackTrace()}"
        return   exceptionString
    }catch (ioe: IOException){
        val exceptionString = "An exception occurred:\n ${ioe.printStackTrace()}"
        return   exceptionString
    } catch (ce: ConnectException){
        val exceptionString = "An exception occurred:\n ${ce.printStackTrace()}"
        return   exceptionString
    }catch (se: SocketException){
        val exceptionString = "An exception occurred:\n ${se.printStackTrace()}"
        return   exceptionString
    }
}

Upvotes: -1

Related Questions