Giter Site home page Giter Site logo

Comments (9)

ben-hudson avatar ben-hudson commented on August 16, 2024 1
private Socket socket;

@Override
protected void onCreate(Bundle savedInstanceState) {

    try {
        socket = IO.socket("http://tyr-einsteinium.rhcloud.com/");
        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                Log.d(getClass().getCanonicalName(), "Connected to server");
            }

        }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... arg0) {
                Log.d(getClass().getCanonicalName(), "Disconnected from server");
            }

        });
        socket.connect();
    } catch (URISyntaxException e) {
        Log.e(getClass().getCanonicalName(), e.toString());
    }
}

@Override
protected void onDestroy() {
    socket.disconnect();
    super.onDestroy();
}

from socket.io-client-java.

FaizVisram avatar FaizVisram commented on August 16, 2024

It's working fine for me. Where are you calling the socket connection? Can you post the relevant code you used to create the socket?

from socket.io-client-java.

FaizVisram avatar FaizVisram commented on August 16, 2024

What kind of class is this? Application, Activity, Fragment?

from socket.io-client-java.

ben-hudson avatar ben-hudson commented on August 16, 2024

Its an ActionBarActivity

from socket.io-client-java.

nkzawa avatar nkzawa commented on August 16, 2024

It may be the same problem with #21.
What happens if you set forceNew option true?

from socket.io-client-java.

ben-hudson avatar ben-hudson commented on August 16, 2024

forceNew works for me. Is there anything I need to do in addition to this? Or is the cleanup the same?

from socket.io-client-java.

nkzawa avatar nkzawa commented on August 16, 2024

forceNew option forces to create a new connection every time, it can cause a performance problem.
So it might be better to use Manager directly instead of IO.socket.

manager = new Manager("http://localhost:3000", options);

socket1 = manager.socket("/foo");
socket2 = manager.socket("/bar");

Manager represents a connection, and IO.socket is just a convenient wrapper of it.
Please see the source code for the details.

https://github.com/nkzawa/socket.io-client.java/blob/master/src/main/java/com/github/nkzawa/socketio/client/IO.java#L52

from socket.io-client-java.

ben-hudson avatar ben-hudson commented on August 16, 2024

So basically by using the manager directly I avoid creating a new one each time.

Got it working! Thanks and great library :)

from socket.io-client-java.

KimmiDhingra1991 avatar KimmiDhingra1991 commented on August 16, 2024

I was also facing the same problem.
You should disconnect the Socket in "ondestroy" method
Example:
onCreate(Bundle savedInstanceState){
Socket.on("foo");
}

onDestroy(){
Socket.off("foo");
}

from socket.io-client-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.