【发布时间】:2017-08-27 23:03:53
【问题描述】:
我正在使用 rachetphp 为 api 服务器创建客户端。 但是我有一个问题,当我的连接关闭时,无论什么原因,我都无法自动重新连接。
这里是我使用的库:https://github.com/ratchetphp/Pawl
<?php
require __DIR__ . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$connector = new Ratchet\Client\Connector($loop);
$connector('ws://127.0.0.1:9000', ['protocol1', 'subprotocol2'], ['Origin' => 'http://localhost'])
->then(function(Ratchet\Client\WebSocket $conn) {
$conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
echo "Received: {$msg}\n";
$conn->close();
});
$conn->on('close', function($code = null, $reason = null) {
echo "Connection closed ({$code} - {$reason})\n";
});
$conn->send('Hello World!');
}, function(\Exception $e) use ($loop) {
echo "Could not connect: {$e->getMessage()}\n";
$loop->stop();
});
$loop->run();
我想在连接关闭后每隔几秒尝试重新连接。 有什么想法吗?
【问题讨论】:
标签: php sockets client wss reactphp