Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require '/var/www/helpdesk/application/bootstrap.php';
- # require 'SplClassLoader.php';
- #$classLoader = new SplClassLoader('WebSocket', __DIR__);
- #$classLoader->register();
- // $interface, $port, $ssl
- $server = new Site5_WebSocket_Server('192.168.0.102', 31337, false);
- $server->setCheckOrigin(false);
- // As is basic rate limiting
- $server->setMaxClients(100);
- $server->setMaxConnectionsPerIp(20);
- $server->setMaxRequestsPerMinute(1000);
- $server->registerApplication('site5', Site5_WebSocket_Application_Site5::getInstance());
- $server->run();
- # later that day
- #namespace WebSocket\Application;
- /**
- * WebSocket Server Application
- *
- * @author Nico Kaiser <nico@kaiser.me>
- */
- abstract class Site5_WebSocket_Application_Application
- {
- protected static $instances = array();
- /**
- * Singleton
- */
- protected function __construct() { }
- final private function __clone() { }
- final public static function getInstance()
- {
- $calledClassName = get_called_class();
- if (!isset(self::$instances[$calledClassName])) {
- self::$instances[$calledClassName] = new $calledClassName();
- }
- return self::$instances[$calledClassName];
- }
- abstract public function onConnect($connection);
- abstract public function onDisconnect($connection);
- abstract public function onData($data, $client);
- // Common methods:
- protected function _decodeData($data)
- {
- $decodedData = json_decode($data, true);
- if($decodedData === null)
- {
- return false;
- }
- if(isset($decodedData['action'], $decodedData['data']) === false)
- {
- return false;
- }
- return $decodedData;
- }
- protected function _encodeData($action, $data)
- {
- if(empty($action))
- {
- return false;
- }
- $payload = array(
- 'action' => $action,
- 'data' => $data
- );
- return json_encode($payload);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement