Exemple #1
0
 /**
  * Prepares connection with web socket server
  *
  * @return void
  */
 public function socketPrepare()
 {
     // log start
     Logger::notice('[SOCKET] Preparing connection...');
     /* 1ST REQUEST : COOKIES */
     $wssJson = $this->socketRequest($this->_cookie);
     // removing null chars
     $wssJson = urldecode(str_replace('%00%09%07%FF0', '', urlencode($wssJson)));
     // check if valid json object
     if (Fetch::isPageJson($wssJson)) {
         // storing sid
         $socketId = json_decode($wssJson)->sid;
         $sockConf = 'sid=' . $socketId;
         // appending io token to cookie
         $ioCookie = Fetch::removePageCookie($this->_cookie, 'PHPSESSID') . 'io=' . $socketId . ';';
         /* 2ND REQUEST : TEST */
         // if response returns data
         if (isset($this->socketRequest($ioCookie, $sockConf)[0])) {
             // generate payload
             $key = '["auth","' . $this->getAuthKey() . '"]';
             /* 3RD REQUEST : HANDSHAKE */
             // if handshake is ok
             if ($this->socketRequest($ioCookie, $sockConf, true, '45:' . (strlen($key) - 1) . $key) == 'ok') {
                 /* 4TH REQUEST: AUTH SUCCESS TEST */
                 // if response returns data
                 if (isset($this->socketRequest($ioCookie, $sockConf)[0])) {
                     // reset socket tick
                     $this->_sockTick = 0;
                     // log endn
                     Logger::notice('[SOCKET] Connection prepared');
                     // return sid
                     return array($ioCookie, $socketId);
                 } else {
                     Logger::crit('[SOCKET] 4: Auth success failed');
                 }
             } else {
                 Logger::crit('[SOCKET] 3: Handshake failed');
             }
         } else {
             Logger::crit('[SOCKET] 2: Test request failed');
         }
     } else {
         Logger::crit('[SOCKET] 1: SID fetch failed');
     }
 }
Exemple #2
0
 /**
  * Extends listen()
  *
  * @param string  $url       Web Socket URL
  * @param array   $sockConf  Web Socket Configuration
  * @param closure $callback  Anonymous method to return frames to
  *
  * @return void
  */
 private function _listen($url, $sockConf, $callback)
 {
     // connecting to websocket server
     $client = new \Devristo\Phpws\Client\WebSocket($url . '&sid=' . $sockConf[1], $this->_loop, Logger::getInstance(), array('http' => array('header' => 'Cookie:' . $sockConf[0])));
     // request
     $client->on("request", function ($headers) {
         // object created
         Logger::notice("[SOCKET] Request object created!");
     });
     // handshake
     $client->on("handshake", function () {
         // handshake made with server
         Logger::notice("[SOCKET] Handshake received!");
     });
     // connect
     $client->on("connect", function () use($client) {
         // connected to websocket
         Logger::notice("[SOCKET] Connected to Websocket!");
         // request connection
         $client->send('2probe');
     });
     // message
     $client->on("message", function ($message) use($client, $url, $sockConf, $callback) {
         // store data in frame
         $frame = $message->getData();
         // if socket has died out
         $this->handleFrame($frame, $client, $callback);
     });
     // start client listener
     $client->open();
     // start looping requests
     $this->_loop->run();
 }
Exemple #3
0
 /**
  * Equivalent to sendMessage() in event emitter but checks and applies logger
  * if logging mode is enabled
  *
  * @param string $outputMsg  Message to send
  * @param bool   $pm         Private message?
  * @param string $user       Who to pm to?
  *
  * @return void
  */
 private function internalSendMessage($outputMsg, $pm = false, $user = false)
 {
     if ($this->_limits['logOnly']) {
         Logger::Notice($outputMsg);
     } else {
         if ($pm !== false && $user !== false) {
             $this->_eventEmitter->sendMessage($outputMsg, $pm, $user);
         } else {
             $this->_eventEmitter->sendMessage($outputMsg);
         }
     }
 }
Exemple #4
0
 private function customQuery($stmt, $params, \Closure $callback = null)
 {
     try {
         // prepare query
         $stmtTest = $this->prepare($stmt);
         // bind params
         foreach ($params as $k => $v) {
             $stmtTest->bindValue($k, $v);
         }
         // execute query
         $stmtTest->execute();
         // if response is needed, give callback
         if ($callback != null) {
             $callback($stmtTest);
         }
         // close query
         $stmtTest->closeCursor();
     } catch (\PDOException $pdoException) {
         // output query error
         Logger::crit($pdoException->getMessage());
     }
 }
Exemple #5
0
 /**
  * Prepares connection with web socket server
  *
  * @return void
  */
 public function socketPrepare()
 {
     // log start
     Logger::notice('[SOCKET] Preparing connection...');
     /* 1ST REQUEST : COOKIES */
     $wssJson = $this->socketRequest(false);
     // removing null chars
     $wssJson = urldecode(str_replace('%00%09%07%FF0', '', urlencode($wssJson)));
     // check if valid json object
     if (Fetch::isPageJson($wssJson)) {
         // storing sid
         $socketId = json_decode($wssJson)->sid;
         $sockConf = 'sid=' . $socketId;
         // appending io token to cookie
         $ioCookie = 'io=' . $socketId . ';';
         /* 2ND REQUEST : TEST */
         // if response returns data
         if (isset($this->socketRequest($ioCookie, $sockConf)[0])) {
             // generate payload
             $stats = '42["stats"]';
             /* 3RD REQUEST : HANDSHAKE */
             // if handshake is ok
             if ($this->socketRequest($ioCookie, $sockConf, true, mb_strlen($stats, 'utf8') . ':' . $stats) == 'ok') {
                 /* 4TH REQUEST: TEST */
                 // if response returns data
                 if (isset($this->socketRequest($ioCookie, $sockConf)[0])) {
                     /* 5TH Auth TEST */
                     // generate payload
                     $partOne = '42["chat"]';
                     $partTwo = '42["user","' . $this->_accessToken . '"]';
                     // if connected
                     if ($this->socketRequest($ioCookie, $sockConf, true, mb_strlen($partOne, 'utf8') . ':' . $partOne . mb_strlen($partTwo, 'utf8') . ':' . $partTwo) == 'ok') {
                         // log end
                         Logger::notice('[SOCKET] Connection prepared');
                         // return sid
                         return array($ioCookie, $socketId);
                     } else {
                         Logger::crit('[SOCKET] 5: Chat Success failed');
                     }
                 } else {
                     Logger::crit('[SOCKET] 4: Stats request failed');
                 }
             } else {
                 Logger::crit('[SOCKET] 3: Handshake failed');
             }
         } else {
             Logger::crit('[SOCKET] 2: Test request failed');
         }
     } else {
         Logger::crit('[SOCKET] 1: SID fetch failed');
     }
 }