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

标签 java android node.js websocket socketcluster

我正在尝试运行 socketcluster-android-client 演示应用程序 (https://github.com/sacOO7/socketcluster-android-demo)。我的服务器在 localhost: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)

我已经验证了以下内容(来自其他 SO 答案):

  • 防火墙访问权限
  • 成功远程登录到 localhost:8000
  • 正在运行的服务器和我的手机在同一个wifi网络上的PC
  • 升级到最新的 Node 版本

这是尝试连接到套接字的代码:

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();

最佳答案

我必须在 URL 中使用 PC 的 IP 地址。已替换 ws://localhost:8000/socketcluster/ws://<PC's IP address>:8000/socketcluster/ .

关于java - SocketCluster - 无法从 Android 应用程序连接到服务器 - 连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54477290/

相关文章:

java - Spring Context 配置需要将 ServletContext 传递给构造函数

java - ScheduleAtFixedRate 的准确性是多少?

android - 如何在通知中的按钮上添加监听器(自定义布局)

android - 当前位置/位置的标准图标

android - 仅适用于 Android 4.0.3-4.0.4 的某些设备上 TabHost 上的应用程序崩溃

node.js - 部署 Typescript NodeJS 服务器

java - Java中的随机整数

java - 使用 @ManyToMany 和 @ManyToOne 关系创建表

node.js - nodejs child_process.spawn return throw er;//未处理的 'error'事件

node.js - Sequelize : M2M Filter entities that don't have a tag out