Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: kotlin not supporting Socket IO in android

Rupesh:

I have two questions

1)My Project contain dagger2,retofit2,kotlin v1.0.21,rxJava2,OkHttp3 i want to implement SocketIO on my project how should i implement?

2) I try to several way but unable to connect socketIO so i try to sample code below given code but still unable to connect socket.. please help thanx in advance


package com.easymakers.myapplication

import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.view.View
import io.socket.client.IO
import io.socket.client.Socket
import io.socket.emitter.Emitter

import kotlinx.android.synthetic.main.activity_main.*
import javax.net.ssl.SSLContext


class MainActivity : AppCompatActivity() {
private var socket : Socket? = null

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

fab.setOnClickListener { view ->

connect(view)
// connect1()
}
}

private fun connect(view : View) {
val opts = IO.Options()
opts.port= 5000
opts.reconnection = false
// opts.query =
socket = IO.socket("https://192.170.1.21",opts)
socket?.connect()
?.on(Socket.EVENT_CONNECT, {
Snackbar.make(view, "Socket connected", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
})
?.on(Socket.EVENT_DISCONNECT, { println("disconnected") })
}

private fun connect1(){
val sc = SSLContext.getInstance("SSL")
sc.init(null, null, null)
val opts = IO.Options()
opts.port = 3000
opts.secure = true
opts.forceNew = true
opts.reconnection = true
val socket = IO.socket("https://103.69.190.10",opts)
socket.on("connection", Emitter.Listener {
println("Connected")
socket.emit("foo", "hi")
socket.disconnect()
}).on("event", Emitter.Listener { }).on(Socket.EVENT_DISCONNECT, Emitter.Listener { })
socket.connect()
}
}



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: kotlin not supporting Socket IO in android

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×