PHP版本是5.5.12
<?php
error_reporting(E_ALL);
$service_port = 2002;
$address = 'x.x.x.x';
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
$input = "(({\"u\":\"994774100040\",\"ut\":0,\"lon\":28.838424,\"lox\":28.941421,\"lax\":41.074951,\"lan\":40.965122,\"rg\":0.0,\"pr\":0.0,\"di\":0.0,\"e\":false,\"p\":0,\"r\":0,\"l\":1,\"cr\":0,\"ac\":2,\"st\":0,\"t\":10,\"a\":5,\"ls\":1}))";
$out = '';
socket_write($socket, $input, strlen($input));
echo "OK.\n";
$buf = '';
//infinite waiting on below line
if (false !== ($bytes = socket_recv($socket, $buf, 1024,0))) {
echo "Read $bytes bytes from socket_recv(). Closing socket...";
} else {
echo "socket_recv() failed; reason: " .
socket_strerror(socket_last_error($socket)) . "\n";
}
socket_close($socket);
echo $buf . "\n";
echo "OK.\n\n";
?>
答案 0 :(得分:0)
我解决了添加以下代码进行握手的问题。:
$key = com_create_guid();
$header = "GET HTTP/1.1\r\n";
$header.= "Upgrade: WebSocket\r\n";
$header.= "Connection: Upgrade\r\n";
$header.= "Host: x.x.x.x:2002\r\n";
$header.= "Origin: http://localhost:81\r\n";
$header.= "Sec-WebSocket-Key: " . $key . "\r\n";
$header.= "\r\n";
socket_write($socket, $header, strlen($header));
Web套接字服务器等待来自客户端的标头以进行握手。