List Server Example: read/write title as UTF8 encoded bytes with length prefix

This commit is contained in:
vis2k 2019-04-04 13:31:09 +02:00
parent 5226ed64de
commit bfe27ffadf

View File

@ -106,9 +106,9 @@ void SendStatus()
// receive it in ParseMessage
writer.Write((ushort)NetworkServer.connections.Count);
writer.Write((ushort)NetworkManager.singleton.maxConnections);
char[] titleChars = gameServerTitle.ToCharArray();
writer.Write((ushort)titleChars.Length);
writer.Write(titleChars);
byte[] titleBytes = Encoding.UTF8.GetBytes(gameServerTitle);
writer.Write((ushort)titleBytes.Length);
writer.Write(titleBytes);
writer.Flush();
// list server only allows up to 128 bytes per message
@ -160,7 +160,7 @@ void ParseMessage(byte[] bytes)
ushort players = reader.ReadUInt16();
ushort capacity = reader.ReadUInt16();
ushort titleLength = reader.ReadUInt16();
string title = new string(reader.ReadChars(titleLength));
string title = Encoding.UTF8.GetString(reader.ReadBytes(titleLength));
//Debug.Log("PARSED: ip=" + ip + /*" port=" + port +*/ " title=" + title + " players=" + players + " capacity= " + capacity);
// build key