Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

Encoding/Unmasking Websocket binary data in PHP

I have set up a PHP WebSocket server that is able to read string data from clients. My question is on how to handle binary data types. Below is the code on the client side that records microphone input as a Float32Array object and sends the data over a WebSocket connection in binary.

websocket = new WebSocket("ws://...");
websocket.binaryType = "arraybuffer";

recorder.onaudioprocess = function(stream) {
    var inputData = stream.inputBuffer.getChannelData(0);
    websocket.send(inputData);
}

As for the server side, I am using the following functions which I've found online for encoding/unmasking.

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('CCS', $b1, 126, $length);
    elseif ($length >= 65536)
        $header = pack('CCN', $b1, 127, $length);

    return $header.$text;
}

function unmask($payload) 
{
    $length = ord($payload[1]) & 127;

    if($length == 126) {
        $masks = substr($payload, 4, 4);
        $data = substr($payload, 8);
        $len = (ord($payload[2]) << 8) + ord($payload[3]);
    }
    elseif($length == 127) {
        $masks = substr($payload, 10, 4);
        $data = substr($payload, 14);
        $len = (ord($payload[2]) << 56) + (ord($payload[3]) << 48) +
            (ord($payload[4]) << 40) + (ord($payload[5]) << 32) + 
            (ord($payload[6]) << 24) +(ord($payload[7]) << 16) + 
            (ord($payload[8]) << 8) + ord($payload[9]);
    }
    else {
        $masks = substr($payload, 2, 4);
        $data = substr($payload, 6);
        $len = $length;
    }

    $text = '';
    for ($i = 0; $i < $len; ++$i) {
        $text .= $data[$i] ^ $masks[$i%4];
    }

    return $text;
}

The code works great except they only work on string data and not for binary type. My question is how do I handle binary type and push them to clients?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't really get what you are trying to do. WebRTC can be used to create Peer2Peer connections. Thus you would use your PHP Server as a signaling server. The actual data is not passed to your server unless it is needed for forwarding because a p2p connection could not be established. Is that what you want to do? Otherwise create a RtcPeerConnection and send data from peer to peer as it is intended by webrtc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...