Class Message
Namespace: Nonoba.GameLibrary
Language: C# / (.NET)
The Message class represents a message between a client and the server.
A message consists of a string type, and a payload of zero or more typed parameters
For example here we create a simple message that the server broadcasts to connected players indicating that a round is starting
Message roundStarting = new Message("start"); // message of type start
Broadcast(roundStarting) // broadcast to all connected players
As another example, here we're building a message containing the username of the connected players.
using System;
using Nonoba.GameLibrary;
namespace MyGame {
public class Game : NonobaGame<NonobaGameUser> {
public override void UserJoined(NonobaGameUser player) {
Message joinMessage = new Message("join"); // create a new message
for (int i = 0; i < Users.Length; i++) {
// add all the currently connected usernames to the message
joinMessage.Add(Users[i].Username);
}
player.Send(joinMessage);
}
// We're not using these methods in this example
public override void GotMessage(NonobaGameUser player, Message m) { }
public override void GameStarted() { }
public override void UserLeft(NonobaGameUser player) { }
}
}
Public Properties
string Type
The type given to this message. This value can only be read.
uint Count
The amount of parameters in the payload.
Constructors
new Message(string type)
Create a new message of the given type
new Message(string type, params object[] parameters)
Create a new message of the given type with the payload given in the parameters
Public Methods
string GetString(uint index)
Gets the string value from the payload at the given index.
byte[] GetByteArray(uint index)
Gets the byte[] value from the payload at the given index. Not currently supported.
bool GetBoolean(uint index)
Gets the boolean value from the payload at the given index.
double GetDouble(uint index)
Gets the double value from the payload at the given index.
float GetFloat(uint index)
Gets the float value from the payload at the given index.
int GetInteger(uint index)
Gets the integer value from the payload at the given index. Same as GetInt().
int GetInt(uint index)
Gets the integer value from the payload at the given index. Same as GetInteger().
void Add(string value)
Adds a string value at the end of the message payload.
void Add(byte[] value)
Adds a byte[] value at the end of the message payload. Not currently supported.
void Add(boolean value)
Adds a boolean value at the end of the message payload.
void Add(double value)
Adds a double value at the end of the message payload.
void Add(float value)
Adds a float value at the end of the message payload.
void Add(integer value)
Adds a integer value at the end of the message payload.
void Add(params object[] parameters)
Adds a series of objects to the end of the message payload.
The Get methods
string GetString(uint index)
byte[] GetByte[](uint index)
bool GetBoolean(uint index)
double GetDouble(uint index)
float GetFloat(uint index)
int GetInteger(uint index)
int GetInt(uint index)
Gets the typed value from the message payload at the specified index.
Examples// Create a message Message m = new Message("hi",1,true,false,"hello",0.4F) // look at the values in the message string type = m.Type; int integer = m.GetInt(0); bool trueBoolean = m.GetBoolean(1); bool falseBoolean = m.GetBoolean(2); string stringValue = m.GetString(3); float floating = m.GetFloat(4);
The Add methods
void Add(string value)
void Add(byte[] value)
void Add(bool value)
void Add(double value)
void Add(float value)
void Add(int value)
void Add(params object[] parameters)
Adds values to the end of the message payload.
Examples// Create a message Message m = new Message("test") m.Add("hello world"); // add a string to index 0 m.Add(true); // add a boolean value to index 1 m.Add(0.4F); // add a floating point number to index 2; // The Add(params object[] parameters) method is a shortcut for adding multiple // parameters at the same time m.Add("hello",false,"world"); // add "hello" to index 3, false to index 4, and "world" to index 5