简体   繁体   English

PHP Websocket 客户端 - 保持连接打开

[英]PHP Websocket Client - Keep connection open

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.我在 Laravel 应用程序中使用PHP-WSS ,需要保持 websocket 客户端打开以接收来自 websocket 服务器的各种消息。

So far I built a CLI php script that I can execute and wait for messages to arrive.到目前为止,我构建了一个 CLI php 脚本,我可以执行该脚本并等待消息到达。

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?问题是,要保持连接对可能从服务器发送的任何消息保持打开状态,使用 while(true) 循环执行以下操作是否是一种好方法? 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.更新:任何使用 PHP-WSS 的人我在 BroadCast 方法中的 Connection.php 中发现了一个错误。

Original function tries to send on a dead connection, which shows the following error Empty read;原始函数尝试在死连接上发送,显示以下错误 Empty read; connection dead?连接死了? (Note the EOF = true) (注意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';
        }
    }
}

Since the php script that renders the page finishes execution, you need to implement websockets on the client itself using a js script由于渲染页面的php脚本执行完毕,需要在客户端自己使用js脚本实现websockets

<?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>

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)在 WscMain.php 文件中,在受保护的函数读取方法中,删除/注释掉(或根据您的喜好和需要更改)以下错误处理程序(大约第 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. PS:对于我的情况,我还在配置中设置了一个非常小的(1 秒)读取超时,如下所示。 (This test function is being executed from a CLI PHP script) (此测试函数是从 CLI PHP 脚本执行的)

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.希望这对某人有所帮助,并感谢所有提供帮助的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM