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.
"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."
First option is not the right way simply because it's not the way socket communication works.
If you are receiving, for example, 5 kB of data and you set your buffer to be large enough, let's say 10 kB, you cannot expect that in one call to $stream->on('data', function ($data) { ...
you will receive all 5 kB.
You must do three things:
$stream->on('data', function ($data) { ...
. When the size of concatenated data is >=4
then you read the size of the message.Good idea is that you set a timer for loop so you can wait for the whole message to be received for a limited amount of time. It could happen that connection between client and server gets broken during transmission and if you do not have a timeout logic your loop will wait forever for the whole message to be received.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With