I'm using popen with fgets to read the output of tcpdump asynchronously.
The below code should be run in the command line, not with apache and viewing it in your browser.
$handle = popen('tcpdump -nnX', 'r');
while (true) {
$output = fgets($handle);
print $output . "\n";
}
The problem arises when I try to output this information via websockets.
Websockets also use an infinite loop (for managing its sockets, ticks, and messages).
It looks something like:
while (true) {
@socket_select($read,$write,$except,1);
foreach ($read as $socket) {
if ($socket == $this->master) {
$client = socket_accept($socket);
...
I send data through the websocket with $websocket->sendToAll($message);.
I can't put the while loops one after the other because it will only run whichever loop I put first,
while (true) { A() }; while (true) { B() };
B() will never be calledI can't merge the while loops, because the websockets slows down the reading of popen, and vise versa.
while (true) { A(); B(); }
if B is taking a long time to finish, A will be slow to run.
What can I do in this situation? I'm open to the idea of threads, communication between forked scripts, or anything else.