Пример #1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $server = new \Hoa\Websocket\Server(new \Hoa\Socket\Server('tcp://127.0.0.1:8889'));
     $server->getConnection()->setNodeName('\\App\\Models\\AnsibleNode');
     $server->on('open', function (\Hoa\Event\Bucket $bucket) {
         $this->info('new connection');
         return;
     });
     $server->on('message', function (\Hoa\Event\Bucket $bucket) {
         $data = json_decode($bucket->getData()['message']);
         switch ($data->command) {
             case webSocket::SETJOBID:
                 $node = $bucket->getSource()->getConnection()->getCurrentNode();
                 $node->setJobID($data->jobid);
                 break;
             case webSocket::BROADCASTIF:
                 $this->info($data->jobid);
                 $bucket->getSource()->broadcastIf(function ($node) use($data) {
                     return $node->getJobID() == 0 || $node->getJobID() == $data->jobid;
                 }, $data->msg);
                 break;
             default:
                 $bucket->getSource()->broadcast($data->msg);
         }
         //$this->info('message: ' . json_encode($data));
         return;
     });
     $server->on('close', function (\Hoa\Event\Bucket $bucket) {
         $this->info('connection closed');
         return;
     });
     $server->run();
 }
Пример #2
0
 /**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     set_time_limit(3600);
     echo '<div style="display:none;"';
     $websocket = new \Hoa\Websocket\Server(new \Hoa\Socket\Server('tcp://127.0.0.1:8889'));
     $websocket->on('open', function (\Hoa\Core\Event\Bucket $bucket) {
         echo 'new connection', "\n";
         return;
     });
     $websocket->on('message', function (\Hoa\Core\Event\Bucket $bucket) {
         $data = $bucket->getData();
         if ($data['message'] == '_close_') {
             echo '</div>';
             exit;
         }
         echo '> message ', $data['message'], "\n";
         $bucket->getSource()->broadcast($data['message']);
         echo '< echo', "\n";
         return;
     });
     $websocket->on('close', function (\Hoa\Core\Event\Bucket $bucket) {
         echo 'connection closed', "\n";
         return;
     });
     $websocket->run();
 }
Пример #3
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $server = new \Hoa\Websocket\Server(new \Hoa\Socket\Server('tcp://192.168.0.51:8889'));
     //Manages the message event to get send data for each client using the broadcast method
     $server->on('message', function (\Hoa\Core\Event\Bucket $bucket) {
         $data = $bucket->getData();
         echo 'message: ', $data['message'], "\n";
         $bucket->getSource()->broadcast($data['message']);
         return;
     });
     //Execute the server
     $server->run();
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $server = new \Hoa\Websocket\Server(new \Hoa\Socket\Server('tcp://192.168.0.51:8889'));
     //Manages the message event to get send data for each client using the broadcast method
     $server->on('message', function (\Hoa\Core\Event\Bucket $bucket) {
         try {
             $data = $bucket->getData();
             $inputMessage = json_decode($data['message'], true);
             $currentNodeId = $bucket->getSource()->getConnection()->getCurrentNode()->getId();
             if (!empty($inputMessage['user'])) {
                 self::$userNodes[$inputMessage['user']] = $currentNodeId;
                 return;
             }
             $nodesUsers = array_flip(self::$userNodes);
             $message = Message::create(['message' => $inputMessage['message'], 'user_from' => $nodesUsers[$currentNodeId], 'user_to' => $inputMessage['to']]);
             //                $nodes = $bucket->getSource()->getConnection()->getNodes();
             //
             //                foreach ($nodes as $node) {
             //
             //                    $userId = $nodesUsers[$node->getId()];
             //                    if (in_array($userId, [$message->user_from, $message->user_to])) {
             //
             //                    }
             //                }
             $bucket->getSource()->broadcastIf(function ($node) use($nodesUsers, $message) {
                 $userId = $nodesUsers[$node->getId()];
                 return in_array($userId, [$message->user_from, $message->user_to]);
             }, $message);
         } catch (\Exception $e) {
             echo $e->getMessage() . "\n" . $e->getTraceAsString();
         }
         return;
     });
     //Execute the server
     $server->run();
 }
Пример #5
0
<?php

/*Create a server variable with the link to the tcp IP and custom port you need to
specify the Homestead IP if you are using homestead or, for local environment using
WAMP, MAMP, ... use 127.0.0..1*/
$server = new Hoa\Websocket\Server(new Hoa\Socket\Server('tcp://127.0.0.1'));
//Manages the message event to get send data for each client using the broadcast method
$server->on('message', function (Hoa\Core\Event\Bucket $bucket) {
    $data = $bucket->getData();
    echo 'message: ', $data['message'], "\n";
    $bucket->getSource()->broadcast($data['message']);
    return;
});
//Execute the server
$server->run();