0
我正在使用rachetphp为api服务器创建一个客户端。 但我有一个问题,当我的连接关闭,无论原因,我无法自动重新连接。如何在ratchetphp上自动重新连接客户端?
这里的lib我使用: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();
我想尝试重新连接的连接结束后每隔几秒钟。 任何想法?
很不错的!与此我可以重新连接后,'错误'太'或'关闭'事件。 – LordSoldier