2

I've tested several libraries (workerman and PHP-Websockets) but in all cases I have the same issue - when I open clients page everything is all right, I have websocket connection with my server, but later it disconnects automatically in a few minutes or less. Is it known issue or is there any methods to handle it? Or should I simply implement some function to force recconection if websocket fails?

This is a code sample for workerman

Server 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();

And client client.html

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!");
}
<!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>

So, at first I start apache server and then run my websocket script via console, websocket server starts fine. Then I open my client web page and websocket connection establishes fine too, but if I wait for about 1 minute or less it fails with no reason. How to establish permanent websocket connection? Any advise will be appreciated. Thanks!

Server

enter image description here

Client

enter image description here

4
  • Are you running the PHP script from console? Is there any output? Does server process quit or continues after you've lost the connection? Commented Feb 23, 2018 at 6:51
  • Yes, I start PHP script for websockets via terminal, it works fine with no issue. When I open client's page server recognizes a new connection and client websocket is connected successfully. But if I wait for about 1 minute or so then websocket connection drops without reason with error code 1006 :( So that server still working but client is not connected any more and from this point I have to catch this failure on the client side and force websocket reconnection or implement permanent websocket connection somehow. Look at screenshots, I've updated my post. Thanks! Commented Feb 24, 2018 at 4:19
  • Seems that it is known issue for the client in case of websocket connection (websocket idle timeout). From this post: "In case of established websocket connection, server or firewall could timeout and terminate the connection after a period of inactivity." Commented Feb 24, 2018 at 4:37
  • Actually, this method solved the problem :) Commented Feb 24, 2018 at 4:44

1 Answer 1

2

As written in this article: "In case of established websocket connection, server or firewall could timeout and terminate the connection after a period of inactivity."

This is the solution!

Modified client's page:

<!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>

Hope it will help to somebody.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.