/** * send data via pushup to notify a user in real time * * @param int $userid id of user to send data to * @param string $function function to call on client js * @param array $args arguments for remotely called js function * @return bool success flag */ public function send($userid, $function, $args = null) { $config = Config::get('pushup'); // pushup active? if (!$config->enable) { return false; } // send message try { $config = $config->server['default']; // instantiate pushup instances $multi = new SC_Pushup_Multi(); foreach ($config['cluster'] as $backend => $frontend) { list($host, $port) = explode(':', $backend); $tcpClient = new SC_TcpClient(); $tcpClient->setTimeout($config['connectTimeout'] * 1000); $pushup = new SC_Pushup(); $pushup->setTcpClient($tcpClient, $host, $port); $pushup->setBackendPassword($config['backendPass']); $multi->addInstance($pushup); } $multi->executeUser($userid, json_encode(array('func' => $function, 'args' => $args))); return true; } catch (SC_Pushup_Exception $e) { } catch (SC_TcpClient_Exception $e) { } return false; }
protected function pushupGetInstance() { $config = Config::get('pushup')->server['default']; $host = array_keys($config['cluster']); list($host, $port) = explode(':', $host[0]); $tcpClient = new SC_TcpClient(); $tcpClient->setTimeout($config['connectTimeout'] * 1000); $pushup = new SC_Pushup(); $pushup->setTcpClient($tcpClient, $host, (int) $port); $pushup->setBackendPassword($config['backendPass']); return $pushup; }
/** * send data and process response * * @param array $data data to send * @return mixed response */ protected function command($data) { $this->connect(); // enter multibyte workaround mode if (ini_get('mbstring.func_overload') & 2) { if ($this->mbenc === null) { $this->mbenc = mb_internal_encoding(); } mb_internal_encoding('latin1'); } // build request $request = $this->serialize($data); // send request try { $this->TcpClient->write($request); } catch (SC_TcpClient_Exception $e) { throw new SC_Pushup_Exception($e->getMessage()); } // read response try { $response = $this->TcpClient->read(2048); if (empty($response)) { throw new SC_Pushup_Exception('empty response'); } // get response length $header = unpack('Nsize', substr($response, 0, 4)); // invalid response if ($header === false) { throw new SC_Pushup_Exception('invalid response, cannot unpack header data'); } // read remaining response data $size = $header['size'] + 4; while (strlen($response) < $size) { $response .= $this->TcpClient->read($size - strlen($response)); } } catch (SC_TcpClient_Exception $e) { $this->disconnect(); SC_Log::get('pushup')->error('network error: ' . $e->getMessage()); throw new SC_Pushup_Exception('network error'); } // process response $response = $this->deserialize($response); // leave multibyte workaround mode if ($this->mbenc !== null) { mb_internal_encoding($this->mbenc); $this->mbenc = null; } // check for errors if (!is_array($response) || empty($response)) { throw new SC_Pushup_Exception('command response error'); } if (($responseState = array_shift($response)) == self::COMMAND_ERROR) { throw new SC_Pushup_Exception(array_shift($response)); } // return result if (empty($response)) { switch ($responseState) { case self::COMMAND_OK: return true; case self::LOGIN_OK: return true; case self::LOGIN_FAILED: throw new SC_Pushup_Exception('login failed'); } } return $response; }
<?php set_include_path(dirname(dirname(__FILE__)) . '/php/lib'); require_once 'SC/TcpClient/Exception.php'; require_once 'SC/TcpClient.php'; require_once 'SC/Pushup/Exception.php'; require_once 'SC/Pushup/Interface.php'; require_once 'SC/Pushup.php'; try { // get tcp client $tcpClient = new SC_TcpClient('127.0.0.1', 6666); // set connect timeout $tcpClient->setTimeout(1 * 1000); // connect to server $tcpClient->connect(); // set read timeout $tcpClient->setTimeout(1 * 1000); // get pushup $pushup = new SC_Pushup(); // inject tcp client $pushup->setTcpClient($tcpClient); // set pushup backend password $pushup->setBackendPassword('secret'); // execute javascript function "Test.alert" with argument "hallo" for the user with id 1 $pushup->executeUser(1, json_encode(array('func' => 'PushupTest.alert', 'args' => 'Hello :-)'))); } catch (Exception $e) { echo get_class($e) . ' (' . $e->getMessage() . ")\n"; }