private void LoginResult(object e) { SocketMessage msg = new SocketMessage(e); object error = msg.GetValue("Error"); if (error != null) { return; } object id = msg.GetValue("ID"); int userID = -1; if (id != null) { userID = int.Parse(id.ToString()); } CurrentUser = new UserInfo( userID, msg.GetValue("UserName")?.ToString(), msg.GetValue("RealName")?.ToString(), msg.GetValue("Description")?.ToString(), msg.GetValue("UserImage")?.ToString() ); ClientSocket.Send("friendslist", new SocketMessage(new object[] { "UserName", CurrentUser.UserName })); }
private void ClientSocket_OnMessagereceived(SocketMessage e) { string dataString = e.GetValue("DataType") == null ? "" : e.GetValue("DataType").ToString(); Reply.ContentType dataType = (Reply.ContentType)Enum.Parse(typeof(Reply.ContentType), dataString); string[] urls = null; if (e.Data.ContainsKey("Links")) { urls = JsonConvert.DeserializeObject <string[]>(JsonConvert.SerializeObject(e.Data["Links"])); } MessageBlock block; if (dataType == Reply.ContentType.Picture && urls != null) { block = new MessageBlock(urls, e.GetValue("Message").ToString()); } else { block = new MessageBlock(); } string timeStamp = e.GetValue("TimeStamp").ToString(); timeStamp = DateTime.Parse(timeStamp).ToString("h:mm tt"); block.Message = e.GetValue("Message").ToString(); block.Sender = e.GetValue("Sender").ToString(); block.Date = timeStamp; block.MaximumSize = new Size(((InnerChatContainer.Width - 20) / 2) - 20, int.MaxValue); Invoke(new MethodInvoker(() => { block.FormatSize(); InnerChatContainer.Controls.Add(block); SelectedChat.LastMessage = e.GetValue("Message").ToString(); SelectedChat.TimeStamp = timeStamp; })); if ((DateTime.Now - lastNotification).TotalSeconds > 5 && block.Sender != CurrentUser.UserName && !Activated) { lastNotification = DateTime.Now; NotificationSound.Play(); } ResizeChat(); }
private void SendPicture() { if (Uploading) { MessageBox.Show("Please wait until the uploading is complete!"); return; } if (CurrentUser == null || RecipientUser == null) { return; } string message = ChatMessage.Text.Trim(); if (message == PreDefinedMessage["TypeRecipient"] + RecipientUser.UserName) { message = ""; } SocketMessage msg = new SocketMessage( new object[] { "ConversationID", ConversationID }, new object[] { "SenderID", CurrentUser.ID }, new object[] { "RecipientID", RecipientUser.ID }, new object[] { "Sender", CurrentUser.UserName }, new object[] { "Message", message }, new object[] { "TimeStamp", DateTime.UtcNow.ToString("MM-dd-yyyy HH:mm:ss") }, new object[] { "DataType", ChatType }, new object[] { "Links", pictureURLs } ); ClientSocket.Send("message", msg); ChatMessage.Text = PreDefinedMessage["TypeRecipient"] + RecipientUser.UserName; ChatMessage.SelectionLength = ChatMessage.SelectionStart = 0; ChatMessage.ForeColor = Color.FromArgb(150, 150, 150); ChatType = Reply.ContentType.String; pictureURLs = null; SendIcon.Image = Properties.Resources.send_disabled; foreach (var upPic in UploadContainer.Controls.OfType <UploadPictureBox>()) { upPic?.Dispose(); } UploadHiddenItems.Text = ""; UploadProgressPercent.Text = "0%"; UploadContainer.Hide(); ClientSocket.Send("typing", new SocketMessage( new object[] { "typing", false }, new object[] { "RecipientID", RecipientUser.ID } )); }
private void Send() { if (ChatType == Reply.ContentType.Picture) { SendPicture(); return; } if (CurrentUser == null || RecipientUser == null) { return; } string Message = ChatMessage.Text.Trim(); if (string.IsNullOrEmpty(Message)) { return; } if (Message == PreDefinedMessage["TypeRecipient"] + RecipientUser.UserName) { return; } SocketMessage msg = new SocketMessage( new object[] { "ConversationID", ConversationID }, new object[] { "SenderID", CurrentUser.ID }, new object[] { "RecipientID", RecipientUser.ID }, new object[] { "Sender", CurrentUser.UserName }, new object[] { "Message", ChatMessage.Text.Trim() }, new object[] { "TimeStamp", DateTime.UtcNow.ToString("MM-dd-yyyy HH:mm:ss") }, new object[] { "DataType", ChatType }, new object[] { "Links", new object[] { } } ); ClientSocket.Send("message", msg); ChatMessage.Text = PreDefinedMessage["TypeRecipient"] + RecipientUser.UserName; ChatMessage.SelectionLength = ChatMessage.SelectionStart = 0; ChatMessage.ForeColor = Color.FromArgb(150, 150, 150); ChatType = Reply.ContentType.String; SendIcon.Image = Properties.Resources.send_disabled; ClientSocket.Send("typing", new SocketMessage( new object[] { "typing", false }, new object[] { "RecipientID", RecipientUser.ID } )); }
private void ClientSocket_SocketEvent(Components.SocketEvent e) { switch (e.Name.ToLower()) { case "connected": if ((bool)e.Data) { ClientSocket.RegisterModule("messenger"); } break; case "moduleregistered": Invoke(new MethodInvoker(() => { LoginButton.Enabled = true; LoginStatus.Text = "Connection Successful!"; })); break; case "login": LoginResult(e.Data); break; case "friendslist": FriendsListResult(e.Data); break; case "conversations": ConversationsResult(e.Data); break; case "typing": SocketMessage msg = new SocketMessage(e.Data); if (RecipientUser != null) { RecipientUser.UserTyping = bool.Parse(msg.GetValue("typing").ToString()); } break; } }