我们目前正在使用一个系统,该系统通过 TCP 接收传入的 JSON 请求并使用 JSON 进行响应。目前我已经在 PHP 中设置了我的套接字:
$socket = fsockopen($host, $port, $errno, $errstr, $timeout);
if(!$socket)
{
fwrite($socket, $jsonLoginRequest); // Authentication JSON
while(json_decode($loginResponse) == false) // We know we have all packets when it's valid JSON.
{
$loginResponse .= fgets($socket, 128);
}
// We are now logged in.
// Now call a test method request
fwrite($socket, $jsonMethodRequest);
while(json_decode($methodResponse) == false) // We know we have all packets when it's valid JSON.
{
$methodResponse .= fgets($socket, 128);
echo $methodResponse; // print response out
}
// Now we have the response to our method request.
fclose($socket);
}
else
{
// error with socket
}
这目前有效,服务器响应方法请求。但是,某些方法会像这样响应以确认调用,但稍后也会响应我所追求的结果。所以我真正需要的是一个 TCP 监听器。谁能告诉我如何像上面那样使用 fsock 编写 TCP 侦听器?
谢谢