开发者

PHP Websocket Server hybi10

开发者 https://www.devze.com 2023-03-28 09:29 出处:网络
So Chrome 14 has implemented hybi10 version of websockets. I have a in house program that our company uses via chrome that uses websockets which is broken with this change.

So Chrome 14 has implemented hybi10 version of websockets. I have a in house program that our company uses via chrome that uses websockets which is broken with this change.

Has anyone been successful framing the data using a php server? I am able to get the new handshake to work but I can't seem to figure out the framing. There is a python example here https://github.com/kanaka/websockify/blob/master/websocket.py#L233 but I am having a difficult time converting this to php开发者_如何转开发, anyone have a suggestion?

I should mention that the function in question on the python example is decode_hybi().


i just completed a class wich makes the PHP-Websocket-Server of Nico Kaiser (https://github.com/nicokaiser/php-websocket) capable of handling hybi-10 frames and handshake. You can download the new class here: http://lemmingzshadow.net/386/php-websocket-serverclient-nach-draft-hybi-10/ (Connection.php)


This code assumes no errors or malformed frames and is based on this answer - How to (de)construct data frames in WebSockets hybi 08+?.

This code is very basic and is far from a complete solution. It works for my purposes (which are pretty basic). Hopefully it is of use to others.

function handle_data($data){
    $bytes = $data;
    $data_length = "";
    $mask = "";
    $coded_data = "" ;
    $decoded_data = "";        
    $data_length = $bytes[1] & 127;
    if($data_length === 126){
       $mask = substr($bytes, 4, 8);
       $coded_data = substr($bytes, 8);
    }else if($data_length === 127){
        $mask = substr($bytes, 10, 14);
        $coded_data = substr($bytes, 14);
    }else{
        $mask = substr($bytes, 2, 6);
        $coded_data = substr($bytes, 6);
    }
    for($i=0;$i<strlen($coded_data);$i++){
        $decoded_data .= $coded_data[$i] ^ $mask[$i%4];
    }
    $this->log("Server Received->".$decoded_data);
    return true;
}

Here is the code to send data back. Again this is pretty basic, it assumes you are sending a single text frame. No continuation frames etc. No error checking either. Hopefully others find it useful.

public function send($data)
{
    $frame = Array();
    $encoded = "";
    $frame[0] = 0x81;
    $data_length = strlen($data);

    if($data_length <= 125){
        $frame[1] = $data_length;    
    }else{
        $frame[1] = 126;  
        $frame[2] = $data_length >> 8;
        $frame[3] = $data_length & 0xFF; 
    }

    for($i=0;$i<sizeof($frame);$i++){
        $encoded .= chr($frame[$i]);
    }

    $encoded .= $data;
    write_to_socket($this->socket, $encoded);  
    return true;     
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号