$this->connector = new Connector();
$this->connect();
$this->listenCommands();
+ $this->startPinger();
}
public function getLogger() {
$this->ready = false;
$this->logger->info('websocket connection closed: '.$reason.' ['.$op.']');
if (!$this->shutting_down) {
- $this->logger->info('reconnecting in 10 seconds');
- Loop::addTimer(10, [$this, 'connect']);
+ $this->logger->info('reconnecting in 5 seconds');
+ Loop::addTimer(5, [$this, 'connect']);
}
}
public function handleIRCMessage(IRCMessage $msg) {
- if ($msg->isPrivMsg()) {
- $this->handlePrivMsg($msg);
- return;
- }
+ $this->last_contact = time();
if ($msg->isPing()) {
$this->sendIRCMessage($msg->makePong());
return;
}
+ if ($msg->isPong()) {
+ return;
+ }
+ $msg->log();
+ if ($msg->isPrivMsg()) {
+ $this->handlePrivMsg($msg);
+ return;
+ }
if ($msg->isNotice() && $msg->getText() == 'Login authentication failed') {
$this->logger->notice('login failed, refreshing access token');
$this->token->refresh();
public function handlePrivMsg(IRCMessage $msg) {
$target = $msg->getPrivMsgTarget();
- if ($target[0] != '#') return;
+ if ($target[0] != '#') return; // direct message
$text = $msg->getText();
if ($text[0] != '!') return;
$channel = Channel::firstWhere('twitch_chat', '=', $target);
}
}
});
+ }
+ private function startPinger() {
+ $this->getLoop()->addPeriodicTimer(15, function () {
+ if (!$this->ready) return;
+ if (time() - $this->last_contact < 60) return;
+ try {
+ $this->sendIRCMessage(IRCMessage::ping());
+ } catch (\Exception $e) {
+ }
+ });
}
public function sendIRCMessage(IRCMessage $msg) {
$irc_message = $msg->encode();
$this->logger->info('sending IRC message '.$irc_message);
$this->ws->send($irc_message);
+ $this->last_contact = time();
}
private $ready = false;
private $shutting_down = false;
+ private $last_contact;
+
}
?>