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
410 views
in Technique[技术] by (71.8m points)

how to get socket_read value to plain text sent from javascript in PHP websocket

i am trying to make my small websocket php class in which i wanted to make functions like onopen onmessage etc like node js [i know there are other libraries out there but i wanted to learn it myself].so far i am able to successfully recieve message from js websocket to php with this code below

<?php 
$host = "localhost";
$port = 8001;
$sockets=array();

function sendAll($connections,$msg){
    foreach ($connections as $each) {
        send($each,$msg);
    }
}

function send($client,$msg){
    $msg = chr(129) . chr(strlen($msg)) . $msg;
    socket_write($client,$msg,strlen($msg));
}


function handshake($client){
    $request = socket_read($client, 5000);
    preg_match('#Sec-WebSocket-Key: (.*)
#', $request, $matches);
    $key = base64_encode(pack(
        'H*',
        sha1($matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
    ));
    $headers = "HTTP/1.1 101 Switching Protocols
";
    $headers .= "Upgrade: websocket
";
    $headers .= "Connection: Upgrade
";
    $headers .= "Sec-WebSocket-Version: 13
";
    $headers .= "Sec-WebSocket-Accept: $key

";
    socket_write($client, $headers, strlen($headers));
}

// Create WebSocket.
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_nonblock($server);
socket_bind($server, $host, $port);
socket_listen($server);    


while(true){

    $client = socket_accept($server);
    if($client != false){
        usleep(100);
        handshake($client);
        array_push($sockets,$client);
        print_r($sockets); 
    }
    else{
        if(count($sockets) > 0){
            foreach ($sockets as $each) {
                $msg = socket_read($each,5000);
                if($msg != false){
                    filter($msg);
                    // send($each,"testing");
                }
            }
        }
    }


}



function filter($msg){
    $msg = unpack("c*",$msg);
    foreach ($msg as $value) {
        echo chr($value);
    }
    // print_r($msg);
}
?>

after some research i found that websocket sends message in binary. i cant convert that message into plain text. i dont know which language or format that data is sent into.tell me please which format is it and how to read it as plain text sample picture below enter image description here

question from:https://stackoverflow.com/questions/65644666/how-to-get-socket-read-value-to-plain-text-sent-from-javascript-in-php-websocket

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

1 Reply

0 votes
by (71.8m points)

finally i find the way to decode from this website

its actually masked which needed to be unmasked . here i found a function from a website all i have to do is to read that scarmled text is unmask with below custom function

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;
    }

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

...