示例#1
0
        /// <summary>
        /// Create a new chatroom
        /// </summary>
        /// <param name="user"></param>
        /// <param name="destinationuser"></param>
        public void MakeChatRoom(string user, string destinationuser)
        {
            ChatRoom c = new ChatRoom(count);

            //Adds the users
            c.AddUser(user);
            c.AddUser(destinationuser);

            _chatRoom.Add(c);
            count++;
        }
        public void ShouldSendMessageByMediatorTransmit()
        {
            var jane = new User("Jane");

            var andrew = new User("Andrew");

            var chatRoom = new ChatRoom();

            chatRoom.AddUser(jane);

            chatRoom.AddUser(andrew);

            jane.SendMessage(new Message("Hello Andrew", andrew, jane));

            andrew.SendMessage(new Message("Hello Jane", jane, andrew));
        }
 protected override void OnSessionNewMessage(WebSocketSession session, WebSocketMessage message)
 {
     if (message.Opcode == WebSocketOpcode.Text)
     {
         string       m           = Encoding.UTF8.GetString(message.Message);
         ChatProtocol chatMessage = JsonConvert.DeserializeObject <ChatProtocol>(m);
         if (string.IsNullOrEmpty(chatMessage.UserName))
         {
             session.Close("UserName missed", WebSocketCloseStatusCode.InternalServerError);
             Sessions.Remove(session.SessionID);
             return;
         }
         if (chatMessage.CommandType == "Login")
         {
             if (Room.ContainsUser(chatMessage.UserName))
             {
                 chatMessage.CommandType = "Error";
                 chatMessage.Message     = "User have been already in chatroom";
                 session.Close("User have already logined", WebSocketCloseStatusCode.InternalServerError);
                 Sessions.Remove(session.SessionID);
                 return;
             }
             else
             {
                 Room.AddUser(chatMessage.UserName, session.RemoteEndpoint);
                 var remotelist = Room.RemoteEndpointList;
                 foreach (var remote in remotelist)
                 {
                     WebSocketSession s = Sessions[remote];
                     if (s != null)
                     {
                         if (!s.Send(m))
                         {
                             Sessions.Remove(session.SessionID);
                             string user = Room.GetUser(remote);
                             if (!string.IsNullOrEmpty(user))
                             {
                                 Room.RemoveUser(user);
                             }
                         }
                     }
                 }
                 return;
             }
         }
         else if (chatMessage.CommandType == "Message")
         {
             var remotelist = Room.RemoteEndpointList;
             foreach (var remote in remotelist)
             {
                 WebSocketSession s = Sessions[remote];
                 if (s != null)
                 {
                     if (!s.Send(m))
                     {
                         Sessions.Remove(session.SessionID);
                         string user = Room.GetUser(remote);
                         if (!string.IsNullOrEmpty(user))
                         {
                             Room.RemoveUser(user);
                         }
                     }
                 }
             }
         }
         else
         {
             return;
         }
     }
 }