I put websocket on the server, without nodejs.

I use this solution as a server: https://github.com/Doncode/simple_php_websocket_server

I start the server from the browser.

on the client:

var socket = new WebSocket("ws://мой адрес:9000/ws"); socket.onopen = function() { alert("Соединение установлено."); }; socket.onclose = function(event) { if (event.wasClean) { alert('Соединение закрыто чисто'); } else { alert('Обрыв соединения'); } alert('Код: ' + event.code + ' причина: ' + event.reason); }; socket.onmessage = function(event) { alert("Получены данные " + event.data); }; socket.onerror = function(error) { alert("Ошибка " + error.message); }; setInterval(function() { socket.send('msg1'); }, 5000); 

Connection is established, there are no errors.

But how to make the server respond? He is silent like a fish ...

The library has a function

 function websocket_onmessage($keyINsock, $str){ echo "\r\n"; echo "WEBSOCKET_ONMESSAGE[$keyINsock] $str \r\n"; echo "\r\n"; websock_send($keyINsock, $str); //эхо 

}

As I understand it, it should work after each received message, but this does not happen. How to be? Problem with server?

  • port 9000 at you strange it seems like by default on the 888 port it is started judging by docks. - Naumov
  • I already experimented it. Of course, I changed the port and on the server. - Floyat
  • Any output to the port? - Naumov
  • How to see? I tried on different ports, it does not help. 888, 9000, 888, 9001, 9999 .... - Floyat
  • Well, then you should bring something there, for example, to make 'fwrite' in the socket file at least - Naumov

1 answer 1

I put websocket on the server, without nodejs. I did not understand the meaning of this phrase, you put the PHP implementation of the link below - of course it is without nodejs!

I use this solution as a server: https://github.com/Doncode/simple_php_websocket_server

The project simple_php_websocket_server - 14 stars I do not recommend it

I recommend to take https://github.com/ratchetphp/Ratchet to build it on the basis of the ReactPHP asynchronous framework https://github.com/reactphp/react Do not forget to put any implementation of EventLoop not to use stream

After you start testing, open the developer tools in the browser - Network -> WS -> Click on your connection -> and you will see all the frames from the server

Successful development!

  • Thank. But I do not need such monsters. I need a simple implementation of the server that would at least somehow respond to messages. Then I would understand what was the matter and would add the modules I needed. In the meantime, I can’t understand something is wrong on the server, or it’s in the wrongly working framework. - Floyat