首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SocketCluster -无法从Android应用程序连接到服务器-拒绝连接

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

Stack Overflow用户
提问于 2019-02-01 10:13:26
回答 1查看 591关注 0票数 1

我正在尝试运行Socket集群-android演示应用程序(https://github.com/sacOO7/socketcluster-android-demo)。我让服务器在localhost上运行:8000成功。我正在从我的手机连接到:ws://localhost:8000/socketcluster/

当我尝试运行应用程序时,连接失败,错误如下:

代码语言:javascript
复制
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)

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

  • 防火墙访问
  • 成功的telnet到本地主机:8000
  • 在PC上运行的服务器和我的手机都在同一个wifi网络上。
  • 升级到最新的节点版本

下面是试图连接到套接字的代码:

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

发布于 2019-02-04 08:55:40

我不得不在URL中使用PC的IP地址。将ws://localhost:8000/socketcluster/替换为ws://<PC's IP address>:8000/socketcluster/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54477290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档