public function connect()
{
$deferred = new Promise\Deferred();
// Request a real-time connection...
$this->apiCall('rtm.start')->then(function (Payload $response) {
$responseData = $response->getData();
// get the team info
$this->team = new Team($this, $responseData['team']);
// Populate self user.
$this->users[$responseData['self']['id']] = new User($this, $responseData['self']);
// populate list of users
foreach ($responseData['users'] as $data) {
$this->users[$data['id']] = new User($this, $data);
}
// populate list of channels
foreach ($responseData['channels'] as $data) {
$this->channels[$data['id']] = new Channel($this, $data);
}
// populate list of groups
foreach ($responseData['groups'] as $data) {
$this->groups[$data['id']] = new Group($this, $data);
}
// populate list of dms
foreach ($responseData['ims'] as $data) {
$this->dms[$data['id']] = new DirectMessageChannel($this, $data);
}
// populate list of bots
foreach ($responseData['bots'] as $data) {
$this->bots[$data['id']] = new Bot($this, $data);
}
// Log PHPWS things to stderr
$logger = new \Zend\Log\Logger();
$logger->addWriter(new \Zend\Log\Writer\Stream('php://stderr'));
// initiate the websocket connection
$this->websocket = new WebSocket($responseData['url'], $this->loop, $logger);
$this->websocket->on('message', function ($message) {
$this->onMessage($message);
});
return $this->websocket->open();
}, function ($exception) use($deferred) {
// if connection was not succesfull
$deferred->reject(new ConnectionException('Could not connect to Slack API: ' . $exception->getMessage(), $exception->getCode()));
})->then(function () use($deferred) {
$this->once('hello', function () use($deferred) {
$deferred->resolve();
});
$this->once('error', function ($data) use($deferred) {
$deferred->reject(new ConnectionException('Could not connect to WebSocket: ' . $data['error']['msg'], $data['error']['code']));
});
});
return $deferred->promise();
}