使用Lidgren.Network解释NetIncomingMessage

时间:2019-03-07 06:48:35

标签: c# monogame lidgren

我还没有找到答案,所以我不确定是我遗漏了一部分难题还是对Lidgren Network库的一部分造成了误解。我有一个能够成功连接的小型服务器/客户端项目,但是当它连接成功时,控制台只会输出“ NetIncomingMessage #XXX”,而不是我在消息中输入的任何数据(例如,类似字符串“你好”)。我怎么知道我接收的数据里面到底有什么?有没有一种方法可以解释消息,以便接收端可以基本上说:“哦,这是播放器Y的坐标”,然后再读取它们?我以为您可能必须自定义NetIncomingMessageType之类的东西,但是我一直在疯狂地挖掘,并花了很长时间试图弄清楚这一点。如何区分聊天消息和玩家坐标?有没有一种方法可以创建自定义消息类型,以便接收端只知道它接收的是哪种类型的数据包,因此当数据包只是原始数字时,我不会尝试始终读取字符串,反之亦然?

作为参考,这是我正在使用的代码:

namespace NetworkSendReceiveTest
{
    class Program
    {
        static bool IsInitialized = false;
        static bool IsNetworkHost;
        static bool IsLooping = true;
        static string HostIP;

        static NetServer Server;
        static NetPeerConfiguration NetConfig;
        static NetClient Client;



        static void Main(string[] args)
        {
            AskIfHosting();

            while(IsLooping == true)
            {
                if(IsNetworkHost == true)
                {
                    ServerUpdate();
                }
                else
                {
                    HostUpdate();
                }
            }
        }

        static void AskIfHosting()
        {
            Console.WriteLine("Are you Hosting? Y/N : ");
            char hostAnswer = Convert.ToChar(Console.ReadLine());

            switch (hostAnswer)
            {
                case 'Y':
                    {
                        // Is the Server
                        IsNetworkHost = true;

                        NetConfig = new NetPeerConfiguration("NetworkTest") { Port = 15051 };
                        Server = new NetServer(NetConfig);
                        Server.Start();

                        //NetConfig.EnableMessageType(NetIncomingMessageType.ConnectionApproval);

                        break;
                    }
                case 'N':
                    {
                        // Not the Server
                        IsNetworkHost = false;

                        NetConfig = new NetPeerConfiguration("NetworkTest") { Port = 15051 };
                        Client = new NetClient(NetConfig);
                        Client.Start();

                        Console.WriteLine("\nPlease Enter Host IP: ");
                        HostIP = Console.ReadLine();

                        Client.Connect(host: HostIP, port: 15051);

                        //NetOutgoingMessage approval = Client.CreateMessage();
                        //approval.Write("secret15051");
                        //Server.Connect(HostIP, 15051, approval);
                        break;
                    }
                default:
                    {
                        IsNetworkHost = false;
                        break;
                    }
            }

            IsInitialized = true;
        }
        static void ServerUpdate()
        {
            NetIncomingMessage msg;
            while ((msg = Server.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.ErrorMessage:
                        Console.WriteLine(msg.ReadString());
                        break;
                    case NetIncomingMessageType.ConnectionApproval:
                        {
                            string s = msg.ReadString();
                            if (s == "secret15051")
                            {
                                msg.SenderConnection.Approve();
                                Console.WriteLine("\nNew Connection Incoming: Approved");
                            }
                            else
                            {
                                msg.SenderConnection.Deny();
                                Console.WriteLine("\nNew Connection Incoming: Denied");
                            }

                            break;
                        }
                    case NetIncomingMessageType.Data:
                        {
                            string message = msg.ReadInt32().ToString();
                            Console.WriteLine(msg);
                            break;
                        }
                    default:
                        Console.WriteLine("Unhandled type: " + msg.MessageType);
                        break;
                }

                Server.Recycle(msg);
            }
        }
        static void HostUpdate()
        {
            var message = Client.CreateMessage();
            message.Write(42);

            Client.SendMessage(message, NetDeliveryMethod.ReliableOrdered);
        }
    }
}

0 个答案:

没有答案