public static void SendMsg(string content, MsgType msgType, MsgSendType sendType, string reciverid, string reciverName, IList <ImageWrapper> imageWrapperList = null) { try { MsgEntity chatContract = new MsgEntity(); chatContract.SenderID = Common.CurrentUser.ID; chatContract.SenderName = Common.CurrentUser.DisplayName; chatContract.Reciver = reciverid; chatContract.ReciverName = reciverName; chatContract.MsgContent = content; chatContract.SendTime = DateTime.Now; chatContract.ImageList = imageWrapperList; chatContract.MsgSendType = (int)sendType; chatContract.MsgType = (int)msgType; //从客户端 Common中获取相应连接 Connection p2pConnection = Common.GetUserConn(reciverid); if (p2pConnection != null) { p2pConnection.SendObject("ClientChatMessage", chatContract); } else { if (Common.TcpConn != null) { Common.TcpConn.SendObject("ChatMessage", chatContract); } } } catch (Exception) { throw; } }
//获取所有在线用户的信息 private void GetP2PInfo() { IList <UserIDEndPoint> userInfoList = Common.TcpConn.SendReceiveObject <IList <UserIDEndPoint> >("GetP2PInfo", "ResP2pInfo", 5000, "GetP2P"); foreach (UserIDEndPoint userInfo in userInfoList) { try { if (userInfo.UserID != Common.CurrentUser.ID) { ConnectionInfo connInfo = new ConnectionInfo(userInfo.IPAddress, userInfo.Port); Connection newTcpConnection = TCPConnection.GetConnection(connInfo); Common.AddUserConn(userInfo.UserID, newTcpConnection); SetUpP2PContract contract = new SetUpP2PContract(); contract.UserID = Common.CurrentUser.ID; //P2p通道打通后,发送一个消息给对方用户,以便于对方用户收到消息后,建立P2P通道 newTcpConnection.SendObject("SetupP2PMessage", contract); } } catch { } } }