SocketCluster - 无法连接到从Android应用程序服务器 - 连接被拒绝

问题描述 投票:0回答:1

我试图运行socketcluster功能的Android客户端的演示应用程序(https://github.com/sacOO7/socketcluster-android-demo)。我有服务器在本地主机上运行:8000成功。我连接到:ws://localhost:8000/socketcluster/从我的手机。

当我尝试运行应用程序,连接失败,出现错误:

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)

我已经验证以下(从其他等答案):

  • 防火墙访问
  • 成功的远程登录到本地主机:8000
  • PC上运行中的服务器和我的手机是同一WiFi网络
  • 升级到最新版本的节点

下面是尝试连接到插座上的代码:

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();
java android node.js websocket socketcluster
1个回答
1
投票

我曾在URL中使用的PC的IP地址。更换ws://localhost:8000/socketcluster/ws://<PC's IP address>:8000/socketcluster/

© www.soinside.com 2019 - 2024. All rights reserved.