private function onMessage(WebSocketMessageInterface $message)
{
// parse the message and get the event name
$payload = Payload::fromJson($message->getData());
if (isset($payload['type'])) {
switch ($payload['type']) {
case 'hello':
$this->connected = true;
break;
case 'team_rename':
$this->team->data['name'] = $payload['name'];
break;
case 'team_domain_change':
$this->team->data['domain'] = $payload['domain'];
break;
case 'channel_joined':
$channel = new Channel($this, $payload['channel']);
$this->channels[$channel->getId()] = $channel;
break;
case 'channel_created':
$this->getChannelById($payload['channel']['id'])->then(function (Channel $channel) {
$this->channels[$channel->getId()] = $channel;
});
break;
case 'channel_deleted':
unset($this->channels[$payload['channel']['id']]);
break;
case 'channel_rename':
$this->channels[$payload['channel']['id']]->data['name'] = $payload['channel']['name'];
break;
case 'channel_archive':
$this->channels[$payload['channel']['id']]->data['is_archived'] = true;
break;
case 'channel_unarchive':
$this->channels[$payload['channel']['id']]->data['is_archived'] = false;
break;
case 'group_joined':
$group = new Group($this, $payload['channel']);
$this->groups[$group->getId()] = $group;
break;
case 'group_rename':
$this->groups[$payload['group']['id']]->data['name'] = $payload['channel']['name'];
break;
case 'group_archive':
$this->groups[$payload['group']['id']]->data['is_archived'] = true;
break;
case 'group_unarchive':
$this->groups[$payload['group']['id']]->data['is_archived'] = false;
break;
case 'im_created':
$dm = new DirectMessageChannel($this, $payload['channel']);
$this->dms[$dm->getId()] = $dm;
break;
case 'bot_added':
$bot = new Bot($this, $payload['bot']);
$this->bots[$bot->getId()] = $bot;
break;
case 'bot_changed':
$bot = new Bot($this, $payload['bot']);
$this->bots[$bot->getId()] = $bot;
break;
}
// emit an event with the attached json
$this->emit($payload['type'], [$payload]);
} else {
// If reply_to is set, then it is a server confirmation for a previously
// sent message
if (isset($payload['reply_to'])) {
if (isset($this->pendingMessages[$payload['reply_to']])) {
$deferred = $this->pendingMessages[$payload['reply_to']];
// Resolve or reject the promise that was waiting for the reply.
if (isset($payload['ok']) && $payload['ok'] === true) {
$deferred->resolve();
} else {
$deferred->reject($payload['error']);
}
unset($this->pendingMessages[$payload['reply_to']]);
}
}
}
}