private void BT_SendAddFriendMessage_Click(object sender, RoutedEventArgs e) { ContentControl bt = sender as ContentControl; long num = Convert.ToInt64(bt.Tag); SocketTool.GetClientUDP().SendToServer(TheMessageModel.MAKEFRIEND(UserSource.UloginNumber ?? 0, num, Model.MessageContent.MakeFriendType.SendStart)); }
private void BT_Save_Click(object sender, RoutedEventArgs e) { UserSource.Uname = TEXT_Name.Text; UserSource.Utag = TEXT_Tag.Text; UserSource.Upassword = TEXT_Pwd.Text; SocketTool.GetClientUDP().SendToServer(TheMessageModel.UPDATE_USER(0, UserSource, HeadImageBytes)); }
private void MENUITEM_Friends_Click(object sender, RoutedEventArgs e) { MenuItem item = (sender as MenuItem); User SelectedUser = item.DataContext as User; switch (item.Tag.ToString().ToUpper()) { case "DELETEFRIEND": SocketTool.GetClientUDP().SendToServer(TheMessageModel.MAKEFRIEND(UserSource.UloginNumber ?? 0, SelectedUser.UloginNumber ?? 0, MakeFriendType.Delete)) ; break; } }
public override void OnApplyTemplate() { SocketClient = SocketTool.GetClientUDP(); SendMessageCommand = new ActionCommand(() => { MessageContent_String MsgContent = new MessageContent_String(Text); MessageModel msg = new MessageModel(MessageType.String, UserFrom.UloginNumber ?? 0, UserTo.UloginNumber ?? 0, SocketTool.IPV4Address.ToString(), MsgContent); int MsgState = SocketClient.SendTo(msg, StaticResource.ServerIpaddress); SendedMessage?.Invoke(this, UserFrom, UserTo, msg, MsgState); Text = ""; }); SendImageCommand = new ActionCommand(() => { OpenFileDialog SelectDialog = new OpenFileDialog(); SelectDialog.Multiselect = true; SelectDialog.Filter = "*.jpg|*.png"; SelectDialog.ShowDialog(); String[] files = SelectDialog.FileNames; long FromNum = UserFrom.UloginNumber ?? 0; long ToNum = UserTo.UloginNumber ?? 0; foreach (String file in files) { ThreadPool.QueueUserWorkItem(call => { String FileName = ""; SocketTool.TCPSendFile(file, FromNum, ToNum, DataPacketType.ImageDataPacket, out FileName); MessageModel msg = TheMessageModel.IMAGE(FileName, FromNum, ToNum); int MsgState = SocketTool.GetClientUDP().SendToServer(msg); //将自己选择的文件位置作为图片发送出去 MessageContent_Image localImg = MessageModel.ToModel <MessageContent_Image>(msg.MessageContent); localImg.SetContent(file); msg.MessageContent = localImg; Dispatcher.Invoke(() => { SendedMessage?.Invoke(this, UserFrom, UserTo, msg, MsgState); }); Thread.Sleep(1000); }); } }); base.OnApplyTemplate(); }
public void OnInitMessageRouter() { SocketTool.GetClientUDP().GetNewMessage += ClientSocket_GetNewMessage_ChatForm; }
//------------------消息处理----------------- private void ClientSocket_GetNewMessage_ChatForm(MessageModel Msg, System.Net.EndPoint From) { if (Msg.MessageType == MessageType.String) { MessageContent_String c = MessageModel.ToModel <MessageContent_String>(Msg.MessageContent); Msg.MessageContent = c; PrintMessageRouter(); } else if (Msg.MessageType == MessageType.Get) { MessageContent_Get get = MessageModel.ToModel <MessageContent_Get>(Msg.MessageContent); if (get.RequestType == Get_Type.File) { if (get.RequestFileType == File_Type.HeadImagoe) { IoTool.CreateDirectory(StaticResource.UserHeadImageBasePath); String headPath = StaticResource.ToUserHeadImagePath(get.RequestArg + ".png"); IoTool.SaveFile(headPath, get.Content); Dispatcher.Invoke(() => { StaticResource.SetHeadImageSource(long.Parse(get.RequestArg.ToString()), ImageTool.GetUserHeadeImage(long.Parse(get.RequestArg.ToString()))); }); } } } else if (Msg.MessageType == MessageType.Login) { MessageContent_Login login = MessageModel.ToModel <MessageContent_Login>(Msg.MessageContent); if (login.LoginType == LoginType.OffOnLine) { MessageBox.Show(MessageStringMark.GetCodeMsg(login.WithMessage)); Dispatcher.Invoke(() => { OnCloseALL(); }); } } else if (Msg.MessageType == MessageType.DataPacket) { MessageContent_DataPacket packet = MessageModel.ToModel <MessageContent_DataPacket>(Msg.MessageContent); //如果是图片的数据 if (packet.PacketType == DataPacketType.ImageDataPacket) { String fname = packet.FileName; //如果是第一个包 if (packet.PacketIndex == 1) { DataPacketsFile.Add(fname, new WritingFile(StaticResource.MessageImagePath + fname) { Tag = packet.FileToken//这条实在没什么用 }); } DataPacketsFile[fname].Writer(packet.Content); if (packet.IsLastPacket) { DataPacketsFile[fname].WriterEnd(); } } } else if (Msg.MessageType == MessageType.Image) { MessageContent_Image img = MessageModel.ToModel <MessageContent_Image>(Msg.MessageContent); Msg.MessageContent = img; PrintMessageRouter(); } else if (Msg.MessageType == MessageType.MakeFriend) { ChatHotContext DB = new ChatHotContext(); MessageContent_MakeFriend content = MessageModel.ToModel <MessageContent_MakeFriend>(Msg.MessageContent); Msg.MessageContent = content; if (content.MakeFriendType == MakeFriendType.SendStart) { if (MessageBox.Show("好友请求", "来自" + content, MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes) { //这个是返回消息所以是反着来的 //SocketTool.GetClientUDP().SendToServer(TheMessageModel.MAKEFRIEND(content.ToNum, content.FromNum, content.FromNum, content.ToNum, MakeFriendType.Success)); Msg.FromIpaddress = StaticResource.IPV4Address.ToString(); Msg.FromLoginNumber = content.ToNum; Msg.ToLoginNumber = content.FromNum; content.MakeFriendType = MakeFriendType.Success; Msg.MessageContent = content; SocketTool.GetClientUDP().SendToServer(Msg); Dispatcher.Invoke(() => { UserListForm.MainUserListForm.AddFriendsItem(DB.Users.Where(u => u.UloginNumber == content.FromNum).First()); }); } } else if (content.MakeFriendType == MakeFriendType.Success) { MessageBox.Show("对方同意"); Dispatcher.Invoke(() => { UserListForm.MainUserListForm.AddFriendsItem(DB.Users.Where(u => u.UloginNumber == content.ToNum).First()); }); } else if (content.MakeFriendType == MakeFriendType.Fail) { MessageBox.Show("对方拒绝"); } else if (content.MakeFriendType == MakeFriendType.DeleteSuccess) { Dispatcher.Invoke(() => { UserListForm.MainUserListForm.RemoveFriendsItem(UserSource.UloginNumber == content.FromNum ? content.ToNum : content.FromNum); }); } DB.Dispose(); return; } else if (Msg.MessageType == MessageType.UpdateUser) { MessageContent_UpdateUser content = MessageModel.ToModel <MessageContent_UpdateUser>(Msg.MessageContent); StaticResource.ALLUserSource = content.GetContent(); Dispatcher.Invoke(() => { UserInfoChanged(content.GetContent()); }); //PrintMessageRouter(content); } //如果是需要提醒用户的消息就调用一下这个方法 void PrintMessageRouter(Object content = null) { if (content != null) { Msg.MessageContent = content; } //如果存在打开的对应登录号的聊天窗体 if (MainChatForm != null && HasChatUser(Msg.FromLoginNumber)) { Dispatcher.Invoke(() => { User UserFrom = GetUserByChatUsersItem(Msg.FromLoginNumber); PrintMessage(Msg, UserFrom, UserSource); }); } //如果不存在 else { //MessageBox.Show("有新的消息--->>" + Msg.FromLoginNumber); Dispatcher.Invoke(() => { UserListForm.MainUserListForm.AppendToMessageQueue(Msg); }); } } }
/// <param name="From">当前客户端的用户</param> /// <param name="To">与当前客户端聊天的用户</param> public ChatTextBox(User From, User To) { SocketClient = SocketTool.GetClientUDP(); UserFrom = From; UserTo = To; }