/** * The main and only function needed to be called to initialise the socket server * @param null $strHostAddress * @param null $intPort */ public function server($strHostAddress = null, $intPort = null) { \Twist::Session()->start(); \Twist::framework()->register()->cancelHandler('error'); \Twist::framework()->register()->cancelHandler('fatal'); \Twist::framework()->register()->cancelHandler('exception'); if (is_null($strHostAddress)) { $strHostAddress = \Twist::framework()->setting('WS_SERVER_HOST'); } if (is_null($intPort)) { $intPort = \Twist::framework()->setting('WS_SERVER_PORT'); } CronLock::$strLockLocation = __DIR__ . '/../lock/'; //Remove the lock on complete failure, fatal error and exception \Twist::framework()->register()->shutdownEvent('CronLock', '\\Packages\\WebSockets\\Models\\CronLock', "destroy"); //Only start if their is no lock file present if (!CronLock::active()) { //Create a lock file so that the server will function CronLock::create(); $this->listen($strHostAddress, $intPort); //Remove the lock on safe shutdown of the server CronLock::destroy(); } }
function dropHistory($intRoomID) { $objDB = Twist::Database(); $strSQL = sprintf("DELETE FROM `%s`.`chat_history`\n\t\t\t\t\tWHERE `room_id` = %d", DATABASE_NAME, $objDB->escapeString($intRoomID)); $objDB->query($strSQL); }
<?php Twist::framework()->package()->uninstall(); //Optional Line: Add this line if you are uninstalling database tables Twist::framework()->package()->importSQL(sprintf('%s/Install/uninstall-websockets.sql', dirname(__FILE__))); //Optional Line: Add this line if you are removing all package settings Twist::framework()->package()->removeSettings(); /** * Remove all Lavish Shopping Hooks for the system */ \Twist::framework()->hooks()->cancel('TWIST_MANAGER_ROUTE', 'websockets-manager', true);
<?php /* * -------------------------------- * Require the TwistPHP framework * -------------------------------- */ require_once 'twist/framework.php'; /* * -------------------------------- * Register the 'Basics' controller * for all requests that start with * the URI '/' (which should be the * base for the site) * -------------------------------- */ Twist::Route()->controller('/%', 'Basics'); /* * -------------------------------- * Respond to all requests with the * relevant registered routes * -------------------------------- */ Twist::Route()->serve();
<?php Twist::framework()->package()->install(); //Optional Line: Add this line if you are adding database tables Twist::framework()->package()->importSQL(sprintf('%s/Install/websockets.sql', dirname(__FILE__))); //Optional Line: Add this line if you are adding framework settings Twist::framework()->package()->importSettings(sprintf('%s/Install/settings.json', dirname(__FILE__))); /** * Install all Lavish Shopping Hooks for the system */ \Twist::framework()->hooks()->register('TWIST_MANAGER_ROUTE', 'websockets-manager', dirname(__FILE__) . '/Hooks/manager.php', true);
/** * Get the users data by email and password of Guest Details * @param $strUID * @param null $strPassword * @return array */ protected static function requestUserData($strUID, $strPassword = null) { $arrOut = array('status' => false, 'message' => 'Login method not allowed', 'data' => array()); //Get the user data if valid credentials if ($strUID != 'guest' && !strstr($strUID, '@')) { //If the UID dosnt contain an @ symbol treat it as a session Key of a logged in user $intUserID = Auth::SessionHandler()->validateCode($strUID, false); if (!is_null($intUserID) && $intUserID > 0) { $arrOut['data'] = \Twist::User()->getData($intUserID); $arrOut['status'] = true; $arrOut['message'] = sprintf('Connected to Twist WebSocket Server as %s %s', $arrOut['data']['firstname'], $arrOut['data']['surname']); } } elseif (\Twist::framework()->setting('WS_ALLOW_GUEST_LOGIN') && $strUID == 'guest' && is_null($strPassword)) { self::$intGuestCount++; //If Guest Users are allowed then set a special guest ID prefixed with a 'g' $arrOut['status'] = true; $arrOut['message'] = 'Connected to Twist WebSocket Server as Guest'; $arrOut['data'] = array('id' => 'g' . self::$intGuestCount, 'firstname' => 'Guest', 'surname' => 'User', 'level' => 1, 'email' => sprintf('guest@%s', \Twist::framework()->setting('HTTP_HOST')), 'session_key' => ''); } elseif (\Twist::framework()->setting('WS_ALLOW_REMOTE_LOGIN')) { //Authenticate the user, grab the user data and then logout (remove session data) Auth::validate($strUID, $strPassword); $arrSession = Auth::current(); Auth::logout(); $arrOut['status'] = $arrSession['status']; $arrOut['message'] = $arrSession['status'] ? sprintf('Connected to Twist WebSocket Server as %s %s', $arrSession['user_data']['firstname'], $arrSession['user_data']['surname']) : $arrSession['message']; $arrOut['data'] = array_key_exists('user_data', $arrSession) ? $arrSession['user_data'] : array(); } return $arrOut; }
/* * -------------------------------- * Create a new cookie with the key * of lastVisit with a value of the * current date * -------------------------------- */ \Twist::Cookie()->set('lastVisit', date('Y-m-d H:i:s')); /* * -------------------------------- * If needed, you can add an expiry * time in seconds * -------------------------------- */ \Twist::Cookie()->set('lastVisit', date('Y-m-d H:i:s'), 86400); /* * -------------------------------- * Optionally you can add a path to * the cookie * -------------------------------- */ \Twist::Cookie()->set('lastVisit', date('Y-m-d H:i:s'), 86400, '/'); /* * -------------------------------- * You are also able to specify the * domain that the cookie is stored * against * -------------------------------- */ \Twist::Cookie()->set('lastVisit', date('Y-m-d H:i:s'), 86400, '/', true);
/** * Send out the admin stats to any user that requests them */ public static function stats() { if (is_array(self::$arrBandwidthData) && count(self::$arrBandwidthData) == 0) { self::$arrBandwidthData = array('last_bytes_in' => 0, 'last_bytes_out' => 0, 'peak_rate' => 0, 'previous_bandwidth_in' => array('unit' => 0, 'format' => '0B/s'), 'previous_bandwidth_out' => array('unit' => 0, 'format' => '0B/s'), 'last_monitor_request' => 0); } //Only output the monitor data once a secound if (self::$arrBandwidthData['last_monitor_request'] == 0 || time() - self::$arrBandwidthData['last_monitor_request'] >= 1) { //Reset the counter self::$arrBandwidthData['last_monitor_request'] = time(); $intTotalUpTime = self::upTime(); $arrServer = array(); $arrUsers = Users::getAll(); $arrConnections = Sockets::getAllConnected(); $arrServer['uptime'] = array('unit' => $intTotalUpTime, 'format' => \Twist::DateTime()->getTimePeriod($intTotalUpTime)); $arrServer['request_in'] = array('unit' => self::$intRequestsIn, 'format' => self::$intRequestsIn); $arrServer['request_out'] = array('unit' => self::$intRequestsOut, 'format' => self::$intRequestsOut); $arrServer['traffic_in'] = array('unit' => self::$intDataBytesIn, 'format' => \Twist::File()->bytesToSize(self::$intDataBytesIn)); $arrServer['traffic_out'] = array('unit' => self::$intDataBytesOut, 'format' => \Twist::File()->bytesToSize(self::$intDataBytesOut)); $arrServer['mem_usage'] = array('unit' => memory_get_usage(), 'format' => \Twist::File()->bytesToSize(memory_get_usage())); $arrServer['users'] = array('unit' => count($arrUsers), 'format' => count($arrUsers)); $arrServer['connections'] = array('unit' => count($arrConnections), 'format' => count($arrConnections)); //Calculate the avrage speed self::$intBandwidthTime = self::$intBandwidthTime == 0 ? self::$intUpTime : self::$intBandwidthTime; $intLastSpeedCheck = time() - self::$intBandwidthTime; if ($intLastSpeedCheck > 2) { $intBytesPerSecOut = floor((self::$intDataBytesOut - self::$arrBandwidthData['last_bytes_out']) / $intLastSpeedCheck); $arrServer['current_bandwidth_out'] = array('unit' => $intBytesPerSecOut, 'format' => sprintf('%s/s', \Twist::File()->bytesToSize($intBytesPerSecOut))); $intBytesPerSecIn = floor((self::$intDataBytesIn - self::$arrBandwidthData['last_bytes_in']) / $intLastSpeedCheck); $arrServer['current_bandwidth_in'] = array('unit' => $intBytesPerSecIn, 'format' => sprintf('%s/s', \Twist::File()->bytesToSize($intBytesPerSecIn))); //Now log and reset all the stats self::$arrBandwidthData['previous_bandwidth_in'] = $arrServer['current_bandwidth_in']; self::$arrBandwidthData['previous_bandwidth_out'] = $arrServer['current_bandwidth_out']; self::$arrBandwidthData['last_bytes_in'] = self::$intDataBytesIn; self::$arrBandwidthData['last_bytes_out'] = self::$intDataBytesOut; self::$intBandwidthTime = time(); } else { $arrServer['current_bandwidth_in'] = self::$arrBandwidthData['previous_bandwidth_in']; $arrServer['current_bandwidth_out'] = self::$arrBandwidthData['previous_bandwidth_out']; } /** if(array_key_exists('SocketChat',$this->resSocketModules)){ $intFixed = $intDynamic = 0; foreach($this->resSocketModules['SocketChat']->arrChatRooms as $arrEachRoom){ ($arrEachRoom['fixed']) ? $intFixed++ : $intDynamic++; } $arrServer['rooms'] = array('unit' => count($this->resSocketModules['SocketChat']->arrChatRooms),'format' => sprintf("%s (%d Fixed / %d Dynamic)", count($this->resSocketModules['SocketChat']->arrChatRooms), $intFixed, $intDynamic )); }*/ $arrUserOut = array(); foreach ($arrUsers as $arrEachUser) { $arrConnectionData = array(); foreach ($arrConnections as $arrEachConnection) { if ($arrEachConnection['data']['user_id'] == $arrEachUser['id']) { $arrConnectionData[] = array('viewStatus' => array_key_exists('viewStatus', $arrEachConnection['data']) ? $arrEachConnection['data']['viewStatus'] : '', 'activeStatus' => array_key_exists('activeStatus', $arrEachConnection['data']) ? $arrEachConnection['data']['activeStatus'] : '', 'currentURI' => array_key_exists('currentURI', $arrEachConnection['data']) ? $arrEachConnection['data']['currentURI'] : '', 'ip' => $arrEachConnection['ip'], 'port' => $arrEachConnection['port']); } } $arrUserOut[] = array('id' => $arrEachUser['id'], 'name' => $arrEachUser['name'], 'connections' => $arrConnectionData); } /** $arrChatOut = array(); if(array_key_exists('SocketChat',$this->resSocketModules) && is_array($this->resSocketModules['SocketChat']->arrChatRooms)){ foreach($this->resSocketModules['SocketChat']->arrChatRooms as $arrEachRoom){ $strUserList = ""; foreach($arrEachRoom['users'] as $intUserID){ $arrUserData = $this->resUsers->getUser($intUserID); $strUserList .= sprintf("%s, ",$arrUserData['name']); } $strUserList = rtrim(trim($strUserList),',')."\n"; $arrChatOut[] = array( 'gid' => $arrEachRoom['id'], 'name' => $arrEachRoom['name'], 'users' => $strUserList ); } }*/ $arrResponseData = array('instance' => '', 'system' => 'twist', 'action' => 'debug', 'message' => 'System Stats', 'data' => array('server' => $arrServer, 'users' => $arrUserOut)); Users::sendAdmin($arrResponseData, null, array('system_admin_log' => true)); } }
<?php \Twist::define('WEBSOCKETS_VIEWS', dirname(__FILE__) . '/../Views'); $this->controller('/websockets/%', 'Packages\\WebSockets\\Controllers\\Manager');
public function _index() { $arrConnectionData = array('ws_host' => array_key_exists('ws_host', $_GET) ? $_GET['ws_host'] : \Twist::framework()->setting('WS_SERVER_HOST'), 'ws_port' => array_key_exists('ws_port', $_GET) ? $_GET['ws_port'] : \Twist::framework()->setting('WS_SERVER_PORT'), 'session_key' => \Twist::Session()->data('user-session_key')); return $this->_view('manager/overview.tpl', $arrConnectionData); }
public static function load() { //Load in all the Twist Hooks and register them ready for use in the system self::$arrApps = \Twist::framework()->hooks()->getAll('WEB_SOCKET_APPS'); }