示例#1
0
文件: Socket.php 项目: koyeo/bichon
 /**
  * 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();
 }
示例#2
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();
 }
示例#3
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();
 }
 /**
  * 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();
示例#6
0
<?php

require __DIR__ . '/../bootstrap/autoload.php';
$server = new Hoa\Websocket\Server(new Hoa\Socket\Server('tcp://127.0.0.1:8899'));
// when someone is connected
$server->on('open', function (Hoa\Core\Event\Bucket $bucket) {
    echo 'new player is joined the world', "\n";
    $data = array('type' => 'newPlayer');
    $bucket->getSource()->broadcast(json_encode($data));
    return;
});
// general stuff
$server->on('message', function (Hoa\Core\Event\Bucket $bucket) {
    $data = $bucket->getData();
    $data = json_decode($data['message']);
    $bucket->getSource()->broadcast(json_encode($data));
    return;
});
// on closing
$server->on('close', function (Hoa\Core\Event\Bucket $bucket) {
    echo 'a player leaved the world', "\n";
    return;
});
$server->run();
示例#7
0
<?php

define('DEVELOPMENT_ENVIRONMENT', true);
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(dirname(__FILE__)));
require_once ROOT . DS . '..' . DS . 'vendor' . DS . 'autoload.php';
use BreakCheck\Logger;
echo 'Iniciando servidor socket...';
$server = new Hoa\Websocket\Server(new Hoa\Socket\Server('tcp://192.168.0.101:8889'));
echo "\nServidor socket Online.\n";
$server->on('open', function (Hoa\Event\Bucket $bucket) {
    echo "Usuário conectado \n";
    $bucket->getSource()->send('PARA DE SE CONECTAR O TEMPO TODO');
    return;
});
$server->on('message', function (Hoa\Event\Bucket $bucket) {
    $dataMsg = $bucket->getData();
    echo "\nRecebido: \n";
    $arrayMsg = json_decode($dataMsg['message'], true);
    var_dump($arrayMsg);
    $action = array_shift($arrayMsg);
    // echo 'Executando ' . $action . "\n";
    $model = new BreakCheck\Map();
    $erro = '';
    try {
        $controller = new BreakCheck\SocketController($model, $action, $arrayMsg);
        $controller->{$action}();
    } catch (\Throwable $e) {
        // var_dump($dataMsg);
        $erro = $e->getMessage();
        echo $e->getMessage() . "\n";