Using a very simple websocket interface, I'm running into a bit of trouble -- if I'm stuck in a loop with a long execution time, no data is flushed out of the sockets (despite calling $client->send() from within the loop).Here's the problematic snippet of code from my class:public function onMessage(ConnectionInterface $from, $msg) {$from->send("Starting Up");$x=0;while($x<10){echo ($x);$from->send($x);$x = $x+1;sleep(1);}}If I run this, none of the output (including the first "Starting Up" message) is sent over the socket until after the loop has completed. Curiously, however, if I look at my console output on the server, echo seems to work just fine.Has anyone else encountered this? Is there a way to flush the buffers within the loop?--
use React\Stream\Stream as Stream;
$ping = new Stream(popen("ping -t 127.0.0.1",'r'),$server->loop);
$ping->on('data',function($data) use ($from){
$from->send($data);
});
$f4vpp = new Stream(popen("f4vpp -v -i in.f4v -o out.mp4",'r'),$server->loop);
$f4vpp->on('data',function($data) use ($from){
$from->send($data);
});
| <?php | |
| /* | |
| * To change this license header, choose License Headers in Project Properties. | |
| * To change this template file, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| namespace App; | |
| use Ratchet\MessageComponentInterface; | |
| use Ratchet\ConnectionInterface; | |
| /** | |
| * Description of Socket | |
| * | |
| * @author <mostafa...@hotmail.com> | |
| */ | |
| class Socket implements MessageComponentInterface{ | |
| /** | |
| * | |
| * @var $clients array of each client | |
| */ | |
| protected $clients; | |
| protected $loop; | |
| /** | |
| * constructor | |
| */ | |
| public function __construct(\React\EventLoop\LoopInterface $loop) { | |
| $this->loop = $loop; | |
| $this->clients = new \SplObjectStorage; | |
| } | |
| /** | |
| * | |
| * @param ConnectionInterface $conn attaches clients | |
| */ | |
| public function onOpen(ConnectionInterface $conn) { | |
| echo "\nNew Client Connected...\n"; | |
| $this->clients->attach($conn); | |
| } | |
| /** | |
| * | |
| * @param ConnectionInterface $from | |
| * @param $msg | |
| */ |
| public function onMessage(ConnectionInterface $from, $msg) { | |
| echo "\nNew Message: " . $msg . "\n"; | |
| foreach($this->clients as $clientSocket){ | |
| $this->loop->futureTick(function() use ($clientSocket){ | |
| $clientSocket->send("\ndata data start\n"); | |
| }); | |
| $this->loop->futureTick(function() use ($clientSocket){ | |
| $clientSocket->send("\ndata data two\n"); | |
| }); | |
| $stream = new \React\Stream\ReadableResourceStream(fopen(storage_path('logs/laravel.log'), 'r'), $this->loop); | |
| $loop = $this->loop; | |
| $stream->on('data', function($data) use ($clientSocket, $loop){ | |
| $loop->futureTick(function() use ($clientSocket, $data){ | |
| $returnValue = file_get_contents(storage_path('logs/laravel.log')) . | |
| file_get_contents(storage_path('logs/laravel.log')) . | |
| file_get_contents(storage_path('logs/laravel.log')) . | |
| file_get_contents(storage_path('logs/laravel.log')) . | |
| file_get_contents(storage_path('logs/laravel.log')) . | |
| file_get_contents(storage_path('logs/laravel.log')); | |
| $clientSocket->send($returnValue); | |
| $clientSocket->send($data); | |
| }); | |
| }); | |
| $stream->on('end', function() use ($clientSocket, $loop){ | |
| $loop->futureTick(function() use ($clientSocket){ | |
| $clientSocket->send('finished'); | |
| }); | |
| }); | |
| $this->loop->run(); | |
| } | |
| } | |
| /** | |
| * | |
| * @param ConnectionInterface $conn | |
| */ | |
| public function onClose(ConnectionInterface $conn) { | |
| echo "\nConnection Closed \n"; | |
| $this->clients->detach($conn); | |
| } | |
| /** | |
| * | |
| * @param ConnectionInterface $conn | |
| * @param \Exception $e | |
| */ | |
| public function onError(ConnectionInterface $conn, \Exception $e) { | |
| echo "\nError on Connection: {$e->getMessage()} on line {$e->getLine()} in file {$e->getFile()} \n"; | |
| $conn->close(); | |
| } | |
| } |