/** * STREAM */ public function testStreamAdapter() { $client = $this->client(); $message = (string) memory_get_usage(true); $routeStream = Route::get('rooms/{roomId}/chatMessages')->with('roomId', $this->debugRoomId())->toStream(); $routeAnswer = Route::post('rooms/{roomId}/chatMessages')->with('roomId', $this->debugRoomId()); // Connect to client $client->adapters->through(StreamBuzzAdapter::class)->request($routeStream)->subscribe(function ($answer) use($client, $message) { $this->assertInternalType('array', $answer); $this->assertArrayHasKey('text', $answer); if ($message === $answer['text']) { $client->disconnect(); } }); // Send message after 1 second $client->loop->addTimer(1, function () use($message, $routeAnswer) { $this->client()->adapters->through(SyncBuzzAdapter::class)->request($routeAnswer->withBody('text', $message)); }); // Throws exception after 10 seconds timeout $client->loop->addTimer(10, function () use($client) { $client->disconnect(); $this->throwException(new \RuntimeException('Client timeout')); }); $client->connect(); }
/** * STREAMING */ public function testStreamAdapter() { $client = $this->client(); $routeStream = Route::get('rooms/{roomId}/chatMessages')->with('roomId', $this->debugRoomId())->toStream(); /** @var \Generator $stream */ $stream = $client->adapters->through(StreamGuzzleAdapter::class)->request($routeStream); $this->assertInstanceOf(\Generator::class, $stream); }
/** * @return array * @throws \InvalidArgumentException */ protected function currentUser() : array { if (!array_key_exists($this->client->token, self::$currentUserId)) { $response = $this->using(AdapterInterface::TYPE_SYNC)->request(Route::get('user')->toApi()); $userId = $response[0] ?? null; if ($userId === null) { throw new \InvalidArgumentException('Broken request. Can not fetch current authenticated user'); } self::$currentUserId[$this->client->token] = $userId; } return self::$currentUserId[$this->client->token]; }
/** * Use the streaming API to listen messages. * The streaming API allows real-time access to messages fetching. * * @param string $roomId * @return Observer * @throws \InvalidArgumentException */ public function messages(string $roomId) : Observer { return $this->using(AdapterInterface::TYPE_STREAM)->request(Route::get('rooms/{roomId}/chatMessages')->with('roomId', $roomId)->toStream()); }
/** * There is also a way to retrieve a single message using its id. * * @param string $roomId Room id * @param string $messageId Message id * @return mixed */ public function find(string $roomId, string $messageId) { return $this->fetch(Route::get('rooms/{roomId}/chatMessages/{messageId}')->withMany(['roomId' => $roomId, 'messageId' => $messageId])); }
/** * List of rooms nested under the specified group. * * @param string $groupId * @return mixed */ public function rooms(string $groupId) { return $this->fetch(Route::get('groups/{groupId}/rooms')->with('groupId', $groupId)); }
/** * List of Gitter channels nested under the current user. * * @param string|null $userId * @return mixed * @throws \InvalidArgumentException */ public function channels(string $userId = null) { return $this->fetch(Route::get('user/{userId}/channels')->with('userId', $userId ?? $this->currentUserId())); }