- Home /
Create single server for multi-platform game (pc, mobile, etc)
Hi, I've started a project which is a multiplayer turn-based racing game. I need a way to have a single server which all players from any platforms will connect to. All that I've seen right now in tutorials is always code on the client with the "isServer" and "isClient" everywhere. I would like to know if I could use websocket like socket.io to have a single server handle all my events. Is websocket supported by all platform Unity supports?
Please share you thoughts and experience!
FYI: I'm not an expert in Unity but i'm a full-time programmer (web and back-end)
Thank you
Why do you need to keep a socket connection open if it's a turn-based game?
how could I handle new data to be sent to other players when a player has played his turn? I want to use it also for a chat system inside the game
Nobody can provide me an answer? :| I know this subject has been probably covered in some other posts but I've never found a precise answer.
Answer by izanbf1803 · Jan 03, 2017 at 07:38 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612100315im_/https://answers.unity.com/themes/thub/images/avi.jpg)