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 Window1() { InitializeComponent(); txtLog.Document.Blocks.Clear(); _client = new ChatClient(); ChatClient.StatusChanged += new StatusChangedEventHandler(mainServer_StatusChanged); addSmileyWindow = new AddSmileyWindow(this); addSmileyWindow.addSmileysToGrid(smileyClass.getAllSmileys()); }
private async void OnLogOn() { var client = new ChatClient(); await client.StartAsync(); try { var info = await client.LogOn(int.Parse(_userIdInput)); var current = Application.Current.MainWindow; var mainVM = new MainViewModel(client, info); var mainWindow = new MainWindow(); Application.Current.MainWindow = mainWindow; mainWindow.DataContext = mainVM; current.Close(); mainWindow.Show(); } catch (DuplicateIdException) { } }
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); }
public Form1() { InitializeComponent(); this.Client = new ChatClient(); this.Client.ChatUpdate += new ChatClient.ChatboxUpdate(listBox1_Update); }
public MainWindow() { InitializeComponent(); Client = new ChatClient(); }