Beispiel #1
0
 /**
  * Join a room.
  *
  * @param array $network The packet information from the network.
  * @param int $room The room id to join.
  * @param string $host The host of the socket.
  * @param string $port The port of the socket.
  *
  * @return \Mars\Network\Socket
  *
  * @throws \Mars\Network\Exception\RoomException When the network variable is empty.
  * @throws \Mars\Network\Exception\SocketException When the socket has failed to connect.
  */
 public function join(array $network = [], $room = null, $host = null, $port = null)
 {
     $config = $this->config();
     if (empty($network)) {
         throw new RoomException('The network can not be empty.', E_WARNING);
     }
     if (!is_null($room)) {
         $config['room'] = $room['id'];
     }
     $socketPort = static::getPort($config['room']);
     $socketHost = static::getHost($config['room'], $config['ips']);
     if (!is_null($host)) {
         $socketHost = $host;
     }
     if (!is_null($port)) {
         $socketPort = $port;
     }
     $socket = new Socket(['host' => $socketHost, 'port' => $socketPort]);
     $socket->connect();
     if ($socket->connected === false) {
         throw new SocketException('Can not connect to the socket ' . $config['host'] . ':' . $config['port'] . '.', E_WARNING);
     }
     $socket->write($this->_bluidConnectionPacket($config['room']));
     $response = $socket->read();
     if ($response === false) {
         throw new RoomException('Can not read the socket while joining the room.', E_WARNING);
     }
     $result = Xml::toArray(Xml::build(Xml::repair($response)));
     $network = array_merge($network, $result);
     $this->loginInfos = $network;
     $socket->write($this->_buildJoinPacket($result, $network, $config['room']));
     return ['socket' => $socket, 'network' => $network];
 }
Beispiel #2
0
 /**
  * Get the needed informations from xat by login the bot on xat website.
  *
  * @return void
  *
  * @throws \Mars\Network\Exception\SocketException When the socket is not connected.
  */
 public function startup()
 {
     $config = $this->config()['socket'];
     $config['port'] += floor(rand(0, 38));
     $socket = new Socket($config);
     $socket->connect();
     if ($socket->connected === false) {
         throw new SocketException('Can not connected to the socket ' . $config['host'] . ':' . $config['port'] . '.', E_WARNING);
     }
     //Send the login packet to xat.
     $socket->write($this->_buildLoginPacket());
     $response = $socket->read();
     if ($response === false) {
         throw new NetworkException('Can not read the socket while connecting to xat.', E_WARNING);
     }
     $result = Xml::toArray(Xml::build(Xml::repair($response)));
     //Send private informations to xat.
     $socket->write($this->_buildPrivatePacket());
     $response = $socket->read();
     if ($response === false) {
         throw new NetworkException('Can not read the socket while connecting to xat.', E_WARNING);
     }
     $result += Xml::toArray(Xml::build(Xml::repair($response)));
     $this->loginInfos = $result;
 }