Unity delay in sending the current value when using sockets

前端 未结 2 1937
故里飘歌
故里飘歌 2021-01-15 17:08

The client.cs in Visual Studio.

private void SendToServer(string HeartRate)
    {
        SetHRTest(HeartRate);
        try
        {
            s = client.         


        
相关标签:
2条回答
  • 2021-01-15 17:29

    You are starting and shutting down the streams at every frame. Move your s, sr and sw initializations to the Start method and use OnApplicationQuit for closing.

    void OnApplicationQuit()
    {
       s.Close();
       soc.Close();
    }
    

    Doing so Unity will still hang as ReadLine is not asynchronous and will lock the execution until your socket receives data.

    First two ways I can think of to go around this are:

    • using threads
    • using async callbacks (which I do in my UDP servers in Unity 3D). See this question: What is AsyncCallback?
    0 讨论(0)
  • 2021-01-15 17:30

    I think it's the fact that you put all the server stuff in the Update. Which means it'll get called every frame. Which is a lot.

    You should probably put all this in a separate method and call that from the client.

    0 讨论(0)
提交回复
热议问题