Skip to content

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

@kalordy

Description

@kalordy

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions