如何在websockets中处理ping远程服务器?

ht4b089n  于 2021-06-02  发布在  Hadoop
关注(0)|答案(0)|浏览(377)

我有一个web界面,通过它我可以在hadoop这样的后端系统上执行/应用操作。我试图编写一个功能,使我能够跟踪服务器的状态。基本上是看服务器/服务是否正常。我需要每两分钟做一次。所以我应该用websocket或sse(服务器发送事件)来实现这个..我已经编写了这个代码来执行这个。但是,当我每2分钟检查一次代码状态时。它变成了一个阻塞调用,我无法执行web ui的任何其他功能
下面是代码

import java.io.*;
import java.net.*;
import java.util.concurrent.TimeUnit;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/echo") 
public class Websocket {

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

            onping(session);
}
public void onping(Session session) throws IOException

 {    if (session.isOpen())
 {
 session.getBasicRemote().sendText("In PING");
 String ip = "200.168.100.46";
     InetAddress inet = InetAddress.getByName(ip);
 if(inet.isReachable(1000))
 {
     session.getBasicRemote().sendText("Alive");

 }

   }
 else 
  {
  System.out.println("Error");
  }

   }
@OnClose
public void onClose(Session session){
System.out.println("Session " +session.getId()+" has ended");
}
}

客户端代码是

<!DOCTYPE html>
 <html>
 <head>
    <title>Websocket</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
 </head>
 <body>

    <div>
        <input type="text" id="messageinput"/>
    </div>
    <div>
        <button type="button" onclick="openSocket();" >Open</button>
        <button type="button" onclick="closeSocket();" >Close</button>
        <button type="button" onclick="pingSocket();" >Ping</button>
    </div>
    <!-- Server responses get written here -->
    <div id="messages"></div>

    <!-- Script to utilise the WebSocket -->
    <script type="text/javascript">

        var webSocket;
        var messages = document.getElementById("messages");

        function openSocket(){
            // Ensures only one connection is open at a time
            if(webSocket !== undefined && webSocket.readyState !== WebSocket.CLOSED){
               writeResponse("WebSocket is already opened.");
                return;
            }
            // Create a new instance of the websocket
            webSocket = new WebSocket("ws://localhost:8080/Websocket/echo");

            /**
             * Binds functions to the listeners for the websocket.
             */
            webSocket.onopen = function(event){
                // For reasons I can't determine, onopen gets called twice
                // and the first time event.data is undefined.
                // Leave a comment if you know the answer.
                if(event.data === undefined)
                    return;

                writeResponse(event.data);
            };

            webSocket.onmessage = function(event){
                writeResponse(event.data);
            };

            webSocket.onclose = function(event){
                writeResponse("Connection closed");
            };
        }

        /**
         * Sends the value of the text input to the server
         */
        function send(){
            var text = document.getElementById("messageinput").value;
            webSocket.send(text);
        }
       function pingSocket(){
            var text = document.getElementById("messageinput").value;
            webSocket.send("PING");
        }
        function closeSocket(){
            var text = document.getElementById("messageinput").value;
            webSocket.send(text);
            webSocket.close();
        }

        function writeResponse(text){
            messages.innerHTML += "<br/>" + text;
        }

    </script>

</body>
</html>

你能告诉我我做错了什么吗?
谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题