How can I send and receive WebSocket messages on the server side?

后端 未结 11 2043
轻奢々
轻奢々 2020-11-22 06:21
  • How can I send and receive messages on the server side using WebSocket, as per the protocol?

  • Why do I get seemingly random bytes at the server when

11条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 06:40

    In addition to the PHP frame encoding function, here follows a decode function:

    function Decode($M){
        $M = array_map("ord", str_split($M));
        $L = $M[1] AND 127;
    
        if ($L == 126)
            $iFM = 4;
        else if ($L == 127)
            $iFM = 10;
        else
            $iFM = 2;
    
        $Masks = array_slice($M, $iFM, 4);
    
        $Out = "";
        for ($i = $iFM + 4, $j = 0; $i < count($M); $i++, $j++ ) {
            $Out .= chr($M[$i] ^ $Masks[$j % 4]);
        }
        return $Out;
    }
    

    I've implemented this and also other functions in an easy-to-use WebSocket PHP class here.

提交回复
热议问题