使用 WebSocket 连接服务器时连接被拒绝

Posted

技术标签:

【中文标题】使用 WebSocket 连接服务器时连接被拒绝【英文标题】:Connection refused when Using WebSocet to connect to server 【发布时间】:2018-06-06 07:23:42 【问题描述】:

我正在尝试从我的 android 应用程序连接到 glassFish-server4.1.1 但是在 90000 毫秒后给我错误无法连接到 /localhost(端口 8080) 我更改了服务器上的端口,但给了我同样的错误 ......

它仅在 netbeans 上连接到服务器,但在 android 上给我失败并低于我的代码

服务器代码`

 @ServerEndpoint("/echo") 

公共类 WebSocketClass

 /**
 * @param session
 * @OnOpen allows us to intercept the creation of a new session.
 * The session class allows us to send data to the user.
 * In the method onOpen, we'll let the user know that the handshake was 
 * successful.
 */


@OnOpen
public void onOpen(Session session)
    System.out.println(session.getId() + " has opened a connection"); 
    try 
        session.getBasicRemote().sendText("Connection Established");
     catch (IOException ex) 
    


/**
 * When a user sends a message to the server, this method will intercept the message
 * and allow us to react to it. For now the message is read as a String.
 * @param message
 * @param session
 */
@OnMessage
public void onMessage12(String message, Session session)
    System.out.println("Message from " + session.getId() + ": " + message);
    try 
        session.getBasicRemote().sendText(message);
     catch (IOException ex) 
    


/**
 * The user closes the connection.
 * 
 * Note: you can't send messages to the client from this method
 * @param session
 */
@OnClose
public void onClose(Session session)
    System.out.println("Session " +session.getId()+" has ended");

客户端代码

private void connectWebSocket() 
    URI uri;
    try 
        uri = new URI("ws://localhost:8080/WebApplication1/echo");
     catch (URISyntaxException e) 
        e.printStackTrace();
        return;
    

    mWebSocketClient = new WebSocketClient(uri) 
        @Override
        public void onOpen(ServerHandshake serverHandshake) 
            Log.i("Websocket", "Opened");
            mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
        

        @Override
        public void onMessage(String s) 
            final String message = s;
            runOnUiThread(new Runnable() 
                @Override
                public void run() 
                    TextView textView = (TextView)findViewById(R.id.messages);
                    textView.setText(textView.getText() + "\n" + message);
                
            );
        

        @Override
        public void onClose(int i, String s, boolean b) 
            Log.i("Websocket", "Closed " + s);
        

        @Override
        public void onError(Exception e) 
            Log.i("Websocket", "Error " + e.getMessage());
        
    ;
    mWebSocketClient.connect();

我不知道为什么会出现这个错误? 谁能帮帮我...

【问题讨论】:

【参考方案1】:

也许您应该更改 IP 以反映客户端上安装 glassfish 服务器的 IP

【讨论】:

以上是关于使用 WebSocket 连接服务器时连接被拒绝的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot Websocket 在 SockJS 客户端测试中抛出“连接被拒绝”

如何使用 faye-websocket-ruby 向 websocket 客户端发送拒绝连接

WebSocket长连接接入支付宝消息服务,实现消息通知

macOS - Dockerize MySQL 服务连接被拒绝,使用时崩溃

ssh 与 *** 连接被拒绝

wifi连接被拒绝是啥意思家里wifi拒绝接入