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