I have to connect a remote server with asynchronous socket connection and retrieve data. I can connect but there is a problem.
Packages are sending by pieces. I have two options; I can set a buffer and get whole package in one piece or combine pieces when all transfer done. I think first option (buffer thing) is the right way.
I'm defining a buffer size but it is not working in first part. In other parts, it works but with this method I can not get whole package in one piece because first part limited with 5,24 Kb.
You can find my code below:
$loop = React\EventLoop\Factory::create();
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dns = $dnsResolverFactory->createCached('8.8.8.8', $loop);
$connector = new React\SocketClient\Connector($loop, $dns);
$connector->create( ENDPOINT_IP , ENDPOINT_PORT )->then(function (React\Stream\Stream $stream) use ($loop) {
$command = '{C:"EL",bmId:43,inst:"my_instance",tok:"my_token"}';
$command_length = strlen($command);
$command_length = pack("N", $command_length);
$stream->write($command_length);
$stream->write($command);
$stream->bufferSize = 999999;
$stream->on('data', function ($data) {
$package = substr($data, 0, 4);
$unpack = unpack('N', $package); // I'm getting whole package size
echo $data;
});
});
$loop->run();
I tried to define a buffer size under $stream->on('data', function ($data) {
line but as you guess it failed. I don't know how to handle it right way.
Thanks in advance.