src/Bot.php
/**
     * Init the bot and set up the loop/actions for the WebSocket
     */
Init the bot and set up the loop/actions for the WebSocket
Init the bot and set up the loop / actions for the WebSocket
public function init()
    {
        $connector = new Connector($this->loop, new \React\Socket\Connector($this->loop));
        $connector($this->wssUrl)->then(function (WebSocket $conn) {
            $this->connection = $conn;
            $this->getaway = $getaway = new getaway($conn, $this->loop, $this->token);
            $this->getaway->send_log = $this->send_log;
            $getaway->addDispatch($this->dispatch);
            $conn->on('message', function (MessageInterface $msg) use ($conn, $getaway) {
                $json = json_decode($msg);
                if (isset($json->d->session_id)) {
                    $this->session_id = $json->d->session_id;
                }
                $getaway->action($json);
            });
            $conn->on('close', function ($code = null, $reason = null) use ($conn) {
                echo "\nConnection closed ({$code} ". $reason != null ? "- {$reason})\n" : "\n";
                if (!$this->reconnect) {
                    die();
                } else {
                    echo "We are begin of a rate limit, connect retrying after 60 seconds.";
                    $this->loop->addTimer(60, function () use ($conn) {
                        $conn->send(json_encode([
                            "op" => 6,
                            "d" => [
                                "token" => $this->getBot()->token,
                                "session_id" => $this->getBot()->session_id,
                                "seq" => 1337
                            ]
                        ]));});
                }
            });
        }, function (Exception $e) {
            echo "Could not connect: {$e->getMessage()}\n";
            $this->stop();
        });
        return null;
    }
public function init ( ) { $ connector = new Connector ( $ this -> loop , new \ React \ Socket \ Connector ( $ this -> loop ) ) ; $ connector ( $ this -> wssUrl ) -> then ( function ( WebSocket $ conn ) { $ this -> connection = $ conn ; $ this -> getaway = $ getaway = new getaway ( $ conn , $ this -> loop , $ this -> token ) ; $ this -> getaway -> send_log = $ this -> send_log ; $ getaway -> addDispatch ( $ this -> dispatch ) ; $ conn -> on ( 'message' , function ( MessageInterface $ msg ) use ( $ conn , $ getaway ) { $ json = json_decode ( $ msg ) ; if ( isset ( $ json -> d -> session_id ) ) { $ this -> session_id = $ json -> d -> session_id ; } $ getaway -> action ( $ json ) ; } ) ; $ conn -> on ( 'close' , function ( $ code = null , $ reason = null ) use ( $ conn ) { echo " \n Connection closed ( { $ code } " . $ reason != null ? " -  { $ reason } ) \n " : " \n " ; if ( ! $ this -> reconnect ) { die ( ) ; } else { echo " We are begin of a rate limit, connect retrying after 60 seconds. " ; $ this -> loop -> addTimer ( 60 , function ( ) use ( $ conn ) { $ conn -> send ( json_encode ( [ " op " => 6 , " d " => [ " token " => $ this -> getBot ( ) -> token , " session_id " => $ this -> getBot ( ) -> session_id , " seq " => 1337 ] ] ) ) ; } ) ; } } ) ; } , function ( Exception $ e ) { echo " Could not connect:  { $ e -> getMessage ( ) } \n " ; $ this -> stop ( ) ; } ) ; return null ; }
Init the bot and set up the loop/actions for the WebSocket
Init the bot and set up the loop / actions for the WebSocket