private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { try { if (!this.toExit) { if ((e.CloseReason == CloseReason.ApplicationExitCall) || (e.CloseReason == CloseReason.WindowsShutDown)) { NetworkComms.Shutdown(); this.toExit = true; this.Dispose(); this.Close(); } else { this.Visible = false; e.Cancel = true; } } } catch (Exception ex) { LogTools.LogException(ex, "MainForm_FormClosing"); } }
// 某客户端用户的状态改变后,通知其他用户 private void UserStateNotify(string userID, OnlineState ustate) { try { //用户状态契约类 UserStateContract userState = new UserStateContract(); userState.UserID = userID; userState.OnLine = ustate; IList <ShortGuid> allUserID; lock (syncLocker) { //获取所有用户字典中的用户链接标识 allUserID = new List <ShortGuid>(userManager.Values); } //给所有用户发送某用户的在线状态 foreach (ShortGuid netID in allUserID) { List <Connection> result = NetworkComms.GetExistingConnection(netID, ConnectionType.TCP); if (result.Count > 0 && result[0].ConnectionInfo.NetworkIdentifier == netID) { result[0].SendObject("UserStateNotify", userState); } } } catch (Exception ex) { LogTools.LogException(ex, "MainForm.UserStateNotify"); } }
private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e) { try { GotoExit(); } catch (Exception ex) { LogTools.LogException(ex, "退出系统ToolStripMenuItem_Click"); } }
//获取所有用户组 private void IncomingGetGrooup(PacketHeader header, Connection Connection, string param) { try { //IList<UserGroup> lstGroup = DoUserGroup.GetAll(); //Connection.SendObject("ResGroup", lstGroup); } catch (Exception ex) { LogTools.LogException(ex, "IncomingGetGrooup"); } }
/// <summary> /// 客户端获取某用户的好友列表 /// </summary> /// <param name="header"></param> /// <param name="Connection"></param> /// <param name="userID"></param> private void IncomingGetFriends(PacketHeader header, Connection Connection, string userID) { try { List <IMUserInfo> myfriends = GetMyFriends(userID); UserListContract ulc = new UserListContract(myfriends); Connection.SendObject("ResGetFriends", ulc); } catch (Exception ex) { LogTools.LogException(ex, "IncomingGetFriends"); } }
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { try { this.Show(); if (this.WindowState == FormWindowState.Minimized) { this.WindowState = FormWindowState.Maximized; } this.Activate(); } catch (Exception ex) { LogTools.LogException(ex, "notifyIcon1_MouseDoubleClick"); } }
private void 显示系统ToolStripMenuItem_Click(object sender, EventArgs e) { try { this.Show(); if (this.WindowState == FormWindowState.Minimized) { this.WindowState = FormWindowState.Maximized; } this.Activate(); } catch (Exception ex) { LogTools.LogException(ex, "显示系统ToolStripMenuItem_Click"); } }
private void button5_Click(object sender, EventArgs e) { try { lock (syncLocker) { LogInfo.LogMessage(DateTime.Now.ToString(), "查看连接信息"); foreach (Connection conn in NetworkComms.GetExistingConnection()) { LogInfo.LogMessage("本地端点" + conn.ConnectionInfo.LocalEndPoint + "远程端点" + conn.ConnectionInfo.RemoteEndPoint, "查看连接信息"); } } } catch (Exception ex) { LogTools.LogException(ex, "button5_Click"); } }
private void button8_Click(object sender, EventArgs e) { try { LogInfo.LogMessage(DateTime.Now.ToString(), "查看在线用户"); lock (syncLocker) { foreach (KeyValuePair <string, ShortGuid> userInfo in userManager) { LogInfo.LogMessage(userInfo.Key, "查看在线用户"); } } } catch (Exception ex) { LogTools.LogException(ex, "button8_Click"); } }
private void GotoExit() { try { if (MessageBox.Show("您确认要退出吗?", "", MessageBoxButtons.OKCancel) == DialogResult.OK) { this.toExit = true; this.Dispose(); this.Close(); } } catch (Exception ex) { LogTools.LogException(ex, "GotoExit"); } }
/// <summary> /// 显示日志信息到窗口 /// </summary> /// <param name="logLine"></param> private void AddLineToLog(string logLine) { try { if (this.InvokeRequired) { object[] args = { logLine }; this.Invoke(new Action <string>(this.AddLineToLog), args); } else { txtLog.Text += "-" + DateTime.Now.ToShortTimeString() + " - " + logLine + Environment.NewLine; } } catch (Exception ex) { LogTools.LogException(ex, "AddLineToLog"); } }
//处理某可客户端离线情况 private void HandleConnectionClosed(Connection Connection) { try { var tempUserID = ""; lock (syncLocker) { //在用户字典中找到网络连接相对应的用户ID foreach (var kv in userManager) { //如果要关闭的网络连接,与某个用户的网络连接相同,则找出此用户 if (kv.Value == Connection.ConnectionInfo.NetworkIdentifier) { tempUserID = kv.Key; break; } } if (tempUserID != "") { //如果找到的用户ID不为空,则从用户字典中删除此项 if (userManager.ContainsKey(tempUserID)) { //连接关闭时,从用户管理器中删除该用户 userManager.Remove(tempUserID); } } } //发送通知给其他客户端,告知其某个用户下线 if (tempUserID != "") { UserStateNotify(tempUserID, OnlineState.Offline); } //应该发送一个消息给所有在线的其他用户 } catch (Exception ex) { LogTools.LogException(ex, "NetworkComms_ConnectionClosed"); } }
// 获取离线消息,稍后实现 private void IncomingMyOffLineMsg(PacketHeader header, Connection Connection, UserIDContract userInfo) { try { MsgSendOrderClient bll = new MsgSendOrderClient(); IList <MsgEntity> msgList = bll.GetAllOffLineMsgByUserID(userInfo.UsersID); foreach (MsgEntity message in msgList) { Connection.SendObject("ServerChatMessage", message); bll.DeleteOffLineMsg(message.ID); } } catch (Exception ex) { LogTools.LogException(ex, "IncomingMyOffLineMsg"); } }
//获取所有在线用户 private void IncomingGetOnlineUser(PacketHeader header, Connection Connection, string getUserID) { try { //此方法中对于客户端传来的参数getUserID并没有使用,内部通讯录,好友都是共同的。 //为了简便直接把用户字典中所有用户ID发给客户端 IList <string> onLineUser; lock (syncLocker) { onLineUser = new List <string>(userManager.Keys); } UserIDListContract usersID = new UserIDListContract(onLineUser); Connection.SendObject("ResOnlineUser", usersID); } catch (Exception ex) { LogTools.LogException(ex, "IncomingGetOnlineUser"); } }
//修改用户密码 private void IncomingChangePsw(PacketHeader header, Connection Connection, UserPswEntity contract) { try { //在数据库中修改用户密码 int resMessage = new UserInfoClient().ResetPwd(contract.UserID, contract.NewPsw, contract.OldPsw); string msg = "密码修改成功"; if (resMessage == -2) { msg = "旧密码不正确"; } else if (resMessage == -1) { msg = "修改失败"; } Connection.SendObject("ResChangePsw", msg); } catch (Exception ex) { LogTools.LogException(ex, "IncomingChangePsw"); } }
private void button4_Click(object sender, EventArgs e) { try { lock (syncLocker) { LogInfo.LogMessage(DateTime.Now.ToString(), "查看连接数"); foreach (Connection conn in NetworkComms.GetExistingConnection()) { //If true the remote end of the Connection responded to an alive/ping test if (conn.ConnectionAlive()) { LogInfo.LogMessage(conn.ConnectionInfo.RemoteEndPoint.ToString(), "查看连接数"); } } } } catch (Exception ex) { LogTools.LogException(ex, "button4_Click"); } }
/// <summary> /// 当有消息传递过来时调用 /// </summary> /// <param name="header"></param> /// <param name="Connection"></param> /// <param name="chatContract"></param> private void IncomingChatMessage(PacketHeader header, Connection Connection, MsgEntity chatContract) { try { lock (syncLocker) { bool isAddOffline = false; if (chatContract.IsWebMsg) { foreach (Connection conn in NetworkComms.GetExistingConnection(userManager["SB_WEB_INFO"], ConnectionType.TCP)) { conn.SendObject("ServerChatMessage", chatContract); } return; } if (string.IsNullOrWhiteSpace(chatContract.Reciver)) { //转发给所有的用户 List <IMUserInfo> myfriends = GetMyFriends(chatContract.SenderID); foreach (var item in myfriends) { //如果用户在线,转发消息 if (userManager.ContainsKey(item.ID)) { //userManager[chatContract.DestUserID].SendObject("ServerChatMessage", chatContract); //应该只有一个返回的连接,但是由于返回的是列表,遍历一下也可 foreach (Connection conn in NetworkComms.GetExistingConnection(userManager[item.ID], ConnectionType.TCP)) { conn.SendObject("ServerChatMessage", chatContract); } } //如果用户不在线,把数据加入到数据库中 //暂时不保存离线图片消息 else { if (!isAddOffline) { isAddOffline = true; new MsgSendOrderClient().AddOfflineMsg(chatContract); } } } } else { List <string> recivers = new List <string>(); if (chatContract.Reciver.Contains(";")) { recivers.AddRange(chatContract.Reciver.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)); } else { recivers.Add(chatContract.Reciver); } foreach (var item in recivers) { //如果用户在线,转发消息 if (userManager.ContainsKey(item)) { //userManager[chatContract.DestUserID].SendObject("ServerChatMessage", chatContract); //应该只有一个返回的连接,但是由于返回的是列表,遍历一下也可 foreach (Connection conn in NetworkComms.GetExistingConnection(userManager[item], ConnectionType.TCP)) { conn.SendObject("ServerChatMessage", chatContract); } } //如果用户不在线,把数据加入到数据库中 //暂时不保存离线图片消息 else { if (!isAddOffline) { isAddOffline = true; new MsgSendOrderClient().AddOfflineMsg(chatContract); } } } } } } catch (Exception ex) { LogTools.LogException(ex, "IncomingChatMessage"); } }
//处理用户登录 private void IncomingLoginHandler(PacketHeader header, Connection Connection, IMUserInfo loginInfo) { try { //从数据库中验证登录信息 string ip = Connection.ConnectionInfo.LocalEndPoint.ToString(); string port = Connection.ConnectionInfo.LocalEndPoint.Port.ToString(); string error = string.Empty; View_IMUser userinfo = null; if (loginInfo.IsWebMsg) { userinfo = new View_IMUser(); userinfo.ID = loginInfo.Code; userinfo.Code = loginInfo.Code; userinfo.Name = "网页用户"; } else { userinfo = new AuthorityClient.LoginClient().Login(loginInfo.Code, loginInfo.Pwd, ip, port, out error); } if (userinfo == null) { UserLoginContract resContract = new UserLoginContract(error, null); Connection.SendObject("ResUserLogin", resContract); } else { UserLoginContract resContract = new UserLoginContract(error, userinfo.Clone()); Connection.SendObject("ResUserLogin", resContract); if (string.IsNullOrWhiteSpace(error)) { lock (syncLocker) { //同一账号登陆,先退出已经登陆的客户端 if (userManager.ContainsKey(userinfo.ID)) { //通知相应的连接,关闭此连接 foreach (Connection conn in NetworkComms.GetExistingConnection(userManager[userinfo.ID], ConnectionType.TCP)) { conn.SendObject("CloseConnection", "msg"); } userManager.Remove(userinfo.ID); } //注册新的用户 if (!userManager.ContainsKey(userinfo.ID)) { userManager.Add(userinfo.ID, Connection.ConnectionInfo.NetworkIdentifier); } } //用户上线后,通知其他用户 UserStateNotify(userinfo.ID, OnlineState.Online); } } } catch (Exception ex) { LogTools.LogException(ex, "IncomingLoginHandler"); } }