Skip to content

Techinques and best practices

Wikiti edited this page Feb 20, 2017 · 3 revisions

Here's a list of techniques and best practices which may be useful to you:

Message verbs

To group messages, you can use whatever syntax you like inside the message body. However, we recommend you using the verb strategy; just create a verb attribute, and assign a meaning to it to distinguish message types. For example:

server.addEventListener(NetworkEvent.MESSAGE_RECIEVED, function(e: NetworkEvent) {
  switch(e.data.verb) {
    case 'send_message':
      trace(e.data.str);
    case 'give_me_a_puppy':
      e.client.send({ name: e.data.name, age: 2, breed: 'Dalmatian' });
  }
});
client.send({ verb: 'send_message', str: 'Hello!' });
client.send({ verb: 'give_me_a_puppy', name: 'Cooper' });

Internally, the library uses a few message verbs to handle some cases:

_core:
  messages:
    auto_verb: "Flag to determine if the current message is a event-driven shortcut."
  sync:
    update_client_data: "Update client information. Required params: uuid(String)"
  errors:
    server_full: 'The server is full.'

Core message handling will prevent event propagation: you won't have to handle them manually.

Message verbs (events-driven shortcut)

You can also use the trigger and on method to automatically manage verbs, so you can separate them easily.

server.trigger("say_hi", { to: "world" });
client.on("say_hi", function(data: Dynamic) {
  trace('Hello, ${data.to}!');
});

Which is a pseudo-equivalent of:

server.send({ verb: "say_hi", to: "world" });
client.addEventListener(NetworkEvent.MESSAGE_RECIEVED, function(e: NetworkEvent) {
  if(e.verb == "say_hi") {
    trace('Hello, ${e.data.to}!');
  }
});

The first case is has more semantic meaning, which improves readability.

Note that clients can also trigger events into the server:

client.trigger("respond", { date: Date.now() });
server.on("respond", function(data: Dynamic) {
  trace('Hello, server! Today's ${data.date}');
});

If you need to check who the sender is, use an extra parameter of type NetworkEvent, which will contain information about the network event.

session.on("verb", function(data: Dynamic, e: NetworkEvent) {
  trace(e.data); // { verb: 'verb', value: 'test', etc: 'etc' }
  trace(e.sender); // { uuid: '...', active: true }
});

This argument is available for callbacks in both server and client mode.

For more complex situations, we encourage you to use raw event handling (with addEventListeners) instead of these quick shortcuts.

Multiple sessions handling

If you want (or need) to handle multiple connections at once, you can use as many sessions as you wish. For example, to handle 2 servers and a client at the same time:

var server1 = Network.registerSession(NetworkMode.SERVER, {
  ip: '0.0.0.0',
  port: 8888,
  max_connections: 4
});
var server2 = Network.registerSession(NetworkMode.SERVER, {
  ip: '0.0.0.0',
  port: 8889,
  max_connections: 4
});

var client1 = Network.registerSession(NetworkMode.CLIENT, {
  ip: '89.73.42.3',
  port: 7777
});

server1.start();
server2.start();
client1.start();

The registered sessions will be available in Network.sessions list:

trace(Network.sessions.length); // 3