void OnDestroy() { if (_client != null) { _client.Close(); _client.OnClose -= _client_OnClose; _client.OnOpen -= _client_OnOpen; _client.OnChat -= _client_OnChat; _client = null; } }
static void Main() { Console.WriteLine("Application start...[ThreadID:{0}]", Thread.CurrentThread.ManagedThreadId); string app_identifier = "chat"; string server_ip = "127.0.0.1"; int server_port = 11515; int max_client = 100; ChatClient client = new ChatClient(); client.Init(app_identifier); ChatServer server = new ChatServer(); server.Init(app_identifier, server_port, max_client); while (true) { Console.Write("command> "); string line = Console.ReadLine(); if (line == "quit") { client.Close(); server.Stop(); break; } else if (line == "start server") { server.Start(); } else if (line == "connect") { client.Connect(server_ip, server_port); } else if (line.IndexOf("login") >= 0) { string[] token = line.Split(' '); if (token.Length >= 2) { client.Login(token[1]); } else { Console.WriteLine("Invalid command."); } } else if (line.IndexOf("logout") >= 0) { client.Logout(); } else if (line.IndexOf("send") >= 0) { string[] token = line.Split(' '); if (token.Length == 3) { client.Send(token[1], token[2]); } else { Console.WriteLine("Invalid command."); } } else { if (line.Length > 0 && client.IsLogin()) { client.SendToAll(line); } } } Console.WriteLine("Application is quitted!"); Console.Read(); }