PHPDaemon\Servers\IRCBouncer\Pool::getConnection PHP Method

getConnection() public method

public getConnection ( string $url )
$url string
    public function getConnection($url)
    {
        $this->client->getConnection($url, function ($conn) use($url) {
            $this->conn = $conn;
            $conn->attachedClients = new ObjectStorage();
            if ($conn->connected) {
                Daemon::log('IRC bot connected at ' . $url);
                $conn->join($this->config->defaultchannels->value);
                $conn->bind('motd', function ($conn) {
                    //Daemon::log($conn->motd);
                });
                foreach ($this as $bouncerConn) {
                    if (!$bouncerConn->attachedServer) {
                        $bouncerConn->attachTo($conn);
                    }
                }
                $conn->bind('command', function ($conn, $from, $cmd, $args) {
                    if ($cmd === 'PONG') {
                        return;
                    } elseif ($cmd === 'PING') {
                        return;
                    }
                    if ($from['orig'] === $conn->servername) {
                        $from['orig'] = $this->config->servername->value;
                    }
                    $conn->attachedClients->each('commandArr', $from['orig'], $cmd, $args);
                });
                $conn->bind('privateMsg', function ($conn, $msg) {
                    Daemon::log('IRCBouncer: got private message \'' . $msg['body'] . '\' from \'' . $msg['from']['orig'] . '\'');
                });
                $conn->bind('msg', function ($conn, $msg) {
                    $msg['ts'] = microtime(true);
                    $msg['dir'] = 'i';
                    $this->messages->insert($msg);
                });
                $conn->bind('disconnect', function ($conn) use($url) {
                    foreach ($this as $bouncerConn) {
                        if ($bouncerConn->attachedServer === $conn) {
                            $bouncerConn->detach();
                        }
                    }
                    $this->getConnection($url);
                });
            } else {
                Daemon::log('IRCBouncer: unable to connect (' . $url . ')');
            }
        });
    }

Usage Example

Example #1
0
 /**
  * @TODO DESCR
  * @param string $url
  */
 public function getConnection($url)
 {
     $this->client->getConnection($url, function ($conn) use($url) {
         $this->conn = $conn;
         $conn->attachedClients = new ObjectStorage();
         if ($conn->connected) {
             Daemon::log('IRC bot connected at ' . $url);
             $conn->join($this->config->defaultchannels->value);
             $conn->bind('motd', function ($conn) {
                 //Daemon::log($conn->motd);
             });
             foreach ($this as $bouncerConn) {
                 if (!$bouncerConn->attachedServer) {
                     $bouncerConn->attachTo($conn);
                 }
             }
             $conn->bind('command', function ($conn, $from, $cmd, $args) {
                 if ($cmd === 'PONG') {
                     return;
                 } elseif ($cmd === 'PING') {
                     return;
                 }
                 if ($from['orig'] === $conn->servername) {
                     $from['orig'] = $this->config->servername->value;
                 }
                 $conn->attachedClients->each('commandArr', $from['orig'], $cmd, $args);
             });
             $conn->bind('privateMsg', function ($conn, $msg) {
                 Daemon::log('IRCBouncer: got private message \'' . $msg['body'] . '\' from \'' . $msg['from']['orig'] . '\'');
             });
             $conn->bind('msg', function ($conn, $msg) {
                 $msg['ts'] = microtime(true);
                 $msg['dir'] = 'i';
                 $this->messages->insert($msg);
             });
             $conn->bind('disconnect', function ($conn) use($url) {
                 foreach ($this as $bouncerConn) {
                     if ($bouncerConn->attachedServer === $conn) {
                         $bouncerConn->detach();
                     }
                 }
                 $this->getConnection($url);
             });
         } else {
             Daemon::log('IRCBouncer: unable to connect (' . $url . ')');
         }
     });
 }