Advertisement
Guest User

Untitled

a guest
Jul 17th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.91 KB | None | 0 0
  1. <?php
  2. require '/var/www/helpdesk/application/bootstrap.php';
  3.  
  4. # require 'SplClassLoader.php';
  5.  
  6. #$classLoader = new SplClassLoader('WebSocket', __DIR__);
  7. #$classLoader->register();
  8.  
  9. // $interface, $port, $ssl
  10. $server = new Site5_WebSocket_Server('192.168.0.102', 31337, false);
  11.  
  12. $server->setCheckOrigin(false);
  13.  
  14. // As is basic rate limiting
  15. $server->setMaxClients(100);
  16. $server->setMaxConnectionsPerIp(20);
  17. $server->setMaxRequestsPerMinute(1000);
  18.  
  19. $server->registerApplication('site5', Site5_WebSocket_Application_Site5::getInstance());
  20. $server->run();
  21.  
  22. # later that day
  23.  
  24. #namespace WebSocket\Application;
  25.  
  26. /**
  27.  * WebSocket Server Application
  28.  *
  29.  * @author Nico Kaiser <nico@kaiser.me>
  30.  */
  31. abstract class Site5_WebSocket_Application_Application
  32. {
  33.     protected static $instances = array();
  34.    
  35.     /**
  36.      * Singleton
  37.      */
  38.     protected function __construct() { }
  39.  
  40.     final private function __clone() { }
  41.    
  42.     final public static function getInstance()
  43.     {
  44.         $calledClassName = get_called_class();
  45.         if (!isset(self::$instances[$calledClassName])) {
  46.             self::$instances[$calledClassName] = new $calledClassName();
  47.         }
  48.  
  49.         return self::$instances[$calledClassName];
  50.     }
  51.  
  52.     abstract public function onConnect($connection);
  53.  
  54.     abstract public function onDisconnect($connection);
  55.  
  56.     abstract public function onData($data, $client);
  57.  
  58.     // Common methods:
  59.    
  60.     protected function _decodeData($data)
  61.     {
  62.         $decodedData = json_decode($data, true);
  63.         if($decodedData === null)
  64.         {
  65.             return false;
  66.         }
  67.        
  68.         if(isset($decodedData['action'], $decodedData['data']) === false)
  69.         {
  70.             return false;
  71.         }
  72.        
  73.         return $decodedData;
  74.     }
  75.    
  76.     protected function _encodeData($action, $data)
  77.     {
  78.         if(empty($action))
  79.         {
  80.             return false;
  81.         }
  82.        
  83.         $payload = array(
  84.             'action' => $action,
  85.             'data' => $data
  86.         );
  87.        
  88.         return json_encode($payload);
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement