This question has already been answered:

There is a WebSocket, there is a code which receives data and sends, in general, everything is as it should be. Everything works, but not for long, after a short time (a couple of minutes) and small manipulations (page updates, etc.), the socket stops receiving messages and I get the following data from onclose:

enter image description here

On the server side, I use this library: https://github.com/pmill/php-chat

On the Internet, there is very little information about this and unfortunately very little I understand in which direction to dig.

Here is my js code:

submit_msg.unbind('click').click(function () { var message = input_message.val().trim(); if (message !== '') { sendChatMessage(); input_message.val(''); } input_message.focus(); return false; }); function connectToSocket() { conn = new WebSocket('ws://localhost:2222'); } // ЭТО Я ПЫТАЛСЯ ВОЗОБНОВЛЯТЬ СОЕДИНЕНИЕ ЕСЛИ ОНО ЗАКРОЕТСЯ function trackingStateWS() { interval_tracking_status_ws = setInterval(function() { if (conn.readyState === conn.CLOSED) { conn.close(); connectToSocket(); } }, 5000); } function connectToChat() { connectToSocket(); trackingStateWS(); conn.onopen = function () { var params = { // my params }; conn.send(JSON.stringify(params)); }; conn.onmessage = function (e) { var data = JSON.parse(e.data), date = new Date(data.timestamp * 1000); // ОБРАБОТКА ПРИХОДЯЩИХ ДАННЫХ }; conn.onclose = function(e) { console.log(e); }; conn.onerror = function (e) { console.log(e); }; return false; } function sendChatMessage() { var params = { // my params }; conn.send(JSON.stringify(params)); return false; } 

ADDED

When a web socket stops sending messages from clients, they can still connect to the web socket and have their status "online". It turns out onopen always works, and onmessage stops working after a couple of minutes.

Reported as a duplicate member of the Spirit Community Sep 15 '18 at 14:24 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Comments are not intended for extended discussion; conversation moved to chat . - Yuriy SPb

1 answer 1

1. Enable logging for PHP CLI

You need to edit php.ini , the gist is shown below.

 # запускаем консоль и вводим php -a # теперь мы в PHP CLI echo php_ini_loaded_file(); echo ini_get('error_reporting'); echo ini_get('log_errors'); echo ini_get('error_log'); # Ctrl+C, теперь мы снова в bash this_function_not_exists(); # Снова в командной строке cat /var/log/cli_php_errors.log 

enter image description here

You need to make sure that the rights to /var/log/cli_php_errors.log are set correctly.

2. Run test chat

We start the test chat from the php example/server.php , wait for the connection to break, look at the logs from item 1

 cat /var/log/cli_php_errors.log 

Trying to understand what errors occurred in the PHP script. Conclusion - post it here.

  • I did everything as you said, caused an error, checked that the logs are working [23-Mar-2018 16:37:34 Europe/Kiev] An Error occurred while handling another error ... . But the disconnect did not show any logs. Apparently, because there was no disconnection as such, the user can connect to the socket, but cannot send a message. How many I use Open Server, only now I saw that it has its own window for logs, everything is already configured there. - Human
  • Open server = web server + base + PHP. Immediately we run php from the console. The console version has its own log of logs, and not the fact that it appears OpSer. In general, here is the same unsolved problem. ru.stackoverflow.com/questions/512116/… - Total Pusher