简体   繁体   English

使用列表连接和断开连接

[英]Using a list to connect & disconnect

Here we have the Connect(); 这里我们有Connect(); function: 功能:

public static void Connect(string username, string password, string roomName, string roomType, string roomID, string roomPass, bool roomVisible)
{

    Console.WriteLine("[Bot] Trying to login...");
    PlayerIO.QuickConnect.SimpleConnect("everybody-edits-su9rn58o40itdbnw69plyw", username, password,
        delegate(Client client)
        {
            Console.WriteLine("[Bot] Logged in. Trying to join the room...");

            Dictionary<string, string> roomData = new Dictionary<string, string>();
            Dictionary<string, string> joinData = new Dictionary<string, string>();

            roomData.Add("editkey", roomPass);
            roomData.Add("name", roomName);
            joinData.Add("editkey", roomPass);


            try
            {
                con = client.Multiplayer.CreateJoinRoom(roomID, roomType, roomVisible, roomData, joinData);
                if (con.Connected)
                {
                    con.Send("init");
                    con.Send("init2");
                    con.OnMessage += new MessageReceivedEventHandler(OnMessage);
                    con.OnDisconnect += delegate(object sender, string reason)
                    {
                        Console.WriteLine("Disconnected, Error: " + reason);
                    };
                    Console.WriteLine("[Bot] Joined the world.");
                }
            }
            catch (PlayerIOError error)
            {
                Console.WriteLine("Error: " + error);

            }

        },
        delegate(PlayerIOError error)
        {
            Console.WriteLine("Error: " + error);
        });
}

The ultimate goal is to disconnect, and my friend tells me this can be done with the use of a list. 最终目标是断开连接,我的朋友告诉我,可以使用列表来完成此操作。 I am not familiar with lists, nor do I know how to use them. 我不熟悉列表,也不知道如何使用它们。

What I am asking is to put this in 'List' form so that (I'm assuming...) we can use a 'Clear' method to possibly disconnect. 我要问的是将其以“列表”形式放置,以便(我假设...)我们可以使用“清除”方法断开连接。 Or maybe a 'Remove' method. 或者也许是“删除”方法。

But like I said, I'm not sure how to use Lists at all, so the Clear method might mean something completely different. 但是就像我说的那样,我完全不确定如何使用列表,因此Clear方法可能意味着完全不同的东西。

Please ask if you need to know more about this function, I'll try to get back as soon as possible. 请询问您是否需要了解更多有关此功能的信息,我会尽快恢复。 But from the vague information I have about lists, you don't need to know every detail of the function. 但是从我关于列表的模糊信息中,您不必知道函数的每个细节。

What your friend is trying to say is that you need to maintain a list of your connections. 您的朋友想说的是,您需要维护一个连接列表。 A List is simply an object that will maintain your references. 列表只是一个将保留您的引用的对象。

Once you are done with all of the connections you can go through the list and "log off" or otherwise disconnect from those channels. 完成所有连接后,您可以浏览列表并“注销”或以其他方式断开与这些通道的连接。

A remove would be more appropriate. 删除将更合适。 Because a for a list structure when you use something like... 因为当您使用类似...的列表结构

list.Clear() //This clears the entire list list.Clear()//这将清除整个列表

And if you are just disconnecting one client, you would not want to remove all clients, just the one. 而且,如果您只是断开一个客户端的连接,则您将不希望删除所有客户端,而只希望删除一个客户端。

list.RemoveAt(int index) or list.Remove(Object o) would just remove a specific client from your list. list.RemoveAt(int index)或list.Remove(Object o)只会从列表中删除特定的客户端。

Here is one link that might help: 这是一个可能有用的链接:

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx http://msdn.microsoft.com/zh-CN/library/6sh2ey19.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM