Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by skoff · Nov 08, 2016 at 03:31 PM · serversocketsocketsauthoritativeauthoritative server

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Nov 09, 2016 at 10:28 PM 0
Share

Why do you need to keep a socket connection open if it's a turn-based game?

avatar image skoff tanoshimi · Nov 09, 2016 at 11:31 PM 0
Share

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

avatar image skoff · Nov 09, 2016 at 11:14 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to do a TCP Server? 4 Answers

Multiplayer game using Sockets or Socket.IO? (without UNET or Photon) 6 Answers

Use sockets to send data from unity 1 Answer

Why i can't receive 2 messages in a row from my server in Unity? 2 Answers

BinaryWriter or Serialization in mobile? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges