1

ledgren でデータ型メッセージを送信するにはどうすればよいですか? 私がそれを正しく理解していれば、手動で送信するとデータ型を取得できますか? やったのに、うまくいかない…

ちょっと奇妙だと思うのは、接続を取得しても 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);
    }
}
4

0 に答える 0