/** * Process the incoming command, default commands have a system of twist * @param $resUserSocket * @param $strMessageData */ protected function process($resUserSocket, $strMessageData) { $arrData = $this->parseString($strMessageData); //Check that we have a valid response if (is_array($arrData) && count($arrData)) { //Check that a valid system call has been passed in if (array_key_exists('instance', $arrData) && array_key_exists('system', $arrData) && array_key_exists('action', $arrData) && array_key_exists('data', $arrData)) { switch ($arrData['system']) { case 'twist': switch ($arrData['action']) { case 'login': Users::login($arrData, $resUserSocket); break; case 'logout': Users::logout($arrData, $resUserSocket); break; case 'debug': //Toggle the admin log for this connection, only allowing toggle of connections that belong to admins $blCurrentStatus = Sockets::getData($resUserSocket, 'system_admin_log'); Sockets::setData($resUserSocket, 'system_admin_log', $blCurrentStatus ? false : true); System::stats(); break; case 'restart': //Shutdown the server and it will restart in 1 minute $this->shutdownServer(); break; case 'active': case 'inactive': //Set the connections current Active status (clicked in the last 30 seconds or not) Sockets::setData($resUserSocket, 'activeStatus', array('status' => $arrData['action'], 'updated' => time())); break; case 'blur': case 'focus': //Set the connections current View status (focused on browser tab or not) Sockets::setData($resUserSocket, 'viewStatus', array('status' => $arrData['action'], 'updated' => time())); break; case 'uri': //Set the connections current URI (The page of the website that the connection is coming from) Sockets::setData($resUserSocket, 'currentURI', $arrData['data']); break; case 'users': Users::sendUserList($resUserSocket, $arrData); break; } break; //This is where the magic happens, any module can be called here using the correct command //This is where the magic happens, any module can be called here using the correct command default: AppHandler::process($arrData['system'], $resUserSocket, $arrData); break; } } else { //Error invalid params System::sendErrorResponse($resUserSocket, $arrData, 'Invalid request parameters'); } } }
/** * Send only to connections that belong to the passed in userID * @param $intUserID * @param $arrResponseData * @param $arrHasConnectionSetting */ public static function send($intUserID, $arrResponseData, $arrHasSocketData = null) { foreach (Sockets::getAllConnected() as $arrEachSocket) { //Check User ID for matches if ($arrEachSocket['data']['user_id'] == $intUserID) { $blConnectionMatch = true; //Run through the connection data and match any required data, for example only send the the users connections when 'system_admin_log' is set to 1 if (is_array($arrHasSocketData)) { foreach ($arrHasSocketData as $strKey => $mxdValue) { if (Sockets::getData($arrEachSocket['socket'], $strKey) != $mxdValue) { $blConnectionMatch = false; break; } } } if ($blConnectionMatch) { Sockets::writeJSON($arrEachSocket['socket'], $arrResponseData); } } } }