示例#1
0
文件: Room.php 项目: Dualse/GitterBot
 /**
  * @throws InvalidArgumentException
  * @return Client
  */
 public function listen()
 {
     $client = $this->client->stream('messages', ['roomId' => $this->id])->on(Stream::EVENT_MESSAGE, function ($stream, $data) {
         $this->onMessage(Message::fromGitterObject($data));
     })->on(Stream::EVENT_END, [$this, 'onClose'])->on(Stream::EVENT_ERROR, [$this, 'onError']);
     return $client;
 }
示例#2
0
 /**
  * Execute the console command.
  *
  * @param Repository $config
  * @param Container $container
  *
  * @return mixed
  * @throws \InvalidArgumentException
  * @throws \RuntimeException
  * @throws \LogicException
  * @throws \Exception
  */
 public function handle(Repository $config, Container $container)
 {
     $started = Carbon::now();
     $client = Client::make($config->get('gitter.token'), $this->argument('room'));
     $stream = $container->make(Room::class)->listen();
     $this->line(sprintf(' Gitter Bot %s started at %s', Client::VERSION, $started->toDateTimeString()));
     $client->getEventLoop()->addPeriodicTimer(1, function () use($started) {
         $memory = number_format(memory_get_usage(true) / 1000 / 1000, 2);
         $uptime = Carbon::now()->diff($started);
         $this->output->write("\r" . sprintf('[memory: %smb] [uptime: %s]%60s', $memory, $uptime->format('%Y.%M.%D %H:%I:%S'), ''));
     });
     $startClient = function () use($client) {
         $this->makePidFile();
         $client->run();
     };
     $startClient();
     $this->removePidFile();
 }
示例#3
0
 /**
  * @return \React\HttpClient\Request
  */
 public function connect()
 {
     $request = $this->client->getHttpClient()->request($this->method, $this->url, $this->headers);
     $request->on('response', function (Response $response) {
         $response->on('data', function ($data, Response $response) {
             $data = (string) $data;
             $this->events->fire(static::EVENT_CHUNK, [$this, $data, $response]);
             $this->buffer->add($data);
         });
     });
     $request->on('end', function () {
         $this->buffer->clear();
         $this->events->fire(static::EVENT_END, [$this]);
     });
     $request->on('error', function ($exception) {
         $this->events->fire(static::EVENT_ERROR, [$this, $exception]);
     });
     $this->events->fire(static::EVENT_CONNECT, [$this, $request]);
     $request->end();
     return $request;
 }
示例#4
0
 /**
  * @param Repository $config
  * @param Container $container
  */
 public function syncUsers(Repository $config, Container $container)
 {
     $this->output->write('Start user sync...');
     $config->set('gitter.output', false);
     $client = Client::make($config->get('gitter.token'), $this->argument('room'));
     $room = $container->make(Room::class);
     $users = $client->request('room.users', ['roomId' => $room->id]);
     $message = "\r<comment>[%s/%s]</comment> %s%80s";
     $count = count($users);
     $current = 1;
     foreach ($users as $user) {
         $user = User::fromGitterObject($user);
         $this->output->write(sprintf($message, $current, $count, $user->login, ''));
         $current++;
     }
     $this->output->write(sprintf($message, $count, $count, 'OK', ''));
 }
示例#5
0
 /**
  * @param Client $client
  * @param $route
  * @param array $args
  * @param string $method
  * @throws \InvalidArgumentException
  */
 public function __construct(Client $client, $route, array $args, $method = 'GET')
 {
     $this->url = $client->getRouter()->route($route, $args);
     $this->method = $method;
     $this->headers = $client->getHeaders();
 }