2012-02-03 38 views
1

這裏我們有Connect();功能:使用列表來連接和斷開

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); 
     }); 
} 

最終目標是斷開連接,而我的朋友告訴我這可以通過使用列表完成。我不熟悉列表,也不知道如何使用它們。

我在問的是把它放在'List'表格中,這樣(我假設...)我們可以使用'Clear'方法來斷開連接。或者可能是一個'刪除'的方法。

但就像我說的,我不確定如何使用列表,所以Clear方法可能意味着完全不同的東西。

請問您是否需要了解更多關於此功能的信息,我會盡快回復。但是從我列出的模糊信息來看,你不需要知道函數的每個細節。

+0

哈,大家來編輯。我似乎無法逃避那場比賽。 – Zenexer 2012-02-04 03:52:54

回答

3

你的朋友試圖說的是,你需要維護一個連接列表。列表僅僅是一個維護你的引用的對象。

完成所有連接後,您可以通過列表並「註銷」或以其他方式斷開與這些通道的連接。

+0

啊,好的。在這種情況下,應該很容易(一旦我瞭解了一些關於清單的更多信息......)並且更加有組織。 你會碰巧知道什麼會更適合斷開:刪除或清除? – 2012-02-03 22:34:24

1

刪除會更合適。因爲對於一個列表結構時使用類似...

list.Clear()//這將清除整個列表

如果你只是切斷一個客戶端,你不想刪除所有客戶,只有一個。

list.RemoveAt(int index)或list.Remove(Object o)只會從您的列表中刪除特定的客戶端。

這裏是一個鏈接,可能會有幫助:

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