Messages

back to iNetwork

Introduction

Networks allow for interconnected systems to communicate and interact with each other. In comparison to other networking toolkits, such as the .Networking Toolkit (which uses a Shared Dictionary), the iNetwork Toolkit allows for Messages to be sent and received to and from the server, so that clients may be able to talk to each other. The simple way in which this toolkit allows for messages to be created, sent and received allows for different tasks to be executed.

back to top

Sending Messages

In order to communicate, messages must be created and sent. Both the client and the server have the ability to create messages.

Message msg = new Message("Name-of-Message");

It is through the name of the message that the client(s) and or server are able to tell which message is which. In addition, information is relayed between clients and between server and client by using the message's AddField method.

msg.AddField("Name-of-Field", Object);

This method only takes in certain objects types, which includes:

  • Bool
  • Byte
  • Char
  • Double
  • Float
  • Int
  • SByte
  • Short
  • UInt
  • ULong
  • UShort

Both client and server then sends out this message through one line of code. However it depends; if the client is the one sending a message that one line of code looks something like the following, where the message is then sent to the server via the connection:

this._connection.SendMessage(msg);

Now, if the server is the one sending a message to all the clients then the line of code looks something like the following:

this._server.BroadcastMessage(msg);

Note that the server is also able to exclude a client or list of clients from receiving the message it is sending out. For example, the server can relay a message to all clients with the exception of the client that the server received a message from prior to.

this._server.BroadcastMessage(msg, (Connection)sender);
back to top

Receiving Messages

Both the client and server receive messages in the same way. Since there can be many different messages passed from client to server and vice versa, they both need to be able to check which message is which. This is where the name of the method comes in very handy. Depending on the message, the client or server may execute different tasks.

switch (msg.Name)
{
    default:
        // Do nothing
        break;

    case "Message1":
        // Do something here
        break;

    case "Message2":
        // Do something else here
        break;

    case "Message3":
        // Do something else here
        break;
}

As previously mentioned, fields can be added to messages in order to relay information or data. When a message is received these fields can now be retrieved. The iNetwork Toolkit provides a number of different get methods in that there are a number of different object types that message fields can take in, such as intergers, strings and bytes. the following is a list of the the different methods provided:

  • GetBoolField
  • GetByteField
  • GetCharField
  • GetDoubleField
  • GetFloatField
  • GetIntField
  • GetSByteField
  • GetShortField
  • GetStringField
  • GetUIntField
  • GetULongField
  • GetUShortField

There is another method that can be used to retrieve data by specifying the name of the field and its type.

  • GetField("name-of-field", type)
back to top