GithubHelp home page GithubHelp logo

Comments (7)

cboden avatar cboden commented on August 29, 2024

Can you post all of your code so I can reproduce please? What's given isn't enough to go on.

from pawl.

nazreinkaram avatar nazreinkaram commented on August 29, 2024

Updated original post as requested

from pawl.

cboden avatar cboden commented on August 29, 2024

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.

from pawl.

nazreinkaram avatar nazreinkaram commented on August 29, 2024

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();

image

from pawl.

jaggedsoft avatar jaggedsoft commented on August 29, 2024

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";
});

from pawl.

jaggedsoft avatar jaggedsoft commented on August 29, 2024

Using the loop above fixed it for me. Thank you
Is it possible to use more than one loop?

from pawl.

kelunik avatar kelunik commented on August 29, 2024

No, everything needs to run on the same event loop.

from pawl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.