-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't connect more than one websockets simultaneously #52
Comments
Can you post all of your code so I can reproduce please? What's given isn't enough to go on. |
Updated original post as requested |
The connect function is an abstraction to hide the event loop, which is fine for one connection, but as you've found doesn't work for multiple as it creates an event loop per call. You need to inject a single event loop into each connection. You will need to use the Connector class instead, as seen in the second example of the README to achieve multiple connections. |
This one worked for multiple connections perfectly. But the problem of freezing still exists If remote server is not running at the time of creating connection (calling connect_to_websocket () in below code) then the script freezes, it doesn't connect to server when server comes online, which needs force closing/canceling the script And where to edit or pass timeout? <?php
require __DIR__ . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$connector = new Ratchet\Client\Connector($loop);
$websocketURLs[] = ['url' => 'wss://echo.websocket.org', 'service' => 'websocket'];
$websocketURLs[] = ['url' => 'ws://echo.socketo.me:9000', 'service' => 'socketo'];
foreach( $websocketURLs as $v ){
connect_to_websocket ( $v['url'], $v['service'], $connector );
}
function connect_to_websocket( &$url, &$service, &$connector){
$responseCount = 0;
echo "\nOK... Will connect to $url at $service\n\n";
$connector( $url, [], [] )->then(function ($connection) use ( &$url, &$service, &$connector, &$responseCount ) {
echo "Connected to $service";
echo "\n============================\n";
$connection->send( "Hello" );
$connection->on('message', function($message) use ($connection, &$url, &$service, &$responseCount) {
$responseCount++;
$message = (string) $message;
echo "$service - $message - Response no. $responseCount - Will disconnect at response no. 10";
echo "\n---------------------------------------------------------------------------\n";
if( $responseCount == 10)
$connection->close();
$connection->send( $message );
});
$connection->on('close', function($code = null, $reason = null) use ( &$url, &$service, &$connector) {
echo "Websocket Connection to $service is closed ({$code} - {$reason}). Reconnecting...\n";
connect_to_websocket( $url, $service, $connector );
});
}, function ($e) {
exit("Could not connect to websocket: {$e->getMessage()}. Exiting...\n");
});
}
$loop->run(); |
I am also having the same problem, I will attempt the fix mentioned above require __DIR__.'/vendor/autoload.php';
// The console should show both ETHBTC and BNBBTC if two websocket connections are established
\Ratchet\Client\connect('wss://stream.binance.com:9443/ws/ethbtc@depth')->then(function($conn) {
$conn->on('message', function($msg) use($conn) {
echo "{$msg}\n";
});
$conn->on('close', function($code = null, $reason = null) {
echo "WebSocket Connection closed ({$code} - {$reason})\n";
});
}, function($e) {
echo "Could not connect: {$e->getMessage()}\n";
});
\Ratchet\Client\connect('wss://stream.binance.com:9443/ws/bnbbtc@depth')->then(function($conn) {
$conn->on('message', function($msg) use($conn) {
echo "{$msg}\n";
});
$conn->on('close', function($code = null, $reason = null) {
echo "WebSocket Connection closed ({$code} - {$reason})\n";
});
}, function($e) {
echo "Could not connect: {$e->getMessage()}\n";
}); |
Using the loop above fixed it for me. Thank you |
No, everything needs to run on the same event loop. |
Unable to connect to two or more WebSocket services simultaneously,
The second request goes in waiting and instantly connect when first connection closes
here is what I did
This only connects to one resource at a time and second connection goes to waiting, as the first connection closed, second (waiting) connection connects instantly.
Also, there is unlimited timeout, how to provide or edit timeout? If remote server is not running at the time of creating connection
(calling connect_to_websocket () in above code)
then the script freezes, it doesn't connect to server when server comes online, which needs force closing/canceling the scriptPHP CLI gives this
The text was updated successfully, but these errors were encountered: