상속: extends Evenement\EventEmitter
예제 #1
0
 /**
  * Connect to AMI and start emitting events.
  *
  * @param string $address Example uaername:password@localhost:5038
  * @return \React\Promise\Promise
  */
 public function connect($address)
 {
     $factory = new Factory($this->eventLoop);
     return $factory->createClient($address)->then(function (Client $client) {
         $this->amiClient = $client;
         $this->actionSender = new ActionSender($client);
         $this->actionSender->events(true);
         $client->on('close', function () {
             $this->logger->debug('AMI connection closed');
         });
         $client->on('event', function (Event $event) {
             $this->wsClient->emit($event->getName(), (array) $event);
         });
     }, function (\Exception $e) {
         $this->logger->err('Connection error: ' . $e->getMessage());
     });
 }
예제 #2
0
 public function open($timeOut = null)
 {
     $promise = parent::open($timeOut);
     $this->on("connect", function () {
         $this->stream->on('close', function () {
             $this->emit('close');
         });
     });
     return $promise;
 }
예제 #3
0
 public function receive(callable $callback, callable $error)
 {
     $should_close = false;
     $this->client->on('request', function () {
         $this->logger->notice('Request object created!');
     });
     $this->client->on('handshake', function () {
         $this->logger->notice('Handshake received!');
     });
     $this->client->on('connect', function () {
         $this->logger->notice('Connected!');
     });
     $this->client->on('error', function () use($error) {
         // Call the error callback.
         $error();
         // Reopen the connection.
         $this->client->close();
         $this->client->open(self::TIMEOUT);
     });
     $this->client->on('close', function () use($should_close) {
         // Reopen the connection.
         if (!$should_close) {
             $this->client->open(self::TIMEOUT);
         }
     });
     $this->client->on('message', function (WebSocketMessageInterface $message) use($callback, &$should_close) {
         // Wait for the message to be finalized.
         if (!$message->isFinalised()) {
             return;
         }
         // Get the contents.
         $contents = json_decode($message->getData(), true);
         if (!$contents) {
             return;
         }
         // Handle the contents.
         $value = $this->handleContents($contents);
         // Call the callback with the value.
         $result = $callback($value);
         // If the callback returns true, close the connection.
         if ($result === true) {
             $should_close = true;
             $this->client->close();
         }
     });
 }
예제 #4
0
 private function setupConnection()
 {
     $this->client = new WebSocket($this->context->url, $this->loop, $this->logger);
     $this->client->on("request", function () {
         $this->logger->notice("Request object created!");
     });
     $this->client->on("handshake", function () {
         $this->logger->notice("Handshake received!");
     });
     $this->client->on("connect", function () {
         $this->logger->notice("Connected!");
     });
 }
 public function run()
 {
     while (true) {
         try {
             $this->loadAddresses();
             //aby načetl nově generované adresy
             $this->client->open();
             $this->loop->run();
         } catch (\Exception $e) {
             $this->logger->err($e->getMessage());
         }
     }
 }
예제 #6
0
 function test_DoubleEchoResourceHandlerResponse()
 {
     $input = str_repeat("a", 1024);
     $input2 = str_repeat("b", 1024);
     $msg = WebSocketMessage::create($input);
     $client = new WebSocket("wss://127.0.0.1:12345/echo/");
     $client->setTimeOut(1000);
     $client->open();
     $client->sendMessage($msg);
     $client->sendMessage(WebSocketMessage::create($input2));
     $msg = $client->readMessage();
     $msg2 = $client->readMessage();
     $client->close();
     $this->assertEquals($input, $msg->getData());
     $this->assertEquals($input2, $msg2->getData());
 }
예제 #7
0
 /**
  * @param callable|callable $callback
  */
 public function onStasisEnd(callable $callback)
 {
     $this->wsClient->on(Event::STASIS_END, $callback);
 }
예제 #8
0
 /**
  * @param callable|callable $callback
  */
 public function onConnect(callable $callback)
 {
     $this->wsClient->on("connect", $callback);
 }
예제 #9
0
 /**
  * @param callable|callable $callback
  */
 public function onClose(callable $callback)
 {
     $this->wsClient->on("close", $callback);
 }