希望这个问题之前没有解决,但我真的在努力寻找答案!
我有一个问题与接收帧通过socket_recv在PHP服务器。当我在一次(例如在周期中)从客户端发送超过1条消息时,出现问题。通过流长度,它的匹配帧计数,但我不能正确地揭开帧,我的unmask函数只返回第一帧。我使用的是我在这里找到的php服务器的一部分,对于一条消息它工作正常,但是当我一次接收更多消息时,它不会打开所有的掩码。
我试图添加循环来通过所有帧,但我无法找到帧的结束并继续到其他帧。
这里是服务器的主while:
while (true) {
//manage multipal connections
$changed = $clients;
//returns the socket resources in $changed array
socket_select($changed, $null, $null, 0, 10);
//check for new socket
if (in_array($socket, $changed)) {
$socket_new = socket_accept($socket); //accpet new socket
$clients[] = $socket_new; //add socket to client array
$header = socket_read($socket_new, 10240); //read data sent by the socket
perform_handshaking($header, $socket_new, $host, $port); //perform websocket handshake
socket_getpeername($socket_new, $ip); //get ip address of connected socket
$response = mask(json_encode(array('type' => 'system', 'status' => true, "id" => "SRV_CONNECTED", 'message' => $ip . ' connected'))); //prepare json data
send_message($response); //notify all users about new connection
//make room for new socket
$found_socket = array_search($socket, $changed);
unset($changed[$found_socket]);
}
foreach ($changed as $changed_socket) {
while (@socket_recv($changed_socket, $buf, 1024, 0) >= 1) {
$received_text = unmask($buf); //unmask data
$tst_msg = json_decode($received_text, true); //json decode
$response_text = parse_msg($tst_msg, $changed_socket);
break 2; //exit this loop
}
$buf = @socket_read($changed_socket, 10240, PHP_NORMAL_READ);
if ($buf === false) { // check disconnected client
// remove client for $clients array
$found_socket = array_search($changed_socket, $clients);
socket_getpeername($changed_socket, $ip);
unset($clients[$found_socket]);
//notify all users about disconnected connection
$response = mask(json_encode(array('type' => 'system', 'message' => $ip . ' disconnected')));
send_message($response);
}
}
我需要调用parse_msg通过@socket_recv接收帧的计数,这应该由unmask函数返回。
下面是unmask函数:
function unmask($payload){
$decMessages = Array();
do { // This should be running until all frames are unmasked and added to $decMessages Array
$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];
}
$decMessages[] = $text;
// Here is problem. It doesn't put correct substr to $payload, so it could run again on stream shorted by last message
$payload = substr($payload, $length, strlen($payload));
}while (($len < strlen($data)) and $countert < 10);
return $decMessages;
}
我认为我的问题在这里:$payload = substr($payload, $length, strlen($payload));我在哪里放错了?
我的问题是:为什么@socket_recv返回多个帧我如何改变我的unmask函数来通过所有帧,而不仅仅是第一个帧
我将非常感谢任何帮助,因为我比我应该做的更多。
提前感谢美妙的!
所以,我找到了解决方案。正如我所想的,问题是一次接收多个帧。我需要编辑我的unmask函数来检查流中是否还有帧。
基本上,我写的是对的但是$length in
$payload = substr($payload, $length, strlen($payload));
是错误的。而不是$length我使用$len + 8,14 od 6取决于$length。我不确定如何描述这其中的原因,但我确定有人会。这是所有。现在它正常工作了!
谢谢!