Creating a PHP websockets server

前端 未结 2 1517
庸人自扰
庸人自扰 2021-01-17 03:42

I am new in websockets technology. I was trying to create a websockets php server and connect to the server with a javascript client. I am using xampp 1.8.3.

I made

相关标签:
2条回答
  • 2021-01-17 04:05

    WebSockets are not raw TCP sockets. They require a rather complex HTTP-like handshake to establish a connection, and require data transferred over them to be encoded and framed in a very particular way. The protocol is defined in RFC 6455.

    Unless you are feeling incredibly masochistic, you don't want to try to implement this yourself. Use a library like Ratchet to implement WebSockets in PHP.

    0 讨论(0)
  • 2021-01-17 04:31

    I had the same problem bro but no need of using Ratchet or other libraries you can write your own simple code . The handshake process,and the masking-unmasking of messages is rather difficult so i copied the code for those

    function perform_handshaking($receved_header,$client_conn, $host, $port)
    {
        $headers = array();
        $lines = preg_split("/\r\n/", $receved_header);
        foreach($lines as $line)
        {
            $line = chop($line);
            if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))
            {
                $headers[$matches[1]] = $matches[2];
            }
        }
    
        $secKey = $headers['Sec-WebSocket-Key'];
        $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
        //hand shaking header
        $upgrade  = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
        "Upgrade: websocket\r\n" .
        "Connection: Upgrade\r\n" .
        "WebSocket-Origin: $host\r\n" .
        "WebSocket-Location: ws://$host:$port/\r\n".
        "Sec-WebSocket-Accept:$secAccept\r\n\r\n";
        socket_write($client_conn,$upgrade,strlen($upgrade));
        return $upgrade;
    }
    
    function unmask($text) {
        $length = ord($text[1]) & 127;
        if($length == 126) {
            $masks = substr($text, 4, 4);
            $data = substr($text, 8);
        }
        elseif($length == 127) {
            $masks = substr($text, 10, 4);
            $data = substr($text, 14);
        }
        else {
            $masks = substr($text, 2, 4);
            $data = substr($text, 6);
        }
        $text = "";
        for ($i = 0; $i < strlen($data); ++$i) {
            $text .= $data[$i] ^ $masks[$i%4];
        }
        return $text;
    }
    
    //Encode message for transfer to client.
    function mask($text)
    {
        $b1 = 0x80 | (0x1 & 0x0f);
        $length = strlen($text);
    
        if($length <= 125)
            $header = pack('CC', $b1, $length);
        elseif($length > 125 && $length < 65536)
            $header = pack('CCn', $b1, 126, $length);
        elseif($length >= 65536)
            $header = pack('CCNN', $b1, 127, $length);
        return $header.$text;
    }
    

    Instead of socket_accept user socket_read to get the http header containing request from webpage that it wants to upgrade its http conn to websocket then use the handshake function above to write an accept header message .then your connection will be established .but still on the client side you have to add events like these

    if("WebSocket" in window){
        var a = "ws://"+serverip+":9000";
        var ws = new WebSocket(a);
        var error = null;
    
        ws.onopen = function(){
            open();
        }
    
        ws.onerror = function(err){
            errorhandler(err);
    
        }
        ws.onmessage = function(e){
            messagehandler(e);
    
        }
        ws.onclose = function(){
            close();
        }
    
    }
    
    function  open(){
                   document.getElementById('logs').innerHTML+='<p>WebSocket Connection OPened</p>';
    
    }
    
    function errorhandler(err){
                   document.getElementById('logs').innerHTML+='<p>WebSocket Connection Error occured &nbsp'+err.data+'</p>';
    
    }
    
    function messagehandler(a){
                   document.getElementById('logs').innerHTML+="<p>"+a.data+"</p>";
    
    }
    
    function close(){
                    document.getElementById('logs').innerHTML+='<p>WebSocket Connection Closed</p>';
                    ws = null;
    
    }
    
    0 讨论(0)
提交回复
热议问题