send one message but receive 2 chunks
VoldemarOzzy opened this issue · 3 comments
Hi, could someone please help as I am stuck. I am starting server
$SocketServer = new React\Socket\Server("host:port", $loop);
$SocketServer->on('connection', function (ConnectionInterface $connection) {
$connection->on('data', function ($data){
echo "\$data=$data";
});
});
When I send {"PING":"1561715200.438945055"}
I have two lines :
Line 1:
$data={
Line 2:
$data="PING":"1561715200.438945055"}
Unfortunately at this stage it is difficult to understand if line 1 was from previous command or not.
Why I have 2 lines? Am i misunderstoond a concept or something? If anybody could clarify that would be great! Thank you.
Hey so simply put this is how it works. We get the data in chunks from the kernel and don't know the supposed format. So we pass it on as is. My guess is that you're using JSON with new lines between them so this package might be interesting: https://github.com/clue/reactphp-ndjson
@VoldemarOzzy What @WyriHaximus said :-)
What you're looking for is called "message framing" which isn't provided by TCP/IP on its own. You can use some delimiters between messages (like NDJSON) or use length indicators like netstrings, see also #166 for possible duplicate.
I hope this helps! 👍
Thank you WyriHaximus!
Thank you clue!
for pointing me to right direction! I have just fixed this. Appreciate!