- Home /
C# Unity - Web Socket Server - Reading incoming messages from clients on new thread.,Unity C#. Creating a network stream within a new thread isn't looping?
SO this is my problem. hoping someone can shed some light.
I have created a Web TCP server script. When a new connection comes in, it creates a new object 'NetworkClientInfo'. The NetworkClientInfo has a constructor that will create a new thread for my 'ListenForMessages' method.
So when a new client connects, it creates this object and passes in the TcpClient object, which then creates a thread, which then forever is supposed to listen for messages from that client. Though its just freezing when I get to NetworkStream.GetStream.
See this code: https://pastebin.com/Htip7rdN
When running this, I would expect the log to show "Before, After" and then to spam "Length Before" every second. Yet instead it is just giving me "Before" and then nothing else happens. If i remove the NetworkStream = client.GetSteram. It runs as intended. Equally if i call the method directly from the main thread (instead of creating a new thread) it runs as expected.
Anyone know what is going on here? Here is my Incoming connections method as well.
Answer by copperhaxor55 · Jul 19, 2018 at 12:33 PM
Just to add, I said if you call it directly from the main thread. It runs fine. Technically its not called from the Main thread as the 'Listen for connections' is also running on its own thread. Though I shouldn't think this detail will matter? Its just the NetworkStream that seems to be messing it up.
Your answer

Follow this Question
Related Questions
Server - Transforms off screen not valid/not updating 2 Answers
Joining a Running Server (Game) and Waiting for Game State Data. How? 1 Answer
C# yield in server-side Co-Routine 0 Answers
Network - Change Varible Across Clients 0 Answers
OnConnectedToServer not called when testing on same machine 0 Answers