Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to do this in php

websocket.onmessage = function(evt) {  
    console.log("Message Received: " + evt.data);  
    }; 


Can anyone help ?

What I have tried:

My php code:

// SEND TO THE SERVER THE JSON
$client->send( json_encode($info) );

// RECEIVED THE INFORMATION
$data = json_decode( $client->receive() );

$x=0; // JUST A COUNTER SO THE WHILE WON'T LOOP FOREVER

// TRIED WHILE BUT NO GOOD
while ($x<10) {
​
sleep(1);
echo $x."<br>";
echo $client->receive();

}


The result is a mix of the information I need and error;

7a6efd45-8a35-4530-a477-b25bd5b584ff0
{"success":true}
{"values":{"0":"16:59:21","3":"22,66","4":"22,66"},"type":"QuoteType","parameter":"petr4"}
{"values":{"0":"16:59:22","3":"22,77","4":"22,77"},"type":"QuoteType","parameter":"petr4"}


AND THE ERROR MESSAGE
Fatal error: Uncaught WebSocket\ConnectionException: Empty read; connection dead? Stream state: {
"crypto":{"protocol":"TLSv1.2","cipher_name":"ECDHE-RSA-AES128-GCM-SHA256","cipher_bits":128,
"cipher_version":"TLSv1.2"},"timed_out":true,"blocked":true,"eof":false,"stream_type":"tcp_socket\/ssl",
"mode":"r+","unread_bytes":0,"seekable":false} in F:\xampp-php72\...\websocket\lib\Base.php:269
Stack trace: #0 F:\xampp-php72\htdocs\testcodes\call\vendor\textalk\websocket\lib\Base.php(143):
WebSocket\Base->read(2) #1 F:\xampp-php72\htdocs\testcodes\call\vendor\textalk\websocket\lib\Base.php(135):
WebSocket\Base->receive_fragment() #2 F:\xampp-php72\htdocs\...\example.php(57):
WebSocket\Base->receive() #3 {main} thrown in F:\xampp-php72\htdocs\...\websocket\lib\Base.php on line 269
Posted
Updated 8-Sep-20 8:02am
Comments
Sandeep Mewara 2-Sep-20 23:39pm    
BTW, you wrote: $x=0; // JUST A COUNTER SO THE WHILE WON'T LOOP FOREVER
but in while loop, no $x changed. It's an infinite while loop.
Sandeep Mewara 2-Sep-20 23:40pm    
can you explain: The result is a mix of the information I need and error;

you get combination as result? or saying when all good you get success or else when not - you get an error.
bigbits 2-Sep-20 23:47pm    
The success is the connection and the token. I tried to simplify the example but maybe this extra info wasn't necessary ( the token and success ).

Basically, I as a websocket client send an information and I need to keep the connection alive and receive any updates from the websocket server ( from a third party ).

I tried the while and inside the while call the $client->receive(); but the error that I get 99% of the times are:

Uncaught WebSocket\ConnectionException: Empty
WebSocket\Base->receive_fragment()

Sorry if I made it more confused.
bigbits 2-Sep-20 23:42pm    
it does have a $x++; it just that I copied and paste quick. Before the condition was true.

What it's important for me is to replicate the same receiving state.

1 solution

The error is because your loop, even if you had the $x++ counter activated, could (and does) still try to read when there's no data to be read.

Your condition should test about the readability of the data [
$client->receive();
] and not just some counter that not only may let you overshoot (as you do) but also not necessarily read all available data if $x increments (when you increment it, of course) the arbitrary constraint of '10'
 
Share this answer
 

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