You are here

function nodejs_message_handler in Node.js integration 6

Same name and namespace in other branches
  1. 7 nodejs.module \nodejs_message_handler()

Menu callback: handles all messages from Node.js server.

1 string reference to 'nodejs_message_handler'
nodejs_menu in ./nodejs.module
Implements hook_menu().

File

./nodejs.module, line 400

Code

function nodejs_message_handler() {
  if (!isset($_POST['serviceKey']) || !nodejs_is_valid_service_key($_POST['serviceKey'])) {
    drupal_json(array(
      'error' => t('Invalid service key.'),
    ));
    exit;
  }
  if (!isset($_POST['messageJson'])) {
    drupal_json(array(
      'error' => t('No message.'),
    ));
    exit;
  }
  $message = json_decode($_POST['messageJson'], TRUE);
  $response = array();
  switch ($message['messageType']) {
    case 'authenticate':
      $response = nodejs_auth_check($message);
      break;
    case 'userOffline':
      nodejs_user_set_offline($message['uid']);
      break;
    default:
      $handlers = array();
      foreach (module_implements('nodejs_message_callback') as $module) {
        $function = $module . '_nodejs_message_callback';
        $handlers += $function($message['messageType']);
      }
      foreach ($handlers as $callback) {
        $callback($message, $response);
      }
  }
  drupal_alter('nodejs_message_response', $response, $message);
  drupal_json($response ? $response : array(
    'error' => t('Not implemented'),
  ));
  exit;
}