Click here to Skip to main content
15,881,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
does anyone know how to avoid WebSocket\ConnectionException from a receive_fragment error? I send a json through a websocket and added a while to receive the response, but it seems that I only get a fragment of it and throws a error afterwards.

{"protocol":"UNKNOWN"," ["class"]=> string(14) "WebSocket\Base" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(84) "textalk\stocks\vendor\textalk\websocket\lib\Base.php" ["line"]=> int(135) ["function"]=> string(16) "receive_fragment" ["class"]=> string(14) "WebSocket\Base" ["type"]=> string(2) "->" ["args"]=> array(0) { } } [2]=> array(6) { ["file"]=> string(61) "F:\xampp-php72\htdocs\testcodes\textalk\stocks\dailyorder.php" ["line"]=> int(51) ["function"]=> string(7) "receive" ["class"]=> string(14) "WebSocket\Base" ["type"]=> string(2) "->" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL } object(WebSocket\ConnectionException)#7 (7) {"Empty read; connection dead?

The Code :

$client->send( json_encode($dailyOrder) );

while (true) {

try {

    $dailyorder = json_decode( $client->receive() );

    var_dump($dailyorder);

   } catch (\WebSocket\ConnectionException $e) {

    var_dump($e);

}
If anyone has any idea it would really help!


What I have tried:

I have tried seen the error but this is all I got.
Posted
Comments
Richard MacCutchan 27-Aug-20 4:04am    
The message is telling you that something broke the connection between your client and the server.
bigbits 27-Aug-20 8:34am    
true, the problem is that we cannot use javascript for example. We have to do it all in php so while on javascript works on php there is something missing there.
Richard MacCutchan 27-Aug-20 9:02am    
I do not think you should be calling json_decode on received fragments, as the decoder will be expecting a complete message. You should collect all the data into a string and then call json_decode on the complete message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900