websocket sending and receiving messages



  • I'll put websocket on the server without nodejs.

    As a server, use this decision: https://github.com/Doncode/simple_php_websocket_server

    I'm launching a browser server.

    On 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, no mistakes.

    But how do you get the server to answer? He's 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); //эхо

    I understand that she has to work after every message she has received, but that's not happening. How? The server problem?



  • I'll put websocket on the server without nodejs. I don't understand the meaning of this phrase, you're putting the PHP down on the reference - naturally without nodejs!

    As a server, use this decision: https://github.com/Doncode/simple_php_websocket_server

    Project simple_php_websocket_server - 14 stars don't recommend it

    I recommend. https://github.com/ratchetphp/Ratchet It's based on the ReactPHP asynchronous frimetre. https://github.com/reactphp/react Don't forget to put in any way EventLoop does not use the stream.

    After you've tried to test Open the manufacturer ' s tools at the browser - Network - state WS - tie Click on your connection - and see the frame from the server.

    Good work!




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2