七度&光 2017-03-14 16:53 采纳率: 22.2%
浏览 38

检查服务器可达性

I'm working on a web-tool. It is built with PHP, HTML and MySQL. My need is to check the reachability of the server every once in a while. So far, I've tried to sending a simple query through JQuery-Ajax function and if the query is received, display an OK message, otherwise show an ERROR message. This request is being sent every x amount of seconds. I would like to know if there is any better approach than mine or any other way to determine the server reachability. I would like the client side to know when the server is no longer active for whatever reason.

This is what I have so far:

Client Side

<!DOCTYPE html>
    <head>
        <script src="assets/js/jquery.js"></script>
        <link href="https://opensource.keycdn.com/fontawesome/4.7.0/font-awesome.min.css" rel="stylesheet">
    </head>
    <title>Check for MySQL availability</title>
    <body onload="checking()">
        <p style="display: inline;">MySQL</p>
        <!-- Simple circle colored as orange (determining), red (unavailable) and green (available) -->
        <p style="display: inline;" id="world"><i class="fa fa-circle" aria-hidden="true"></i></p>
    </body>
    <script>
        function checking(){
            var dataString = 'signum=some_string';
            $.ajax({
                type: "POST",
                url: "testsql.php",
                data: dataString,
                cache: true,
                timeout: 30000, //Timeout after 30 seconds
                beforeSend: function(){
                //Checking status... (orange)
                $("#world").html('<i class="fa fa-spinner fa-pulse fa-fw"></i>').css('color', 'orange');
                },
                error: function() {
                    //Show MySQL as unavailable (red)
                    $("#world").html('<i class="fa fa-circle" aria-hidden="true"></i>').css('color', 'red');
                },
                success: function(data){
                    if(!data){
                        console.err("Error!");
                        //MySQL is unavailable (red)
                        $("#world").html('<i class="fa fa-circle" aria-hidden="true"></i>').css('color', 'red');
                    }else if(data){
                        //MySQL is available (green)
                        console.log("MySQL available!");
                        $("#world").html('<i class="fa fa-circle" aria-hidden="true"></i>').css('color', 'green');
                        }else{
                        //MySQL is unavailable (red)
                        console.err("Error!");
                        $("#world").html('<i class="fa fa-circle" aria-hidden="true"></i>').css('color', 'red');
                    }
                }
            });
            //Send Ajax requests every 3 seconds
            var t = setTimeout(checking, 3000);
        }
    </script>
    </html>

Note: The solution provided on Is there any way to check reachability test of server in jQuery did not work for me

  • 写回答

1条回答 默认 最新

  • weixin_33705053 2017-03-14 17:00
    关注

    It might be a bit more efficient to use an image (assuming that your server successfully sending an image indicates that it is up):

    function checkStatus(imgUrl) {
        return new Promise(function(resolve, reject) {
            var img = new Image();
            img.src = imgUrl;
            img.onload = resolve;
            img.onerror = reject;
        });
    }
    
    var imageUrl = 'https://cdn.sstatic.net/Sites/stackoverflow/img/sprites.svg?v=d689de80c4e2';
    var UPTIME_INTERVAL = 3000;
    
    setInterval(function() {
        checkStatus(imageUrl)
           .then(function(success) { console.log('success') })
           .catch(function(err) { console.log('error') })
    }, UPTIME_INTERVAL);
    

    Edit: A very small image would be ideal in this scenario

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)