lidgren-network-gen3
lidgren-network-gen3 copied to clipboard
Possible client send to all or read random incoming messages?
Hey I am working with unity 3d. I try to make a framework where clients later can send messages with random sizes.. so you can write float int, int... or int, float, int if you want.
How is possible to handle it via server. I don't know how I can read incoming message with random stuff or can I forward to all OTHER clients form a client (which goes through the server)? I want to make the server possible to use like a relay server.
Hello,
Yes it is possible you need to write an enum with all your possible values, e.g.:
enum PossibleTypes
{
Type_Int,
Type_Double,
Type_Bool
// etc..
}
after that you need to write a byte from enum to message before you write a value (integer, double, float, bool, etc.), e.g.: (Note: This is for client-side.)
NetOutgoingMessage netOutgoingMessage = this.netClient.CreateMessage();
netOutgoingMessage.Write((byte)PossibleTypes.Type_Int);
netOutgoingMessage.Write(30);
netOutgoingMessage.Write((byte)PossibleTypes.Type_Double);
netOutgoingMessage.Write(3890.6879);
netOutgoingMessage.Write((byte)PossibleTypes.Type_Bool);
netOutgoingMessage.Write(true);
netOutgoingMessage.Write((byte)PossibleTypes.Type_Double);
netOutgoingMessage.Write(58.65);
netOutgoingMessage.Write((byte)PossibleTypes.Type_Int);
netOutgoingMessage.Write(78);
// TODO: Write your custom values.
this.netClient.SendMessage(netOutgoingMessage, NetDeliveryMethod.ReliableOrdered);
On the server side or other client side you can read them by reading the first byte and cast it to enum value after that read the next bytes for the specific type, e.g.:
while (netIncomingMessage.PositionInBytes < netIncomingMessage.LengthBytes - 1)
{
PossibleTypes Type = (PossibleTypes)netIncomingMessage.ReadByte();
if (Type == PossibleTypes.Type_Int)
{
Console.WriteLine(netIncomingMessage.ReadInt32());
}
else if (Type == PossibleTypes.Type_Double)
{
Console.WriteLine(netIncomingMessage.ReadDouble());
}
else if (Type == PossibleTypes.Type_Bool)
{
Console.WriteLine(netIncomingMessage.ReadBoolean().ToString());
}
}
In the end you can create a new message and send it to other clients (that on server side).
Thank you very much. I thought about something like that but I didn't know how to know how many incoming messages holds types in it.
But I see it now
while (netIncomingMessage.PositionInBytes < netIncomingMessage.LengthBytes - 1)
@MaZyGer with pleasure :)