PHP websocket disconnect问题

时间:2018-02-22 16:16:00

标签: javascript php websocket

我已经测试了几个库(workermanPHP-Websockets),但在所有情况下我都有同样的问题 - 当我打开客户端页面时,一切都很好,我和我的websocket连接服务器,但稍后它会在几分钟或更短的时间内自动断开连接。它是已知问题还是有任何方法来处理它?或者我应该简单地实现一些函数来强制recconection如果websocket失败?

这是 workerman

的代码示例

服务器test_ws.php

<?php
require_once '/home/ubuntu/workspace/workerman/vendor/autoload.php';
use Workerman\Worker;

// Create a Websocket server
$ws_worker = new Worker("websocket://0.0.0.0:8082");

// 6 processes
$ws_worker->count = 6;

// Emitted when new connection come
$ws_worker->onConnect = function($connection)
{
    echo "New connection\n";
    $connection->send('Hello from server');
};

// Emitted when data received
$ws_worker->onMessage = function($connection, $data)
{
    // Send hello $data
    $connection->send('Data from server: ' . $data);
};

// Emitted when connection closed
$ws_worker->onClose = function($connection)
{
    echo "Connection closed\n";
};

// Run worker
Worker::runAll();

客户client.html

&#13;
&#13;
var socket = new WebSocket("wss://example.com:8082");
        
socket.onopen = function() {
   console.log("Connected.");
};

socket.onclose = function(event) {
   if (event.wasClean) {
      console.log('Connection closed');
   } else {
      console.log('Connection failure');
   }
   console.log('Code: ' + event.code + ' Reason: ' + event.reason);
};
        
socket.onmessage = function(event) {
   console.log("Data received: " + event.data);
};

socket.onerror = function(error) {
   console.log("Error: " + error.message);
};
            
function send() {
   socket.send("Hello!");
}
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <title>Workerman Sockets Test</title>
</head>
<body>
   <h3>Hello</h3>
   <button onclick="send()">Send Message</button>
</body>
</html>
&#13;
&#13;
&#13;

所以,首先我启动apache服务器,然后通过控制台运行我的websocket脚本,websocket服务器启动正常。然后我打开我的客户端网页和websocket连接建立也很好,但是如果我等待大约1分钟或更短时间它没有理由失败。如何建立永久的websocket连接?任何建议将不胜感激。谢谢!

服务器

enter image description here

客户端

enter image description here

1 个答案:

答案 0 :(得分:1)

this article所述:“如果建立了websocket连接,服务器或防火墙可能会超时并在一段时间不活动后终止连接。”

这是解决方案!

修改后的客户页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Workerman Sockets Test</title>
</head>
<body>
    <h3>Hello</h3>
    <button onclick="send()">Send Message</button>
    <script type="text/javascript">
        var socket = new WebSocket("wss://example.com:8082");

        var timerID = 0; 
        function keepAlive() { 
            var timeout = 20000;  
            if (socket.readyState == socket.OPEN) {  
                socket.send('');  
            }  
            timerId = setTimeout(keepAlive, timeout);  
        }  
        function cancelKeepAlive() {  
            if (timerId) {  
                clearTimeout(timerId);  
            }  
        }

        socket.onopen = function() {
          console.log("Connected.");
          document.body.innerHTML += "<p>Connected.</p>";

          keepAlive();
        };

        socket.onclose = function(event) {
          if (event.wasClean) {
            console.log('Connection closed');
            document.body.innerHTML += "<p>Connection closed</p>";
          } else {
            console.log('Connection failure');
            document.body.innerHTML += "<p>Connection failure</p>";
          }
          console.log('Code: ' + event.code + ' Reason: ' + event.reason);
          document.body.innerHTML += "<p>Code: " + event.code + " Reason: " + event.reason + "</p>";

          cancelKeepAlive();
        };

        socket.onmessage = function(event) {
          console.log("Data received: " + event.data);
          document.body.innerHTML += "<p>Data received: " + event.data + "</p>";
        };

        socket.onerror = function(error) {
          console.log("Error: " + error.message);
          document.body.innerHTML += "<p>Error: " + event.message + "</p>";

          cancelKeepAlive();
        };

        function send() {
            socket.send("Hello");
        }
    </script>
</body>
</html>

希望它会对某人有所帮助。