public static void Main(string[] args) { var client = new ChatClient(); client.OnGotInfo += (o, e) => { foreach (var f in e.OnlineUsers) Console.WriteLine(f); }; client.OnGotMessage += (o, e) => Console.WriteLine($"Got message fron {e.SourceId}! {e.ChatContent}"); client.OnGotError += (o, e) => { if (e.Error == ErrorKind.DuplicateId) Console.WriteLine("Duplicated ID!"); }; client.Start(); while(true) { try { Console.WriteLine("Input ID!"); var idStr = Console.ReadLine(); var id = Int32.Parse(idStr); client.LogOn(id).Wait(); break; } catch(AggregateException e) { e.Handle(ex => ex is DuplicateIdException); } } while (true) { var input = Console.ReadLine(); try { var m = Regex.Match(input, @"^\s*(?<command>\S*)"); if (m.Success) { var collection = m.Groups["command"]; switch (collection.ToString()) { case "sendto": var subStr = input.Substring(m.Index + "sendto".Length); var m2 = Regex.Match(subStr, @"\s*(?<id>\d+)\s+(?<message>.*)"); if (m2.Success) { client.Chat(Int32.Parse(m2.Groups["id"].ToString()), m2.Groups["message"].ToString()); } break; default: Console.WriteLine("Unsupported!"); break; } } } finally { } } }
public MainViewModel(ChatClient client, LogOnRequestResult result) { _client = client; UserId = client.UserId; foreach (var u in result.OnlineUsers) if (u != UserId) Messages.Add(new ChatMessages(u)); _client.OnGotMessage += (o, e) => { Action del = () => { foreach (var item in Messages) { if (item.DestUserId == e.SourceId) item.Messages.Add(new Message(e.SourceId, e.ChatContent)); } }; Application.Current.Dispatcher.BeginInvoke(del); }; _client.OnGotNewBody += (o, e) => { Action del = () => Messages.Add(new ChatMessages(e.NewUserId)); Application.Current.Dispatcher.BeginInvoke(del); }; SendCommand = new DelegateCommand(() => { var current = Messages[CurrentIndex]; _client.Chat(current.DestUserId, MessageToSend); current.Messages.Add(new Message(UserId, MessageToSend)); }, () => MessageToSend != string.Empty); }