Mahima MS
Mahima MS

Reputation: 153

SocketCluster - Unable to connect to server from Android app - Connection refused

I am trying to run the socketcluster-android-client demo app (https://github.com/sacOO7/socketcluster-android-demo). I have the server running at localhost:8000 successfully. I'm connecting to: ws://localhost:8000/socketcluster/ from my mobile.

When I try to run the app, the connection fails with the error:

Got connect error com.neovisionaries.ws.client.WebSocketException: Failed to connect to 'localhost:8000': failed to connect to localhost/127.0.0.1 (port 8000) from /127.0.0.1 (port 42405) after 5000ms: isConnected failed: ECONNREFUSED (Connection refused)

I've verified the following(from other SO answers):

Here's the code that tries to connect to the socket:

socket = new Socket(url);
socket.setListener(new BasicListener() {

    public void onConnected(Socket socket, Map<String, List<String>> headers) {
        socket.createChannel("MyClassroom").subscribe(new Ack() {
            @Override
            public void call(String name, Object error, Object data) {
                if (error==null){
                    Log.i ("Success","subscribed to channel "+name);
                }
            }
        });
        Log.i("Success ","Connected to endpoint");
    }

    public void onDisconnected(Socket socket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) {
        Log.i("Success ","Disconnected from end-point");
    }

    public void onConnectError(Socket socket,WebSocketException exception) {
        Log.i("Success ","Got connect error "+ exception);
    }

    public void onSetAuthToken(String token, Socket socket) {
        socket.setAuthToken(token);
    }

    public void onAuthentication(Socket socket,Boolean status) {
        if (status) {
            Log.i("Success ","socket is authenticated");
        } else {
            Log.i("Success ","Authentication is required (optional)");
        }
    }

});

socket.setReconnection(new ReconnectStrategy().setMaxAttempts(10).setDelay(3000));

socket.connectAsync();

Upvotes: 1

Views: 718

Answers (1)

Mahima MS
Mahima MS

Reputation: 153

I had to use the IP address of the PC in the URL. Replaced ws://localhost:8000/socketcluster/ with ws://<PC's IP address>:8000/socketcluster/.

Upvotes: 1

Related Questions