Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to a socket.io namespace using the socket.io-client.java library

I can connect to socket.io web service no problem using this library https://github.com/nkzawa/socket.io-client.java . I cannot quite figure out how to connect to a specific namespace though. I have looked through the test code and think I might need to create a Manager class.

Any help would be appreciated. Thank you very much.

like image 546
patrick_corrigan Avatar asked Jul 19 '15 18:07

patrick_corrigan


People also ask

How do you check if Socket.IO client is connected or not?

You can check the socket. connected property: var socket = io. connect(); console.

What is the difference between Socket.IO and Socket.IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).

What is Socket.IO client?

Socket.IO is a library that enables real-time, bidirectional and event-based communication between the browser and the server. It consists of: a Node. js server: Source | API. a Javascript client library for the browser (which can be also run from Node.


2 Answers

Manager manager = new Manager(new URI("http://socket.com"));
Socket socket = manager.socket("/my-namespace");
socket.connect();
like image 149
patrick_corrigan Avatar answered Sep 30 '22 19:09

patrick_corrigan


The above answer is not working for me.

Actually this code works, please use latest version of socket.io client version : https://github.com/socketio/socket.io-client-java

Add this in your build.graddle :

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

Then you can connect to your namespace with this snippet :

Socket socket;
try {
        socket = IO.socket(socket_host + "/your_namespace");
    } catch (URISyntaxException e) {
        Log.d("ERROR :", e.toString());
    }
socket.connect();

Check this github issue where there are more explanation : https://github.com/nkzawa/socket.io-android-chat/issues/8

like image 40
Raj Avatar answered Sep 30 '22 18:09

Raj