Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

初次使用后的一些体验与几点疑惑,期望得到大佬们的一些帮助,谢谢! #195

Open
kalordy opened this issue Jan 10, 2024 · 0 comments

Comments

@kalordy
Copy link

kalordy commented Jan 10, 2024

Describe your issue
php7.4 websocket-php1.5

体验1、parse_url 遇到控制符会自动在请求URI参数结尾增添下划线,依据PHP文档信息:”注意组件中的控制字符(参见 ctype_cntrl())替换为下划线(_)。“,所以不做处理直接使用会报错。
代码块:

  /**
   * Perform WebSocket handshake
   */
  protected function connect(): void
  {
      $url_parts = parse_url($this->socket_uri);  // 有潜在报错可能

疑惑1、不管多次循环请求还是单次请求send,在 receive()接收服务端时候,遇到 stream_get_meta_data($this->socket)中的 $meta['time_out'] = true; 返回的结果为:An error has occurred: Client read timeout
代码块:

protected function read(string $length): string
    {
        $data = '';
        while (strlen($data) < $length) {
            $buffer = @fread($this->socket, $length - strlen($data));

            if (!$buffer) {
                $meta = stream_get_meta_data($this->socket);var_dump($meta);
                if (!empty($meta['timed_out'])) {
                    $message = 'Client read timeout';
                    $this->logger->error($message, $meta);
                    throw new TimeoutException($message, ConnectionException::TIMED_OUT, $meta);
                }

以下为本人的代码块:
$xfyun 含参数的完整 URL,协议是ws://,在postman请求链接没有问题
$filePath 文件路径正常

  $TrClient = new Client($xfyun, [ 'timeout' => 2]);
  $check = $TrClient->receive();
  $result = json_decode($check, true);

  $handle = fopen($filePath, "rb");
  if($result['action'] === 'started' && $result['code'] === '0' && $handle)
  {
        // var_dump(stream_get_meta_data($handle));

        $chunkSize = 1280;
        // while(!feof($handle))
        // {
           $fileData = fread($handle, $chunkSize); // 可以得到 数据
           $TrClient->binary($fileData);
           $revText  = $TrClient->receive(); // Client read timeout

        // }
     $TrClient->binary('{"end: true"}');  // 内容上传结束
  }

  $TrClient->close();
  fclose($handle);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant