PHP作为客户端连接到NodeJS Web套接字



需要我的laravel应用程序连接到基于nodejs的套接字服务器。该函数需要传递自定义标头以进行身份验证,并保持每1分钟ping一次连接以防止与套接字服务器断开连接

有人能推荐任何好的库来实现上述功能吗。在这里,PHP充当客户端

提前感谢

感谢您的建议,我最终使用了棘轮/棘爪客户端,它运行良好:

示例:

$headers = ['HEADER' => 'Value'];
$ip = '1.1.1.1';
RatchetClientconnect($ip,[],$headers)->then(function($conn) use ($payload) {
  $conn->on('message', function($msg) use ($conn) {
      $response = json_decode($msg, TRUE);
      var_dump($response);
      $conn->close();
  });
  $conn->send('Hello!');
  $conn->on('close', function ($code = null, $reason = null) use ($connector, $loop, $app) {
    echo "Connection closed ({$code} - {$reason})n";
  });
}, function ($e) {
    echo "Could not connect: {$e->getMessage()}n";
});

最新更新