php ini 长连接秒数,php使用webSocket实现Echarts长连接自动刷新的解决方案(2):后端服务端代码返回json数据...
$address = "127.0.0.1";
$port = 9090; //調試的時候,可以多換端口來測試程序!
set_time_limit(0);
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_block($sock);
socket_bind($sock, $address, $port);
socket_listen($sock, 4);
do {
echo "Waiting for Connection...\n";
$msgsock = socket_accept($sock);
echo "Waiting for Request...\n";
$buf = socket_read($msgsock, 8192);//讀取請求
echo "Request Received: $buf\n";
$response = hand_shake($buf);
socket_write($msgsock, $response, strlen($response)); //發送響應
//開始通信...
$buf = socket_read($msgsock, 8192); //獲取前端發送的消息
//返回json數據;
if (unmask($buf) == 'getPeerInfo') {
$msg = '[
{
"vid": "1",
"vname": "楊過",
"vdata": "20.00",
"vtask": "70.00"
},
{
"vid": "2",
"vname": "小龍女",
"vdata": "40.00",
"vtask": "100.00"
},
{
"vid": "3",
"vname": "郭靖",
"vdata": "40.00",
"vtask": "100.00"
},
{
"vid": "4",
"vname": "黃蓉",
"vdata": "30.00",
"vtask": "100.00"
},
{
"vid": "5",
"vname": "歐陽鋒",
"vdata": "90.00",
"vtask": "100.00"
},
{
"vid": "6",
"vname": "洪七公",
"vdata": "90.00",
"vtask": "100.00"
},
{
"vid": "7",
"vname": "黃老邪",
"vdata": "68.00",
"vtask": "90.00"
},
{
"vid": "8",
"vname": "一燈大師",
"vdata": "30.00",
"vtask": "79.00"
},
{
"vid": "9",
"vname": "老頑童",
"vdata": "89.00",
"vtask": "98.00"
},
{
"vid": "10",
"vname": "郭襄",
"vdata": "69.00",
"vtask": "96.00"
}]';
$response_text = mask($msg);
socket_write($msgsock, $response_text, strlen($response_text));
} else {
//這里不建議用array,因為json_encode之后出現空值,可能是編碼問題。
//一旦編碼出問題,就是返回空,又不報錯
$msg = '{"error:"Invalid method"}';
$response_text = mask(json_encode($msg));
socket_write($msgsock, $response_text, strlen($response_text));
};
socket_close($msgsock);
} while (true);
socket_close($sock);
function hand_shake($buf)
{
$buf = substr($buf, strpos($buf, 'Sec-WebSocket-Key:') + 18);
$key = trim(substr($buf, 0, strpos($buf, "\r\n")));
$new_key = base64_encode(sha1($key . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));
$new_message = "HTTP/1.1 101 Switching Protocols\r\n";
$new_message .= "Upgrade: websocket\r\n";
$new_message .= "Sec-WebSocket-Version: 13\r\n";
$new_message .= "Connection: Upgrade\r\n";
$new_message .= "Sec-WebSocket-Accept: " . $new_key . "\r\n\r\n";
return $new_message;
}
//解碼數據
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;
}
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('CCn', $b1, 126, $length);
elseif ($length >= 65536)
$header = pack('CCNN', $b1, 127, $length);
return $header . $text;
}
總結
以上是生活随笔為你收集整理的php ini 长连接秒数,php使用webSocket实现Echarts长连接自动刷新的解决方案(2):后端服务端代码返回json数据...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常州大学 计算机与人工智能学院,第一届计
- 下一篇: 计算机设备全年销量excel,计算机二级