我有两个通过本地UNIX套接字进行通信的PHP脚本。一个脚本上的代码如下所示:
socket_write($client, "SomeData\n");
socket_write($client, "OtherData\n");
//wait for input, or do some 'lengthy' calculations
socket_write($client, "LastData\n");
客户端的代码就是
@socket_select($read,$write,$except,null);
foreach ($read as $socket) {
echo "Received from socket: ".socket_recv($socket, $buffer, $maxBuffer, 0)."\n";
}
我的问题是对socket_write()的两次连续调用似乎是缓冲的,因此输出如下所示:
Received from socket: SomeData
OtherData
Received from socket: LastData
显然上面并不是什么大不了的事,但实际上,我正在传递JSON对象,它们正如此碰撞:
{"response":"loginOK", "token":"123456"}{"response":"data","x":12, "y":34}
这会导致客户端出现解析错误。我无法保证某些函数会被一个接一个地调用,或者延迟介于两者之间,因此我无法将结果可靠地整理到一个JSON对象中。有没有办法让每个socket_write()调用结果到客户端的一个接收事件?