Ejemplo n.º 1
0
 /**
  * Preform the handshake protocol between the server and the client, after this the client is free to communicate with the server
  * @param $resSocket
  * @param $strBuffer
  * @return bool
  */
 protected function handshake($resSocket, $strBuffer)
 {
     System::debug("\nRequesting handshake...");
     System::debug($strBuffer);
     list($strRequestURI, $strHost, $strOriginAddress, $intKey1, $intKey2, $strLast8Bytes, $strAcceptKey, $intProtocolVersion, $strProtocol) = $this->getHeaders($strBuffer);
     System::debug("Handshaking...");
     Sockets::setTemporaryVersion($resSocket, $intProtocolVersion);
     //echo "\n\n".$intProtocolVersion."\n\n";
     if ($intProtocolVersion == '13' || $intProtocolVersion == '8') {
         // Generate our Socket-Accept key based on the IETF specifications
         //$strReturnAcceptKey = $strAcceptKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
         //$strReturnAcceptKey = sha1($strReturnAcceptKey, true);
         //$strReturnAcceptKey = base64_encode($strReturnAcceptKey);
         $strReturnAcceptKey = base64_encode(pack('H*', sha1($strAcceptKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
         $arrUpgradeHeaders = array('HTTP/1.1 101 Switching Protocols', 'Upgrade: websocket', 'Connection: Upgrade', sprintf('Sec-WebSocket-Accept: %s', $strReturnAcceptKey));
         //Only return the protocol header if originally sent it.
         if (!is_null($strProtocol)) {
             $arrUpgradeHeaders[] = sprintf('Sec-WebSocket-Protocol: %s', $strProtocol);
         }
         //Set the end bit
         $arrUpgradeHeaders[] = '';
     } else {
         $arrUpgradeHeaders = array('HTTP/1.1 101 WebSocket Protocol Handshake', 'Upgrade: WebSocket', 'Connection: Upgrade', sprintf('Sec-WebSocket-Origin: %s', $strOriginAddress), sprintf('Sec-WebSocket-Location: ws://%s%s', $strHost, $strRequestURI), '', $this->calculateKey($intKey1, $intKey2, $strLast8Bytes));
     }
     $strUpgradeHeader = '';
     foreach ($arrUpgradeHeaders as $strEachHeader) {
         $strUpgradeHeader .= sprintf("%s\r\n", $strEachHeader);
     }
     //$strUpgradeHeader .= chr(0);
     Sockets::writeRawBuffer($resSocket, $strUpgradeHeader);
     Sockets::setTemporaryHandshake($resSocket, true);
     //Send the safari fix so that safari wont error upon connection
     $arrSafariFix = array('instance' => null, 'system' => 'twist-safari-fix', 'action' => '', 'message' => 'Safari Fix', 'data' => array());
     Sockets::writeJSON($resSocket, $arrSafariFix);
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Send out a list of all active users on the server to the requesting socket
  * @param $resUserSocket
  * @param $arrData
  * @return array
  */
 public static function sendUserList($resUserSocket, $arrData)
 {
     $arrUsers = $arrOut = array();
     foreach (self::getAll() as $arrEachUser) {
         $arrUsers[$arrEachUser['name']] = array('user_id' => $arrEachUser['id'], 'user_name' => $arrEachUser['name']);
     }
     //Sort the list and then rebuild with numerical keys
     ksort($arrUsers);
     foreach ($arrUsers as $arrEachUser) {
         $arrOut[] = $arrEachUser;
     }
     $arrResponse = array('instance' => array_key_exists('instance', $arrData) ? $arrData['instance'] : null, 'system' => 'twist', 'action' => $arrData['action'], 'message' => 'All active users', 'data' => array('users' => $arrOut));
     Sockets::writeJSON($arrResponse, $resUserSocket);
 }