XNA中的Lidgren连接(客户端/服务器)

时间:2013-11-05 21:36:16

标签: c# xna lidgren

我对这个“后帮助论坛”的事情有点新鲜,如果我做错了什么的话,对我这么做。哦,是的......我到处寻找有用的东西。所以像我一样绝望,我最终到了这里。 无论如何,我们转到实际问题:

有人可以请求告诉我如何在lidgren中发送数据类型的消息吗? 如果我理解正确,你手动发送数据时会得到数据类型吗? 虽然我已经这样做了,但它不起作用......

我认为有点奇怪的是,client.ServerConnection始终为null,尽管我得到了一个连接...也许它可能是一些有用的信息。

以下是代码:

class Program
{
    static NetServer server;
    static NetPeerConfiguration config;

    static void Main(string[] args)
    {
        //Initialize configuration for server and start the server
        config = new NetPeerConfiguration("chat");
        config.Port = 14242;
        config.LocalAddress = NetUtility.Resolve("localhost");
        config.MaximumConnections = 10;
        config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);

        server = new NetServer(config);

        server.Start();
        Console.WriteLine("Server is up and running!");

        NetIncomingMessage inc;

        Console.WriteLine("Waiting for connections...");
        while (true)
        {
            if ((inc = server.ReadMessage()) != null)
            {

                switch (inc.MessageType)
                {
                    case NetIncomingMessageType.ConnectionApproval:
                        Console.WriteLine("Incomnig LOGIN");
                        inc.SenderConnection.Approve();
                        NetOutgoingMessage outmsg = server.CreateMessage();
                        outmsg.Write("Welcome to my ChatRoom. Please behave, otherwise I'll eat you!");
                        outmsg.Write((byte)PacketType.MESSAGE);
                        server.SendMessage(outmsg, inc.SenderConnection, NetDeliveryMethod.ReliableOrdered, 0);
                        Console.WriteLine("Approved new connection.");
                        break;
                    case NetIncomingMessageType.Data:
                            outmsg = server.CreateMessage(inc.ReadString());
                            server.SendMessage(outmsg, server.Connections, NetDeliveryMethod.ReliableOrdered, 0);
                        break;
                    case NetIncomingMessageType.StatusChanged:
                        Console.WriteLine(inc.SenderConnection.ToString() + " Status Changed To: " + (NetConnectionStatus)inc.SenderConnection.Status);
                        break;

                }
               server.Recycle(inc);
            }

        }
    }
}

}

class Program
{
    static NetClient client;

    static string hostip = "localhost";
    static int port = 14242;
    static bool isRunning = true;
    static bool notConnected = true;
    static void Main(string[] args)
    {

        NetPeerConfiguration config = new NetPeerConfiguration("chat");

        client = new NetClient(config);

        client.Start();

        client.Connect("localhost", 14242);

        Console.WriteLine("Client started. Checking for connection approval.");

        if (client.ServerConnection != null) 
        CheckForConnetionApproval();

        while (isRunning)
        {   
            ReadMessages(); 
            GetInputAndSendItToServer();
        }

    }

    private static void ReadMessages()
    {
        NetIncomingMessage inc;

        while ((inc = client.ReadMessage()) != null)
        {
            switch (inc.MessageType)
            {
                case NetIncomingMessageType.Data:
                     Console.WriteLine(inc.ReadString());
                    break;
            }
        }
    }

    private static void CheckForConnetionApproval()
    {
        bool CanStart = false;

        NetIncomingMessage inc;

        while (!CanStart)
        {
            if ((inc = client.ReadMessage()) != null)
            {
                switch (inc.MessageType)
                {
                    case NetIncomingMessageType.Data:
                        Console.WriteLine(inc.ReadString());
                        CanStart = true;
                        break;
                    case NetIncomingMessageType.StatusChanged:
                        Console.WriteLine("Status: " + (NetConnectionStatus)inc.SenderConnection.Status);
                        break;
                    default:
                        //Console.WriteLine(inc.ReadString());
                        break;
                }
            }
        }
    }

    static string text = "";
    private static void GetInputAndSendItToServer()
    {
        ConsoleKeyInfo kinfo = Console.ReadKey();
        while (kinfo.Key != ConsoleKey.Enter)
        {
            text = Console.ReadLine();
        }

        NetOutgoingMessage outmsg = client.CreateMessage(text);
        client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered);
    }
}

由于

HndrenG

0 个答案:

没有答案