bind() 공개 메소드

Start the server
public bind ( )
예제 #1
0
 public function launch()
 {
     $server = new WebSocketServer($this->config['url'], $this->loop, $this->logger);
     $this->loop->addPeriodicTimer($this->config['delay_try_reconnect'], function () {
         $timer = 0;
         foreach ($this->bots_retrying as $tb_id => $true) {
             $this->loop->addTimer($timer++, function () use($tb_id) {
                 $this->logger->debug('Try to reconnect client for bot ' . $tb_id);
                 $this->addSlackClient($this->bots[$tb_id]);
             });
         }
     });
     $this->loop->addPeriodicTimer($this->config['delay_ping'], function () {
         foreach ($this->bots_connected as $tb_id => $connected) {
             if ($connected) {
                 $always_connected = $this->bots[$tb_id]->clientSendPing();
                 if (!$always_connected) {
                     $this->logger->warn('Ping fail set to retry for bot ' . $tb_id);
                     $this->setToRetry($this->bots[$tb_id]);
                 }
             }
         }
     });
     $server->on('message', function (WebSocketTransportInterface $user, WebSocketMessage $message) {
         $data = json_decode($message->getData(), true);
         $this->logger->debug("Bot2hook websocket server receive message " . $message->getData());
         if (is_array($data) && isset($data['type'])) {
             switch ($data['type']) {
                 case 'add_bot':
                     $this->logger->debug("New bot receive via incomming webhook " . json_encode($data['bot']));
                     $bot = new Bot($data['bot']);
                     $this->addSlackClient($bot);
                     break;
                 case 'status':
                     $user->sendString(json_encode(['bot2hook' => ['bots' => $this->bots, 'bots_connected' => $this->bots_connected, 'bots_retrying' => $this->bots_retrying]]));
                     break;
                 case 'reporting':
                     $user->sendString(json_encode(['memory' => ['usage' => memory_get_usage(true), 'peak_usage' => memory_get_peak_usage(true), 'limit' => $this->getMemoryLimit()], 'bots_count' => ['connected' => count($this->bots_connected), 'retrying' => count($this->bots_retrying)]]));
                     break;
             }
         }
     });
     $server->bind();
     $this->init();
     $this->loop->run();
 }
예제 #2
0
파일: rooms.php 프로젝트: rb-cohen/phpws
use Devristo\Phpws\Server\WebSocketServer;
$loop = \React\EventLoop\Factory::create();
// Create a logger which writes everything to the STDOUT
$logger = new \Zend\Log\Logger();
$writer = new Zend\Log\Writer\Stream("php://output");
$logger->addWriter($writer);
// Create a WebSocket server and create a router which sends all user requesting /echo to the DemoEchoHandler above
$server = new WebSocketServer("tcp://0.0.0.0:12345", $loop, $logger);
$handler = new \Devristo\Phpws\RemoteEvent\RemoteEvents($logger);
$server->on("connect", function (TransportInterface $transport) use($loop, $logger, $handler) {
    $stack = StackTransport::create($transport, array(function (TransportInterface $transport) use($loop, $logger) {
        return new RemoteEventTransport($transport, $loop, $logger);
    }));
    $handler->listenTo($stack);
});
$handler->room("time")->on("subscribe", function (StackTransport $transport) use($logger, $handler) {
    $logger->notice("Someone joined our room full of time enthousiasts!!");
});
// Each 0.5 seconds sent the time to all connected clients
$loop->addPeriodicTimer(0.5, function () use($server, $handler, $logger) {
    $time = new DateTime();
    $string = $time->format("Y-m-d H:i:s");
    if (count($handler->room("time")->getMembers())) {
        $logger->notice("Broadcasting time to time room: {$string}");
    }
    $handler->room("time")->remoteEmit("time", $string);
});
// Bind the server
$server->bind();
// Start the event loop
$loop->run();