예제 #1
0
        //void PreProceed(IMessage msg, string minfo)
        //{

        //}

        //void PostProceed(IMessage msg, string minfo)
        //{

        //}
        #endregion

        #region 事件监听
        /// <summary>
        /// 客户端掉线事件
        /// </summary>
        /// <param name="remoteEndPoint"></param>
        private void ClientOffline(string remoteEndPoint)
        {
            Terminal.ServerPrint(InfoType.信息, "客户端<" + remoteEndPoint + ">断开了连接。服务器释放通信连接");
            string id = "";

            if (runningData.Clients_remoteEndPoint.ContainsKey(remoteEndPoint))
            {
                id = runningData.Clients_remoteEndPoint[remoteEndPoint].UserId;
                //从在的答疑室中清除
                foreach (string roomID in ((User_Server)runningData.Clients_remoteEndPoint[remoteEndPoint]).RoomIDs)
                {
                    runningData.ChatRoom_id[roomID].memberList.Remove(runningData.Clients_remoteEndPoint[remoteEndPoint]);
                    if (runningData.ChatRoom_id.ContainsKey(roomID))
                    {
                        foreach (UserInfo user in runningData.ChatRoom_id[roomID].memberList)
                        {
                            ServerSocket.Send(runningData.Clients_id[user.UserId], MessageTranslate.EncapsulationInfo(MessageContent.某人退出答疑室, MessageType.通知, roomID, id));
                        }
                    }
                }
                runningData.Clients_remoteEndPoint.Remove(remoteEndPoint);
            }
            if (id != null)
            {
                if (runningData.Clients_id.ContainsKey(id))
                {
                    runningData.Clients_id.Remove(id);
                }
            }
        }
예제 #2
0
        static void test1()
        {
            Console.Write("PORT:");
            int port = int.Parse(Console.ReadLine());

            Console.WriteLine(ClientC.Access("127.0.0.1", 9840, port).Info);
            ClientC.Receive(ar => { Console.WriteLine(ar); });
            while (true)
            {
                string cmd = Console.ReadLine();
                if (cmd == "shutdown")
                {
                    break;
                }
                else if (cmd == "login")
                {
                    ClientC.Send(MessageTranslate.EncapsulationInfo(MessageContent.登录, MessageType.请求, "1501", "123"));
                }
                else if (cmd == "login1")
                {
                    ClientC.Send(MessageTranslate.EncapsulationInfo(MessageContent.登录, MessageType.请求, "0000", "123456"));
                }
                else
                {
                    ClientC.Send(cmd);
                }
            }
        }
예제 #3
0
        private IResult ExitRoomInform(IReceiveInfo receiveInfo, UserInfo userInfo)
        {
            string roomID = receiveInfo.Message["房间名"];

            Terminal.ClientPrint(receiveInfo.RemoteEndPoint, InfoType.信息, userInfo.UserId + "主动离开答疑室=>房间号:" + roomID);
            IResult result;

            if (!runningData.ChatRoom_id.ContainsKey(roomID))//房间存在判定
            {
                result = new Result(baseResult.Faild, "该答疑室未被创建");
            }
            else if (!runningData.ChatRoom_id[roomID].memberList.Contains(userInfo))
            {
                result = new Result(baseResult.Faild, "用户不在答疑室");
            }
            else
            {
                runningData.ChatRoom_id[roomID].memberList.Remove(userInfo);
                ((User_Server)userInfo).RoomIDs.Remove(roomID);
                if (runningData.ChatRoom_id.ContainsKey(roomID))
                {
                    foreach (UserInfo user in runningData.ChatRoom_id[roomID].memberList)
                    {
                        ServerSocket.Send(runningData.Clients_id[user.UserId], MessageTranslate.EncapsulationInfo(MessageContent.某人退出答疑室, MessageType.通知, roomID, userInfo.UserId));
                    }
                }
                result = new Result(baseResult.Successful);
            }
            return(result);
        }
예제 #4
0
        private IResult MuteUserInform(IReceiveInfo receiveInfo, UserInfo userInfo)
        {
            string  roomID = receiveInfo.Message["房间名"];
            string  userID = receiveInfo.Message["学号"];
            bool    isMute = bool.Parse(receiveInfo.Message["是否静音"]);
            IResult result;

            if (!runningData.ChatRoom_id.ContainsKey(roomID))//房间存在判定
            {
                result = new Result(baseResult.Faild, "该答疑室未被创建");
            }
            else if (!runningData.ChatRoom_id[roomID].memberList.Contains(runningData.Clients_remoteEndPoint[runningData.Clients_id[userID]]))
            {
                result = new Result(baseResult.Faild, "用户不在答疑室");
            }
            else
            {
                if (isMute)
                {
                    Terminal.ClientPrint(receiveInfo.RemoteEndPoint, InfoType.信息, "禁止" + userID + "发言 答疑室=>房间号:" + roomID);
                }
                else
                {
                    Terminal.ClientPrint(receiveInfo.RemoteEndPoint, InfoType.信息, "允许" + userID + "发言 答疑室=>房间号:" + roomID);
                }
                ServerSocket.Send(runningData.Clients_id[userID], MessageTranslate.EncapsulationInfo(MessageContent.静音自己, MessageType.通知, roomID, isMute.ToString()));
                result = new Result(baseResult.Successful);
            }
            return(result);
        }
 public IResult Login(string id, string password)
 {
     lock (LOCK)
     {
         this.UserInfo = new User_Client(id);
         if (waitingRespond)
         {
             throw new Exception("产生过未上锁的异步请求");
         }
         this.waitingRespond = true;
         isSendSucceful      = true;
         clientSocket.Send(MessageTranslate.EncapsulationInfo(MessageContent.登录, MessageType.请求, id, password), ar => { if (ar == baseResult.Faild.ToString())
                                                                                                                        {
                                                                                                                            isSendSucceful = false;
                                                                                                                        }
                           });
         while (waitingRespond)
         {
             if (!isSendSucceful)
             {
                 waitingRespond = false; return(new Result(baseResult.Faild, "发送send失败"));
             }
         }
         //等待响应
         if (respondMessage["结果"] == baseResult.Successful.ToString())
         {
             this.UserInfo.UserState = UserState.已登录;
             UserInfo.IsPrerogative  = Boolean.Parse(respondMessage["权限"]);
             return(new Result(baseResult.Successful, respondMessage["权限"]));
         }
         return(new Result(baseResult.Faild, respondMessage["权限"]));
     }
 }
        public IResult JoinNCRoom(string roomId, string password = "")
        {
            lock (LOCK)
            {
                if (waitingRespond)
                {
                    throw new Exception("产生过未上锁的异步请求");
                }
                this.waitingRespond = true;
                isSendSucceful      = true;
                clientSocket.Send(MessageTranslate.EncapsulationInfo(MessageContent.加入答疑室, MessageType.请求, roomId, password), ar => { if (ar == baseResult.Faild.ToString())
                                                                                                                                      {
                                                                                                                                          isSendSucceful = false;
                                                                                                                                      }
                                  });

                while (waitingRespond)
                {
                    if (!isSendSucceful)
                    {
                        waitingRespond = false; return(new Result(baseResult.Faild, "发送send失败"));
                    }
                }
                //等待响应
                if (respondMessage["结果"] == baseResult.Successful.ToString())
                {
                    builder = respondMessage["创建者"];
                    callOMCS.JoinRoom(roomId, builder);
                    UserInfo.RoomID = roomId;
                    return(new Result(baseResult.Successful, respondMessage["描述"]));
                }
                return(new Result(baseResult.Faild, respondMessage["描述"]));
            }
        }
        private IResult SpeakInform(IReceiveInfo receiveInfo, UserInfo userInfo)
        {
            string roomID = receiveInfo.Message["房间名"];

            Terminal.ClientPrint(receiveInfo.RemoteEndPoint, InfoType.请求, "发言请求=>" + receiveInfo.Message["学号"] + "房间号:" + roomID);
            IResult result;

            if (!runningData.ChatRoom_id.ContainsKey(roomID))//房间存在判定
            {
                result = new Result(baseResult.Faild, "该答疑室未被创建");
            }
            else if (!runningData.ChatRoom_id[roomID].memberList.Contains(userInfo))
            {
                result = new Result(baseResult.Faild, "用户不在答疑室");
            }
            else
            {
                User_Server builder = (User_Server)runningData.ChatRoom_id[roomID].Builder;
                if (builder.RoomIDs.Contains(roomID))
                {
                    ServerSocket.Send(runningData.Clients_id[builder.UserId], MessageTranslate.EncapsulationInfo(MessageContent.发言请求, MessageType.通知, roomID, userInfo.UserId));
                    result = new Result(baseResult.Successful);
                }
                else
                {
                    result = new Result(baseResult.Faild, "答疑室已无老师");
                }
            }
            if (result.BaseResult == baseResult.Faild)
            {
                Terminal.ServerPrint(InfoType.响应, "<" + receiveInfo.RemoteEndPoint + ">:" + "失败 描述:" + result.Info);
            }
            return(result);
        }
예제 #8
0
 public IResult MuteUser(string roomid, string guestId)
 {
     if (UserInfo.IsPrerogative == false)
     {
         return(new Result(baseResult.Faild, "没有权限"));
     }
     clientSocket.Send(MessageTranslate.EncapsulationInfo(MessageContent.静音某人, MessageType.通知, roomid, guestId));
     return(new Result(baseResult.Successful));
 }
예제 #9
0
        private IResult LoginRequest(IReceiveInfo receiveInfo, UserInfo userInfo)
        {
            Terminal.ClientPrint(receiveInfo.RemoteEndPoint, InfoType.请求, "登录客户端=>学号:" + receiveInfo.Message["学号"]);
            //之前的ID;
            string password   = receiveInfo.Message["密码"];
            string lastUserId = userInfo.UserId == null ? "" : userInfo.UserId;

            userInfo.UserId = receiveInfo.Message["学号"];
            IResult result;

            //判断是否已登录
            if (runningData.Clients_id.ContainsKey(userInfo.UserId))
            {
                result = new Result(baseResult.Faild, "该用户已登录");
            }
            else
            {
                Terminal.ServerPrint(InfoType.信息, "访问数据库中......");
                //验证结果
                if (ServerCallDatabase.CheckUserInfo(userInfo, password).BaseResult == baseResult.Successful)
                {
                    //是否权限用户
                    bool IsPrerogative = ServerCallDatabase.IsPrerogative(userInfo);
                    userInfo.IsPrerogative = IsPrerogative;
                    //已登录了 移除登陆
                    if (runningData.Clients_id.ContainsKey(lastUserId))
                    {
                        runningData.Clients_id.Remove(lastUserId);
                    }
                    runningData.Clients_id[userInfo.UserId] = receiveInfo.RemoteEndPoint;
                    result = new Result(baseResult.Successful, IsPrerogative.ToString());
                }
                else
                {
                    result = new Result(baseResult.Faild, "错误的账号或密码");
                }
            }

            if (result.BaseResult == baseResult.Successful)
            {
                Terminal.ServerPrint(InfoType.响应, "<" + receiveInfo.RemoteEndPoint + ">:" + "成功登录 权限:" + result.Info);
            }
            else
            {
                Terminal.ServerPrint(InfoType.响应, "<" + receiveInfo.RemoteEndPoint + ">:" + "登录失败 原因:" + result.Info);
            }
            ServerSocket.Send(receiveInfo.RemoteEndPoint, MessageTranslate.EncapsulationInfo(MessageContent.登录, MessageType.响应, result.BaseResult.ToString(), result.Info));
            return(result);
        }
 public IResult ExitNCRoom(string roomId)
 {
     clientSocket.Send(MessageTranslate.EncapsulationInfo(MessageContent.退出答疑室, MessageType.通知, roomId));
     UserInfo.RoomID = null;
     try
     {
         callOMCS.ExitRoom();
     }
     catch (Exception e)
     {
         OnException?.Invoke(e);
         return(new Result(baseResult.Faild, e.Message));
     }
     return(new Result(baseResult.Successful));
 }
예제 #11
0
 public IResult JoinNCRoom(string roomId, string password = "")
 {
     lock (LOCK)
     {
         if (waitingRespond)
         {
             throw new Exception("产生过未上锁的异步请求");
         }
         this.waitingRespond = true;
         clientSocket.Send(MessageTranslate.EncapsulationInfo(MessageContent.加入答疑室, MessageType.请求, roomId, password));
         while (waitingRespond)
         {
         }                         //等待响应
         if (respondMessage["结果"] == baseResult.Successful.ToString())
         {
             callOMCS.JoinRoom(roomId, respondMessage["创建者"]);
             return(new Result(baseResult.Successful, respondMessage["描述"]));
         }
         return(new Result(baseResult.Faild, respondMessage["描述"]));
     }
 }
예제 #12
0
        private IResult JoinRoomRequest(IReceiveInfo receiveInfo, UserInfo userInfo)
        {
            string roomID   = receiveInfo.Message["房间名"];
            string password = receiveInfo.Message["密码"];

            Terminal.ClientPrint(receiveInfo.RemoteEndPoint, InfoType.请求, "加入答疑室=>房间号:" + roomID);
            IResult result;

            if (!runningData.ChatRoom_id.ContainsKey(roomID))//房间存在判定
            {
                result = new Result(baseResult.Faild, "该答疑室未被创建");
            }
            else if (runningData.ChatRoom_id[roomID].memberList.Contains(userInfo))
            {
                result = new Result(baseResult.Faild, "用户已在答疑室");
            }
            else if (runningData.ChatRoom_id[roomID].Password != password)
            {
                result = new Result(baseResult.Faild, "密码错误");
            }
            else//成功加入
            {
                runningData.ChatRoom_id[roomID].memberList.Add(userInfo);
                ((User_Server)userInfo).RoomIDs.Add(roomID);
                result = new Result(baseResult.Successful, "房间号" + roomID);
            }
            if (result.BaseResult == baseResult.Faild)
            {
                Terminal.ServerPrint(InfoType.响应, "<" + receiveInfo.RemoteEndPoint + ">:" + "加入失败 描述:" + result.Info);
                ServerSocket.Send(receiveInfo.RemoteEndPoint, MessageTranslate.EncapsulationInfo(MessageContent.加入答疑室, MessageType.响应, result.BaseResult.ToString(), result.Info, "error"));
            }
            else
            {
                Terminal.ServerPrint(InfoType.响应, "<" + receiveInfo.RemoteEndPoint + ">:" + "加入成功 描述:" + result.Info);
                ServerSocket.Send(receiveInfo.RemoteEndPoint, MessageTranslate.EncapsulationInfo(MessageContent.加入答疑室, MessageType.响应, result.BaseResult.ToString(), result.Info, runningData.ChatRoom_id[roomID].Builder.UserId));
            }
            return(result);
        }
예제 #13
0
        //[AOPMethod("创建答疑室请求", PreProceed, PostProceed)]
        private IResult CreateRoomRequest(IReceiveInfo receiveInfo, UserInfo userInfo)
        {
            string roomID   = receiveInfo.Message["房间名"];
            string password = receiveInfo.Message["密码"];

            Terminal.ClientPrint(receiveInfo.RemoteEndPoint, InfoType.请求, "创建答疑室=>房间号:" + roomID);
            IResult result;

            if (!runningData.Clients_remoteEndPoint[receiveInfo.RemoteEndPoint].IsPrerogative)//权限判定
            {
                result = new Result(baseResult.Faild, "用户没有创建权限");
            }
            else if (runningData.ChatRoom_id.ContainsKey(roomID))//房间存在判定
            {
                result = new Result(baseResult.Faild, "该答疑室已存在");
            }
            else//成功创建
            {
                ChatRoom chatroom = new ChatRoom(password);
                chatroom.RoomEmpty += RoomEmpty;
                chatroom.Builder    = userInfo;
                chatroom.GroupName  = roomID;
                chatroom.memberList.Add(userInfo);
                runningData.ChatRoom_id[roomID] = chatroom;
                ((User_Server)userInfo).RoomIDs.Add(roomID);
                result = new Result(baseResult.Successful, "房间号" + roomID);
            }
            if (result.BaseResult == baseResult.Faild)
            {
                Terminal.ServerPrint(InfoType.响应, "<" + receiveInfo.RemoteEndPoint + ">:" + "创建失败 描述:" + result.Info);
            }
            else
            {
                Terminal.ServerPrint(InfoType.响应, "<" + receiveInfo.RemoteEndPoint + ">:" + "创建成功 描述:" + result.Info);
            }
            ServerSocket.Send(receiveInfo.RemoteEndPoint, MessageTranslate.EncapsulationInfo(MessageContent.创建答疑室, MessageType.响应, result.BaseResult.ToString(), result.Info));
            return(result);
        }
예제 #14
0
 public IResult ExitNCRoom(string roomId)
 {
     clientSocket.Send(MessageTranslate.EncapsulationInfo(MessageContent.退出答疑室, MessageType.通知, roomId));
     callOMCS.ExitRoom();
     return(new Result(baseResult.Successful));
 }
 public void SpeakInform(string roomId)
 {
     clientSocket.Send(MessageTranslate.EncapsulationInfo(MessageContent.发言请求, MessageType.通知, roomId, UserInfo.UserId));
 }