C# Sockets & multi-threading

Hi,

I am making a very simple game, with a socket server. On the client, I receive data in a new thread. When the player joins the game, the server sends a packet to the client containing world data. For example, players, when the client receives this, it has to spawn that player. But this is not possible from a new thread, only the main thread.

I tried making a system, that triggers a bool that a player has to spawn. But I think this can be done better. But how?

Thanks in advance,
Wiebren de Haan

Hi,
I have a quite similar scenario (also using Sockets in separate thread) and solved it quite similar to what you mentioned:

One central script for the scene is listening to the Sockets events and is responsible for spawning new GameObjects. Therefore it keeps a List to buffer incoming messages from the server, like Players to spawn.
Then in the Update() method this list is processed and GameObjects are instantiated as required.
Just don’t forget to enclose all accesses to the buffer list with a lock to avoid concurring accesses or race conditions.

Here is an example showing the logic for incoming text messages handled by a MessageHandler class:

    [RequireComponent(typeof(Text))]
    public class MessageHandler : MonoBehaviour
    {
        //setup data
        public float MessageDisplayDuration_sec = 4;
        //instance data
        private Text MessageOutput;
        private List<string> _MessagesToDisplay = new List<string>();
        private object _MessagesToDisplayLock = new object();
        private float _NextMessageTime;
    
        void Start()
        {
            MessageOutput = gameObject.GetComponent<Text>();
        }
    
        void Update()
        {
            if (_NextMessageTime < Time.unscaledTime)
            {
                displayNextMessage();
            }
        }
    
        /// <summary>
        /// Can be called from any thread to add messages to the buffer
        /// </summary>
        /// <param name="iMessageText"></param>
        /// <param name="iAllowDuplicates"></param>
        private void addMessageForDisplay(string iMessageText, bool iAllowDuplicates = false)
        {
            lock (_MessagesToDisplayLock)
            {
                //check duplicates
                if (!iAllowDuplicates && _MessagesToDisplay.Count>0)
                    if (_MessagesToDisplay[_MessagesToDisplay.Count - 1] == iMessageText) return;
                //add message to display
                _MessagesToDisplay.Add(iMessageText);
            }
        }
    
        private void displayNextMessage()
        {
            //Check message buffer
            lock (_MessagesToDisplayLock)
            {
                if (_MessagesToDisplay.Count > 0)
                {
                    //display next message and move from buffer to history
                    MessageOutput.text = _MessagesToDisplay[0];
                    _MessagesToDisplay.RemoveAt(0);
                    _NextMessageTime = Time.unscaledTime + MessageDisplayDuration_sec;
                }
                else
                {
                    //clear message output
                    MessageOutput.text = "";
                }
            }
        }
    }

Hope it helps…
Greetings!

reinski