Server Client Synchronization of about skipping problem

There are many online synchronization scheme, I looked, too much trouble, the couple can not read, do not look old. So I simply wrote about, to facilitate quick start.

Before doing, the server must be udp communications, I will not explain this, I use the lidgren communications framework, I do not know, you can look at my past articles.

Code of ideas, the server sends every second, you can do with the thread.

  public static void Confrontation()
        {
            while (true)
            {
                Thread.Sleep(1000);
                Output("目前" + Frame);
                List<NetConnection> all = s_server.Connections; // get copy
                //   all.Remove(msg.SenderConnection);
                if (all.Count > 0)
                {
                    NetOutgoingMessage om = s_server.CreateMessage();
                    JsonDatas message = new JsonDatas();
                    message.jsons["type"] = "5";
                    message.jsons["msg"] = Frame;
                    string json = JsonConvert.SerializeObject(message);
                    om.Write(json);
                    s_server.SendMessage(om, all, NetDeliveryMethod.ReliableOrdered, 0);
                }
                if (Frame%7==0)
                {
                    Frame += 2;
                }
                else
                {
                    Frame++;
                }
            }
        }

This is the thread inside the method, all on behalf of all online users socket port. Json data format to facilitate determination client. Frame is the frame, plus 1 per second, and finally I Frame% 7 for the convenience of skipping the test, that is, to a multiple of 7 frame, my frame is not plus 1, plus 2 but allow the client to determine the need skip frames.

 

Look at the client, because the client updata there is one second 60, I put him into one second 10. similar

 if (GameModel.getInstance().Rate == 6)
        {
            GameModel.getInstance().Rate = 0;
}   GameModel.getInstance().Rate++;

Then, I put the server sent me Frame multiplied by 10; correspondence;

There are three logical processing client mode, waiting for the normal operation, frame skipping.

Because the client is waiting for all the frames is finished, the server frame has not come, this time to wait, you can do pause.

Skipping is determined by the server and client frame frame difference is greater than 10. Then iterate so quickly so that the gap between the client and server within only 10.

Last result: before the data is the client of the current frame, followed by the service side frame

结尾等待是 我把服务器关了,客户端自然等待了。

最后代码:非常短

    void Update()
    {
        if (FrameBool && GameModel.getInstance().LogicalFrame > 0)
        {
            Frame = GameModel.getInstance().LogicalFrame;
            FrameBool = false;
        }
        if (FrameBool == false)
        {
            OnSetShaderFrame();
        }
    }

    //总负责 战斗 逻辑处理
    public void OnSetShaderFrame()
    {
        if (GameModel.getInstance().Rate == Rate)
        {
            GameModel.getInstance().Rate = 0;

            if (GameModel.getInstance().LogicalFrame == Frame)//帧相同,客户端等待
            {
                Debug.Log("-><color=#E88748>" + Frame + "客户端等待 " + GameModel.getInstance().LogicalFrame + "</color>");
            }
            else
            {
                Debug.Log("-><color=#90E848>" + Frame + "逻辑帧 " + GameModel.getInstance().LogicalFrame + "</color>");
                Frame++;//执行完毕 帧加一

                if (GameModel.getInstance().LogicalFrame - Frame > 7)//超过1秒 ,6帧 一秒
                {
                    for (int i = 0; i < (GameModel.getInstance().LogicalFrame - Frame); i++)//保留当前6帧,其他跳帧
                    {
                        Debug.Log("-><color=#E84848>" + Frame + "跳帧执行 " + GameModel.getInstance().LogicalFrame + "</color>");
                        Frame++;//执行完毕 帧加一
                    }
                }
            }

        }
        GameModel.getInstance().Rate++;
    }

祝大家好运!

Guess you like

Origin www.cnblogs.com/big-zhou/p/11387886.html