Sending and Receiving a Room Module Message with JavaScript

This example shows how to send a room module message from a JavaScript client, and then receive that room module message in a JavaScript room module.

Here's the HTML code for the JavaScript Union client that sends the room module message.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Send Module Message</title>

<!--CSS-->
<style type="text/css">
#output {
  border: inset 2px;
  height: 100px;
  width: 400px;
  overflow: auto;
  padding: 5px;
  margin-bottom: 5px
}
</style>

<!--Load the OrbiterMicro JavaScript library-->
<script type="text/javascript" src="OrbiterMicro_1.1.0.514_Release_min.js"></script>

<!--Application code-->
<script type="text/javascript" src="SendRoomModuleMessage.js"></script>

</head>

<body onload="init()">
<!--Contains the application output-->
<div id="output"></div>

</body>
</html>

Here's the JavaScript code for the JavaScript Union client. It creates the room with the module that will receive the message, then joins the room, then sends the message.

//==============================================================================
// VARIABLES
//==============================================================================
var orbiter;
var msgManager;
var UPC = net.user1.orbiter.UPC;
var roomID = "examples.testroom";

//==============================================================================
// INITIALIZATION
//==============================================================================
function init () {
  // Create the Orbiter instance, used to connect to and communicate with Union
  orbiter = new net.user1.orbiter.Orbiter();

  // Enable logging to the browser's JavaScript console
  orbiter.getLog().setLevel("debug");
  orbiter.enableConsole();

  // If required JavaScript capabilities are missing, abort
  if (!orbiter.getSystem().isJavaScriptCompatible()) {
    displayChatMessage("Your browser is not supported.");
    return;
  }

  // Register for Orbiter's connection events
  orbiter.addEventListener(net.user1.orbiter.OrbiterEvent.READY, readyListener, this);
  orbiter.addEventListener(net.user1.orbiter.OrbiterEvent.CLOSE, closeListener, this);

  // Connect to Union
  orbiter.connect("yourserver.com", 80);
  displayMessage("Connecting to Union...");
}

//==============================================================================
// ORBITER EVENT LISTENERS
//==============================================================================
// Triggered when the connection is ready
function readyListener (e) {
  // Register for incoming messages from Union
  msgManager = orbiter.getMessageManager();
  msgManager.addMessageListener(UPC.JOINED_ROOM, joinedRoomListener, this);

  displayMessage("Connected.");
  displayMessage("Joining room...");

  // Create the room with the module that will receive the message
  msgManager.sendUPC(UPC.CREATE_ROOM,
    roomID,
    null,
    null,
    "script|RoomModuleMessageReceiver.js");

  // Join the room
  msgManager.sendUPC(UPC.JOIN_ROOM, roomID);
}

// Triggered when the connection is closed
function closeListener (e) {
  displayMessage("Orbiter connection closed.");
}

//==============================================================================
// ROOM EVENT LISTENER
//==============================================================================
// Triggered when a JOINED_ROOM message is received
function joinedRoomListener () {
  displayMessage("Room joined. Sending module message. Check server log.");

  // Send the room-module message, along with two test arguments
  msgManager.sendUPC(net.user1.orbiter.UPC.SEND_ROOMMODULE_MESSAGE,
                     roomID,
                     "TEST_MESSAGE",
                     "testArg1"+ "|" + "testValue1",
                     "testArg2"+ "|" + "testValue2");
}

//==============================================================================
// UI
//==============================================================================
// Displays a single message
function displayMessage (message) {
  // Make the new chat message element
  var msg = document.createElement("span");
  msg.appendChild(document.createTextNode(message));
  msg.appendChild(document.createElement("br"));

  // Append the new message to the chat
  var output = document.getElementById("output");
  output.appendChild(msg);

  // Trim the output to 500 messages
  if (output.childNodes.length > 500) {
    output.removeChild(output.firstChild);
  }
  output.scrollTop = output.scrollHeight;
}

Here's the code for the JavaScript room module. To use the code, copy it to a file named RoomModuleMessageReceiver.js, and place that file in Union Server's modules directory.

//==============================================================================
// IMPORTS
//==============================================================================
importClass(net.user1.union.core.event.RoomEvent);
importClass(org.apache.log4j.Logger);

//==============================================================================
// VARIABLES
//==============================================================================
var moduleContext;
var wrapper;
var log = Logger.getLogger("RoomModuleMessageReceiver");

//==============================================================================
// REQUIRED ROOM-MODULE INTERFACE METHODS
//==============================================================================
function init (ctx, wrap) {
  moduleContext = ctx;
  wrapper = wrap;

  // Register onModuleMessage to be executed when a module message arrive
  wrapper.addRoomEventListener(RoomEvent.MODULE_MESSAGE, "onModuleMessage");
}

function shutdown () {
  // Clean up resources
}

//==============================================================================
// LISTENER FUNCTIONS
//==============================================================================
// Triggered when any modue message arrives
function onModuleMessage (e) {
  log.debug("Module message received.");
  log.debug("    From client: " + e.getClient().getClientID());
  log.debug("    Message name: " + e.getMessage().getMessageName());
  log.debug("    testArg1's value: " + e.getMessage().getArg("testArg1"));
  log.debug("    testArg2's value: " + e.getMessage().getArg("testArg2"));

  // Print a list of all module message arguments in the log
  log.debug("    All message arguments:");

  var args = e.getMessage().getArgs();
  var argIterator = args.entrySet().iterator();
  var arg;
  while (argIterator.hasNext()) {
    arg = argIterator.next();
    log.debug("        " + arg.getKey() + " = " + arg.getValue());
  }
}