C# 聊天程序 Lidgren

发布于 2024-08-13 02:16:21 字数 1996 浏览 5 评论 0原文

我编写了以下程序,该程序应该启动、显示表单并连接到服务器并获取消息。但是当我启动时什么也没有发生?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lidgren.Network;
using System.Threading;

namespace WindowsGame2
{
    public partial class Form1 : Form
    {
        private NetClient client;
        private NetBuffer buffer;

        public Form1()
        {
            InitializeComponent();
        }

        private void Connect()
        {
            NetConfiguration config = new NetConfiguration("xesh");
            NetClient client = new NetClient(config);

            client.Connect("75.127.105.216", 14242);

            NetBuffer buffer = client.CreateBuffer();
        }

        private void ReceiveMessages()
        {
            Connect();
            bool keepGoing = true;
            while (keepGoing)
            {
                NetMessageType type;
                while (client.ReadMessage(buffer, out type))
                {
                    switch (type)
                    {
                        case NetMessageType.DebugMessage:
                            Console.WriteLine(buffer.ReadString());
                            break;

                        case NetMessageType.StatusChanged:
                            Console.WriteLine("New status: " + client.Status + " Reason: " + buffer.ReadString());
                            break;

                        case NetMessageType.Data:
                            break;
                    }
                }
            }
        }

        private void Update(string str)
        {
            ReceiveMessages();
            textBox1.AppendText(str + "\r\n");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        static void Main(string[] args)
        {
            Form1 form = new Form1();
        }
    }
}

I wrote the following program that is suppose to start up, show the form and connect to the server and get an messages. However when I start it nothing happens?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lidgren.Network;
using System.Threading;

namespace WindowsGame2
{
    public partial class Form1 : Form
    {
        private NetClient client;
        private NetBuffer buffer;

        public Form1()
        {
            InitializeComponent();
        }

        private void Connect()
        {
            NetConfiguration config = new NetConfiguration("xesh");
            NetClient client = new NetClient(config);

            client.Connect("75.127.105.216", 14242);

            NetBuffer buffer = client.CreateBuffer();
        }

        private void ReceiveMessages()
        {
            Connect();
            bool keepGoing = true;
            while (keepGoing)
            {
                NetMessageType type;
                while (client.ReadMessage(buffer, out type))
                {
                    switch (type)
                    {
                        case NetMessageType.DebugMessage:
                            Console.WriteLine(buffer.ReadString());
                            break;

                        case NetMessageType.StatusChanged:
                            Console.WriteLine("New status: " + client.Status + " Reason: " + buffer.ReadString());
                            break;

                        case NetMessageType.Data:
                            break;
                    }
                }
            }
        }

        private void Update(string str)
        {
            ReceiveMessages();
            textBox1.AppendText(str + "\r\n");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        static void Main(string[] args)
        {
            Form1 form = new Form1();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上的星空 2024-08-20 02:16:21

您已声明:

    private NetClient client;        
    private NetBuffer buffer;

但是您的 Connect() 方法构造了新的本地客户端缓冲区,当该方法返回时,它们将超出范围。实例的客户端缓冲区永远不会被初始化,因此在 ReceiveMessages() 方法中使用时没有任何意义。

You have declared:

    private NetClient client;        
    private NetBuffer buffer;

However your Connect() method constructs new local client and buffer which will be out of scope when the method returns. The instance's client and buffer never get initialized and there fore will not be meaningful when used in the ReceiveMessages() method.

恬淡成诗 2024-08-20 02:16:21

你没有调用你的方法。

尝试:

static void Main(string[] args)
{
    Form1 form = new Form1();
    ReceiveMessages();
    // or 
    Update("Me");
}

您的表单上有按钮吗?有点需要更多信息

You aren't calling your methods.

try:

static void Main(string[] args)
{
    Form1 form = new Form1();
    ReceiveMessages();
    // or 
    Update("Me");
}

Do you have any buttons on your form? Kinda need a bit more information

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文