Exemplo n.º 1
0
 public static void ClientMessage(string connectionId, string message)
 {
     string[] values = message.Split("-");
     switch (values[0])
     {
     case "jinfangjian":
         UserSocket userSocket = null;
         clientPool.TryGetValue(connectionId, out userSocket);
         User user = new User()
         {
             Name       = connectionId,
             UserSocket = userSocket
         };
         int chatId = int.Parse(values[1]);
         // N 号房间 添加用户
         for (int i = 0; i < Global.chatRooms.Count; i++)
         {
             if (Global.chatRooms[i].Id == chatId)
             {
                 Global.chatRooms[i].AddUser(user, out string outMesg);
                 //发送信息
                 ChatHandler.Send(outMesg, userSocket.WebSocket);
                 break;
             }
         }
         break;
     }
 }
Exemplo n.º 2
0
 public NonBlockingUdpBroadcastImpl() : base()
 {
     UserSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
     UserSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
     UserSocket.ExclusiveAddressUse = false;
     UserSocket.Blocking            = false;
 }
 public NonBlockingUdpMulticastImpl(EndPoint multicastAddress, EndPoint localAddress, bool loopback, int ttl) : base()
 {
     timeToLive            = ttl;
     multicastLoopback     = loopback;
     this.localAddress     = localAddress;
     this.multicastAddress = multicastAddress;
     UserSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
     UserSocket.ExclusiveAddressUse = false;
     UserSocket.Blocking            = false;
 }
Exemplo n.º 4
0
        public static void ClientConnection(string connectionId, WebSocket webSocket)
        {
            //添加用户
            UserSocket userSocket = new UserSocket
            {
                WebSocket             = webSocket,
                WebSocketConnectionId = connectionId
            };

            clientPool.TryAdd(connectionId, userSocket);

            ChatHandler.Send("连接成功", webSocket);
        }
Exemplo n.º 5
0
        public HashSet <string> GetConnection(string username)
        {
            //!!!!!!!the must fix
            UserSocket Con = new UserSocket();

            if (Users.TryGetValue(username, out Con) == false)
            {
                return(null);
            }
            else
            {
                return(Con.ConnectionIds);
            }
            //return Users.GetValueOrDefault(username).ConnectionIds;
        }
Exemplo n.º 6
0
        private void Connector_LogoutEvent(object sender, User e)
        {
            UserSocket userSocket = GetUserSocket(e.UserID);

            if (userSocket == null)
            {
                ShowMessage("要注销的用户不存在");
                return;
            }
            LoginedUserList.Remove(userSocket);
            UserQuitEvent?.Invoke(this, userSocket.ToUser());
            foreach (UserSocket u in LoginedUserList)
            {
                connector.SendUserChange(u, e, CommandType.UserQuit);
            }
        }
Exemplo n.º 7
0
 public void Close()
 {
     try
     {
         writeQueue.Clear();
         cache.Clear();
         m_bIsReading = false;
         m_bIsWriting = false;
         UserSocket.Shutdown(SocketShutdown.Both);
         UserSocket.Close();
         UserSocket = null;
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Exemplo n.º 8
0
 public bool SendLoginResult(UserSocket userSocket, bool result)
 {
     try
     {
         MessageDictionary messageD = new MessageDictionary();
         messageD.Add(MesKeyStr.CommandType, CommandType.LoginResult.ToString());
         messageD.Add(MesKeyStr.LoginResult, result.ToString());
         messageD.Add(MesKeyStr.UserID, userSocket.UserID);
         messageD.Add(MesKeyStr.NickName, userSocket.NickName);
         Send(userSocket.Socket, messageD.ToString());
         return(true);
     }
     catch (Exception e)
     {
         ShowMessage("发送登录结果失败" + e.Message + "\n" + e.StackTrace + "\n");
         return(false);
     }
 }
Exemplo n.º 9
0
 private void Connector_PrivateMessageEvent(object sender, MessageDictionary e)
 {
     for (int i = 0; i < LoginedUserList.Count; i++)
     {
         if (LoginedUserList[i].UserID.Equals(e[MesKeyStr.TargetUserID]))
         {
             UserSocket user = GetUserSocket(e[MesKeyStr.UserID]);
             string     ip   = ((IPEndPoint)user.Socket.RemoteEndPoint).Address.ToString();
             e.Add(MesKeyStr.NickName, user.NickName);
             e.Add(MesKeyStr.IP, ip);
             e.Add(MesKeyStr.DateTime, DateTime.Now.ToString());
             MessageArrivedEvent?.Invoke(this, e);
             e.Add(MesKeyStr.Sender, Sender.self.ToString());
             connector.SendMessage(user.Socket, e);
             e[MesKeyStr.Sender] = Sender.others.ToString();
             connector.SendMessage(LoginedUserList[i].Socket, e);
         }
     }
 }
Exemplo n.º 10
0
        private void OnWrite()
        {
            //判断发送队列是否有信息
            if (writeQueue.Count == 0)
            {
                m_bIsWriting = false;
                return;
            }
            //取出第一条代发消息
            byte[] buff = writeQueue.Dequeue();
            //设置消息发送异步对象的发送数据缓冲区数据
            SendSAEA.SetBuffer(buff, 0, buff.Length);
            //开启异步发送
            bool willRaiseEvent = UserSocket.SendAsync(SendSAEA);

            //是否挂起
            if (!willRaiseEvent)
            {
                sendProcess(SendSAEA);
            }
        }
Exemplo n.º 11
0
        private void Connector_GroupMessageEvent(object sender, MessageDictionary e)
        {
            UserSocket user = GetUserSocket(e[MesKeyStr.UserID]);
            string     ip   = ((IPEndPoint)user.Socket.RemoteEndPoint).Address.ToString();

            e.Add(MesKeyStr.NickName, user.NickName);
            e.Add(MesKeyStr.IP, ip);
            e.Add(MesKeyStr.DateTime, DateTime.Now.ToString());
            MessageArrivedEvent?.Invoke(this, e);
            e.Add(MesKeyStr.Sender, Sender.others.ToString());
            foreach (UserSocket u in LoginedUserList)
            {
                if (u.UserID.Equals(e[MesKeyStr.UserID]))
                {
                    continue;
                }
                connector.SendMessage(u.Socket, e);
            }
            e[MesKeyStr.Sender] = Sender.self.ToString();
            connector.SendMessage(user.Socket, e);
        }
Exemplo n.º 12
0
        private void Connector_LoginEvent(object sender, LoginEventArgs e)
        {
            Account account = GetAccount(e.UserID);

            if (account == null)
            {
                connector.SendLoginResult(new UserSocket(e.UserID, " ", e.ReceiveSocket), false);
                return;
            }

            UserSocket newUserSocket = new UserSocket(account.UserID, account.NickName, e.ReceiveSocket);

            if (account.ConfirmPassword(e.PassWord) && GetUserSocket(e.UserID) == null)
            {
                connector.SendLoginResult(newUserSocket, true);
                //connector.SendUserChange(newUserSocket, newUserSocket.ToUser(), CommandType.UserJoin);

                //向已登录的用户发送新登录用户的信息
                foreach (UserSocket oldUser in LoginedUserList)
                {
                    connector.SendUserChange(oldUser, newUserSocket.ToUser(), CommandType.UserJoin);
                }

                //向新登录用户发送已登录的用户的信息
                foreach (UserSocket oldUser in LoginedUserList)
                {
                    connector.SendUserChange(newUserSocket, oldUser.ToUser(), CommandType.UserJoin);
                }

                LoginedUserList.Add(newUserSocket);

                UserJoinEvent?.Invoke(this, new User(newUserSocket.UserID, newUserSocket.NickName));
                return;
            }
            else
            {
                connector.SendLoginResult(newUserSocket, false);
                return;
            }
        }
Exemplo n.º 13
0
        public bool SendUserChange(UserSocket oldUserSocket, User newUser, CommandType commandType)
        {
            if (commandType != CommandType.UserJoin && commandType != CommandType.UserQuit)
            {
                ShowMessage("发送用户变化错误,非法的命令类型");
                return(false);
            }

            MessageDictionary messageD = new MessageDictionary();

            messageD.Add(MesKeyStr.CommandType, commandType.ToString());
            messageD.Add(MesKeyStr.UserID, newUser.UserID);
            messageD.Add(MesKeyStr.NickName, newUser.NickName);
            try
            {
                Send(oldUserSocket.Socket, messageD.ToString());
                return(true);
            }
            catch (Exception e)
            {
                ShowMessage("发送用户变化错误" + e.Message + "\n" + e.StackTrace + "\n");
                return(false);
            }
        }
Exemplo n.º 14
0
 public void CloseSocket()
 {
     UserSocket.Shutdown(SocketShutdown.Both);
     UserSocket.Close();
 }