如何处理Websockets中的ping远程服务器?

时间:2015-08-05 05:54:43

标签: java java-ee servlets hadoop websocket

我有一个Web界面,我可以通过它在Hadoop等后端系统上执行/应用操作。我正在尝试编写一个功能,允许我跟踪服务器的状态。基本上是查看服务器/服务是启动还是关闭。我需要每2分钟做一次。因此,我应该使用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>

所以你能让我知道我做错了什么吗?

由于

0 个答案:

没有答案