0

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()
    }
}
Dhruv Tyagi
  • 814
  • 1
  • 9
  • 28
Rupesh
  • 51
  • 2
  • 10

2 Answers2

3

I used this library Socket.IO

With this code it was enough on method OnCreate. Even with an authentication token, but it's optional.

val opts = IO.Options()
    opts.query = "token=XXXXXXXXX"
    var socket = IO.socket("https://MYSERVERSOCKET.com", opts)

    socket?.on(Socket.EVENT_CONNECT, {
          Log.d("","==============================CONNECTED")
    })?.on(Socket.EVENT_DISCONNECT, {
          Log.d("","==============================OFF")
    })

Start the connection

btnFindNext.setOnClickListener{
        socket.connect()
}

and an example of emit

btnOtro.setOnClickListener{

        val obj = JSONObject()
        obj.put("var1", "5ab0a8931522f51fac37f5a1")
        obj.put("var2", "91.8392894")
        obj.put("var3", "-93.8392894")

        socket.emit("SendData", obj)
    }
Luis Moreno
  • 615
  • 6
  • 6
2

1.1) https://github.com/socketio/socket.io-client-java

Gradle

Add it as a gradle dependency for Android Studio, in build.gradle:

compile ('io.socket:socket.io-client:1.0.0') {
  // excluding org.json which is provided by Android
  exclude group: 'org.json', module: 'json'
}

Socket.IO Server 1.x suppport

The current version of socket.io-client-java doesn't support socket.io server 1.x. Please use socket.io-client-java 0.9.x for that instead.

1.2) in github you may see: enter image description here this link follow you to the updated version which support 1.x and upper server versions. link: http://socketio.github.io/socket.io-client-java/project-summary.html

it have next dependency:

compile 'io.socket:socket.io-client:1.0.0-SNAPSHOT'

2) https://socket.io/get-started/chat/

Next step you must start node.js server

Integrating Socket.IO is composed of two parts:

1) A server that integrates with (or mounts on) the Node.JS HTTP Server: socket.io

2) A client library that loads on the browser side: socket.io-client During development, socket.io serves the client automatically for us, as we’ll see, so for now we only have to install one module:

npm install --save socket.io
no_fate
  • 1,625
  • 14
  • 22
  • using that only... forget to add over here – Rupesh Feb 07 '18 at 08:28
  • Thanx for replay we successfully implemented this into web, (no issue with nodejs) problem with Android only , i think this issue come in iOS.. do u have different approach ? or i doing something wrong – Rupesh Feb 07 '18 at 10:19
  • We have already implemented socket.io on NodeJS server express app & also referenced socket.io in our web project. And able to connect server socket. But when we use same approach as kotlin socket.io client to connect server socket.io, no connection is establishing between client & server. Not even connect event firing on server side. – Rupesh Feb 07 '18 at 10:34
  • what version of socket.io do you have on server and in client? – no_fate Feb 07 '18 at 10:40
  • @Rupesh, i am answer about versions because "The current version of socket.io-client-java doesn't support socket.io server 1.x. Please use socket.io-client-java 0.9.x for that instead." – no_fate Feb 07 '18 at 10:42
  • Android Client side we are using 'io.socket:socket.io-client:1.0.0' Sever side "socket.io": "^2.0.3" , Web client used 2.1.2 – Rupesh Feb 07 '18 at 10:59
  • @Rupesh, i am asking, i am sorry, not answer) – no_fate Feb 07 '18 at 11:01
  • @Rupesh, i am found new version of socket.io client, pls check: http://socketio.github.io/socket.io-client-java/project-info.html so in this version in gradle use: compile 'io.socket:socket.io-client:1.0.0-SNAPSHOT' – no_fate Feb 07 '18 at 11:11
  • i try this version but sill facing this issueError:(33, 20) Failed to resolve: io.socket:socket.io-client:1.0.0-SNAPSHOT Show in File
    Show in Project Structure dialog add plugin mavenCentral()
    – Rupesh Feb 07 '18 at 11:21
  • @Rupesh, add firstly in application's settings.gradle such a: "include 'io.socket:socket.io-client:1.0.0-SNAPSHOT'" and then: "compile project('io.socket:socket.io-client:1.0.0-SNAPSHOT') " OR add this "repositories { mavenCentral() }" – no_fate Feb 07 '18 at 11:24
  • i am sorry to bother u, but i already add this into build.gradle , how we suppose to add into setting.gradle – Rupesh Feb 07 '18 at 11:28
  • @Rupesh, https://stackoverflow.com/questions/16718026/how-to-build-an-android-library-with-android-studio-and-gradle and: https://stackoverflow.com/questions/28493470/gradle-failed-to-resolve-library-in-android-studio – no_fate Feb 07 '18 at 11:35
  • not working , if u have Sample code in kotlin please do share – Rupesh Feb 07 '18 at 12:25
  • same issue io.socket:socket.io-client:1.0.0-SNAPSHOT this will not working mavenCental() and even with particular repository download URL also... – Rupesh Feb 07 '18 at 12:46
  • please upload your build.gradle and full error message – no_fate Feb 07 '18 at 12:51