Hello, @skoff.
I’m working on a simple MMO game with Unity and Go (GoLang) as server to test Unity networking with C#.
You can look at my starting project here. I have a simple code to notify server on key press.
I recommend to use native sockets, because are faster and server side with C++, Java or Go can handle a lot of users.
I’m using a simple structure for sockets.
Look at my code: https://github.com/izanbf1803/Unity-GoLang--MMO-Game
public class Message {
public int Len;
public SOCKET_TAG Tag;
public string Data;
}
First 4 bytes ( sizeof int ) are the length of the data. Next 4 bytes are the socket tag. ( enum ). Next bytes (size defined in the fist 4 bytes) are all the data.
I can make a switch with the tag: example:
switch (msg.tag) {
case TAG.SET_ID:
client.id = msg.data;
break:
case TAG.POSITION_CHANGED:
client.position = positionDecode(msg.data)
break;
}
The server side and the client are threaded. A simple thread for recive sockets and push to a queue, and the main thead reads the queue and uses the data:
private void Input() { // Threaded
while (true) {
try {
Message _in = Recv();
lock (queueLock) {
queue.Enqueue(_in);
}
} catch (SocketException e) {
break;
}
}
}
void ClientUpdate() {
while (client.queue.Count > 0) {
Message msg;
lock (client.queueLock)
msg = client.queue.Dequeue();
switch (msg.Tag) {
case SOCKET_TAG.SET_ID:
client.ID = int.Parse(msg.Data);
break;
}
}
}
How i create the struct? here you have:
public void Send(SOCKET_TAG tag, string msg) {
byte[] data = ascii.GetBytes(msg);
int len = data.Length;
byte[] packet = new byte[sizeof(int) * 2 + len];
packet[0] = (byte)(len);
packet[1] = (byte)(len >> 8); // this encodes an integer to 4 bytes:
packet[2] = (byte)(len >> 16); // 3060 -> [244, 11, 0, 0]
packet[3] = (byte)(len >> 24);
packet[4] = (byte)((int)tag);
packet[5] = (byte)((int)tag >> 8); // this encodes an integer to 4 bytes:
packet[6] = (byte)((int)tag >> 16); // 3060 -> [244, 11, 0, 0]
packet[7] = (byte)((int)tag >> 24);
System.Buffer.BlockCopy(data, 0, packet, sizeof(int)*2, len);
lock (sendLock)
conn.Send(packet, 0, packet.Length, SocketFlags.None);
}
Look at my repo for more info.