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
Client

