nkzawa / socket.io-android-chat

A simple chat demo for socket.io and Android
MIT License
2k stars 690 forks source link

Kotlin Socket-iO not connecting #77

Open RpeshB opened 6 years ago

RpeshB commented 6 years ago

Unable to connect to server, I have to 2 Que 1) I am using Kotlin v 1.2.21 with Dagger2, Retofit2, Okhttp3,RXJava library please let me know which is the best way to implement Socket IO ? 2) After so much try unable to connect to server, So i decide to make sample project sokcet IO connection but still unable to connect please check sample code (Using Https, web Project working fine prob with only android ) `package com.easymakers.myapplication

import android.os.Bundle import android.support.design.widget.Snackbar import android.support.v7.app.AppCompatActivity import android.util.Log import android.view.Menu import android.view.MenuItem import com.github.nkzawa.emitter.Emitter import com.github.nkzawa.socketio.client.IO import com.github.nkzawa.socketio.client.Socket

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

class MainActivity : AppCompatActivity() {

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

    fab.setOnClickListener { view ->
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        //connect()
        connect1()
    }
}

 private fun connect() {
    val socket = IO.socket("https://192.168.1.23:5000")
    socket.connect()
            .on(Socket.EVENT_CONNECT, {
                println("connected")
            })
            .on(Socket.EVENT_DISCONNECT, { println("disconnected") })
}

private fun connect1(){
    val sc = SSLContext.getInstance("SSL")
    sc.init(null, null, null)
    val opts = IO.Options()
    opts.secure = true
    opts.forceNew = true
    opts.reconnection = true
    val  socket = IO.socket("https://192.168.1.23:5000",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()
}

} Usingimplementation 'com.github.nkzawa:socket.io-client:0.5.2'` If any one can help me

gaurishanand13 commented 4 years ago

i am having the same problem, IO.socket("https://192.168.1.23:5000") is not connecting. how did you solve it?

CarminatiGiovanni commented 3 years ago

I'm on the same point I can't connect to the server