Reputation: 3181
I am using PHP-WSS in a laravel application and need to keep a websocket client open to receive various messages from the websocket server.
So far I built a CLI php script that I can execute and wait for messages to arrive.
I built the following function to test...
The question is, to keep the connection open for any messages that might be sent from the server, is it a good approach to do it as below using a while(true) loop? Is there anyway I can do this better? (To me it looks dirty and wish to improve it and do it properly)
function testWebsocketClient() {
$url = 'wss://example.com/?token=xyz123456';
$client = new WebSocketClient($url, new ClientConfig());
while(true){
sleep(5);
$client->send('test');
$return = $client->receive(); // test received OK
}
return $return;
}
UPDATE: Anyone using PHP-WSS I found a bug in Connection.php in broadCast method.
Original function tries to send on a dead connection, which shows the following error Empty read; connection dead? (Note the EOF = true)
public function broadCast(string $data): void
{
foreach ($this->clients as $client) {
if (is_resource($client) ) { // check if not yet closed/broken etc
fwrite($client, $this->encode($data));
} else {
echo 'Skipping a closed connection';
}
}
}
I changed it to
public function broadCast(string $data): void
{
foreach ($this->clients as $client) {
//echo PHP_EOL. stream_get_status($client) .PHP_EOL;
$clientMeta = ( stream_get_meta_data($client) );
$clientEof = $clientMeta['eof'];
if (is_resource($client) && $clientEof == false ) { // check if not yet closed/broken etc
fwrite($client, $this->encode($data));
} else {
echo 'Skipping a closed connection';
}
}
}
Upvotes: 1
Views: 5203
Reputation: 3181
After an entire week struggling with this I found a solution...
There is an error handler that throws an exception if the buffer is empty, thus bring the client to an end.
In WscMain.php file, inside protected function read method, remove/comment out (or change to your liking and needs) the following error handler (approx line 302)
if(false){ // added a false condition so the code within doesn't execute
if (false && $buff === false ) {
$metadata = stream_get_meta_data($this->socket);
throw new ConnectionException(
'Broken frame, read ' . strlen($data) . ' of stated '
. $len . ' bytes. Stream state: '
. json_encode($metadata),
CommonsContract::CLIENT_BROKEN_FRAME
);
}
if (false && $buff === '') {
//print_r($this->socket);
$metadata = stream_get_meta_data($this->socket);
throw new ConnectionException(
'Empty read; connection dead? Stream state: ' . json_encode($metadata). ' '.PHP_EOL.( (int) $this->socket ) ,
CommonsContract::CLIENT_EMPTY_READ
);
}
}
PS: For my case, I also made a very small (1 sec) read timeout in the config, as follows. (This test function is being executed from a CLI PHP script)
function runWebsocketClientTest(){
echo PHP_EOL;
echo __FUNCTION__;
echo PHP_EOL;
$timeout = 1;
$return = '';
$config = new ClientConfig();
$config->setTimeout($timeout);
$client = new WebSocketClient('ws://localhost:8000', $config);
while($client->isConnected()){
if($client->isConnected()){ // this might be unnecessary
echo $client->receive();
}
}
}
Hope this helps someone and thanks to all those that assisted.
Upvotes: 0
Reputation: 4381
Since the php script that renders the page finishes execution, you need to implement websockets on the client itself using a js script
<?php
// here output your page
wss_path = 'wss://example.com/?token=xyz123456';
?>
// JS script (see its working results in console by pressing `F12`)
<script>
let socket = new WebSocket(<?= wss_path ?>);
socket.onopen = function(e) {
console.log('[open] Connection opened');
socket.send("Hi! I am a new web-socket client.");
};
socket.onmessage = function(event) {
const message = JSON.parse(event.data);
console.log('From the server:', message);
};
socket.onclose = function(event) {
if (event.wasClean) {
console.log(`[close] Connetion closed clearly, code=${event.code} reason=${event.reason}`);
} else {
console.log('[close] Сonnection closed unexpectedly');
}
};
socket.onerror = function(error) {
console.log(`[error] ${error}`);
};
</script>
Upvotes: 2