class Nodejs in Node.js integration 7
Same name and namespace in other branches
- 6 nodejs.module \Nodejs
Hierarchy
- class \Nodejs
Expanded class hierarchy of Nodejs
2 string references to 'Nodejs'
- nodejs_init in ./
nodejs.module - Implements hook_init().
- nodejs_menu in ./
nodejs.module - Implements hook_menu().
File
- ./
nodejs.module, line 803
View source
class Nodejs {
public static $messages = array();
public static $config = NULL;
public static $baseUrl = NULL;
public static $nodeServerVersion = NULL;
public static function initConfig() {
if (!isset(self::$config)) {
self::$config = nodejs_get_config();
self::$baseUrl = nodejs_get_url(self::$config);
}
}
public static function getMessages() {
return self::$messages;
}
public static function enqueueMessage(StdClass $message) {
self::$messages[] = $message;
}
public static function sendMessages() {
foreach (self::$messages as $message) {
self::sendMessage($message);
}
}
public static function getServerVersion() {
if (isset(self::$nodeServerVersion)) {
return self::$nodeServerVersion;
}
$data = self::healthCheck();
if (!isset($data->version)) {
self::$nodeServerVersion = FALSE;
}
else {
self::$nodeServerVersion = $data->version;
}
return self::$nodeServerVersion;
}
public static function checkServerVersion() {
$server_version = self::getServerVersion();
if (!$server_version) {
// Version number is missing. Assume incompatibility.
return FALSE;
}
$current_parts = explode('.', $server_version);
$required_parts = explode('.', NODEJS_SERVER_VERSION);
$current_major = reset($current_parts);
$required_major = reset($required_parts);
if ($current_major != $required_major || version_compare($server_version, NODEJS_SERVER_VERSION) < 0) {
return FALSE;
}
return TRUE;
}
public static function sendMessage(StdClass $message) {
self::initConfig();
drupal_alter('nodejs_message', $message);
$message->clientSocketId = nodejs_get_client_socket_id();
$options = array(
'method' => 'POST',
'data' => drupal_json_encode($message),
// This is server to server, so start with a low default timeout.
'timeout' => !empty(self::$config['timeout']) ? self::$config['timeout'] : 5,
);
return self::httpRequest('nodejs/publish', $options);
}
public static function healthCheck() {
$data = self::httpRequest('nodejs/health/check', array(), TRUE);
return $data;
}
public static function setUserPresenceList($uid, array $uids) {
return self::httpRequest("nodejs/user/presence-list/{$uid}/" . implode(',', $uids));
}
public static function logoutUser($token) {
$options = array(
'method' => 'POST',
);
return self::httpRequest("nodejs/user/logout/{$token}", $options);
}
public static function sendContentTokenMessage($message) {
$message->clientSocketId = nodejs_get_client_socket_id();
drupal_alter('nodejs_content_channel_message', $message);
$options = array(
'method' => 'POST',
'data' => drupal_json_encode($message),
'options' => array(
'timeout' => 5.0,
),
);
return self::httpRequest('nodejs/content/token/message', $options);
}
public static function sendContentToken($message) {
$options = array(
'method' => 'POST',
'data' => drupal_json_encode($message),
);
return self::httpRequest('nodejs/content/token', $options);
}
public static function getContentTokenUsers($message) {
$options = array(
'method' => 'POST',
'data' => drupal_json_encode($message),
);
return self::httpRequest('nodejs/content/token/users', $options);
}
public static function kickUser($uid) {
$options = array(
'method' => 'POST',
);
return self::httpRequest("nodejs/user/kick/{$uid}", $options);
}
public static function addUserToChannel($uid, $channel) {
$options = array(
'method' => 'POST',
);
return self::httpRequest("nodejs/user/channel/add/{$channel}/{$uid}", $options);
}
public static function removeUserFromChannel($uid, $channel) {
$options = array(
'method' => 'POST',
);
return self::httpRequest("nodejs/user/channel/remove/{$channel}/{$uid}", $options);
}
public static function addChannel($channel) {
$options = array(
'method' => 'POST',
);
return self::httpRequest("nodejs/channel/add/{$channel}", $options);
}
public static function checkChannel($channel) {
return self::httpRequest("nodejs/channel/check/{$channel}");
}
public static function removeChannel($channel) {
$options = array(
'method' => 'POST',
);
return self::httpRequest("nodejs/channel/remove/{$channel}", $options);
}
public static function httpRequest($url, $options = array(), $ignore_version = FALSE) {
self::initConfig();
if (!$ignore_version && !self::checkServerVersion()) {
return FALSE;
}
$options += array(
'method' => 'GET',
'headers' => array(),
);
$options['headers'] += array(
'NodejsServiceKey' => self::$config['serviceKey'],
'Content-type' => 'application/json',
);
$response = drupal_http_request(self::$baseUrl . $url, $options);
// If a http error occurred, and logging of http errors is enabled, log it.
if (isset($response->error)) {
if (self::$config['log_http_errors']) {
$params = array(
'%code' => $response->code,
'%error' => $response->error,
'%url' => $url,
);
$log_message = 'Error reaching the Node.js server at "%url": [%code] %error.';
if (!empty($options['data'])) {
$params['data'] = $options['data'];
$log_message = 'Error reaching the Node.js server at "%url" with data "%data": [%code] %error.';
}
watchdog('nodejs', $log_message, $params);
}
return FALSE;
}
// No errors, so return Node.js server response.
return json_decode($response->data);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Nodejs:: |
public static | property | ||
Nodejs:: |
public static | property | ||
Nodejs:: |
public static | property | ||
Nodejs:: |
public static | property | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function | ||
Nodejs:: |
public static | function |