Пример #1
0
        public void Handle(HttpListenerRequest request, HttpListenerResponse response)
        {
            using (var ms = new MemoryStream())
            {
                if (request.HttpMethod == "OPTIONS")
                {
                    response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
                    response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                    response.AddHeader("Access-Control-Max-Age", "1728000");
                }
                response.AppendHeader("Access-Control-Allow-Origin", "*");

                request.InputStream.CopyTo(ms);
                var content           = Encoding.UTF8.GetString(ms.ToArray());
                var registrationModel = JObject.Parse(content);

                var session = registrationModel["session"].ToString();

                var           user    = _sessionManagementService.GetUserBySession(session);
                var           chatIds = _dbContext.ApplicationUserChatRooms.Where(p => p.ApplicationUserId == user).ToList();
                List <string> Chats   = new List <string>();
                foreach (var id in chatIds)
                {
                    Chats.Add(_dbContext.ChatRooms.FirstOrDefault(p => p.Id == id.ChatRoomId).Title);
                }
                response.StatusCode = 200;

                var json = JsonConvert.SerializeObject(Chats);
                response.OutputStream.Write(Encoding.UTF8.GetBytes(json));
                response.Close();
            }
        }
Пример #2
0
        static async Task OnMessage(
            IWebSocketConnection connection,
            SessionManagementService sessionManagement,
            WebSocketConnectionManager webSocketConnectionManager,
            ApplicationDbContext applicationDbContext,
            string message)
        {
            var json = JObject.Parse(message);

            if (json["type"].ToString() == "message")
            {
                var sessionId = json["sessionId"].ToString();
                var chatName  = json["chatName"].ToString();

                var chatId = applicationDbContext.ChatRooms.FirstOrDefault(p => p.Title == chatName).Id;

                var userId = sessionManagement.GetUserBySession(sessionId);
                var user   = applicationDbContext.ApplicationUsers.Find(userId);

                applicationDbContext.ChatMessages.Add(new ChatMessage()
                {
                    Body = message,
                    SenderApplicationUserId = (int)userId,
                    ChatRoomId = chatId
                });
                applicationDbContext.SaveChanges();

                webSocketConnectionManager.AddSocketConnection(sessionId, connection);

                foreach (var activeConnection in webSocketConnectionManager.GetAllActiveConnections())
                {
                    await activeConnection.Send(message);
                }
            }
            else if (json["type"].ToString() == "REQ")
            {
                var sessionId = json["sessionId"].ToString();
                var chatName  = json["chatName"].ToString();

                var chatId = applicationDbContext.ChatRooms.FirstOrDefault(p => p.Title == chatName).Id;

                var chatMessages = applicationDbContext.ChatMessages.Where(p => p.ChatRoomId == chatId).ToList();

                webSocketConnectionManager.AddSocketConnection(sessionId, connection);

                var webSocket = webSocketConnectionManager.GetConnectionBySession(sessionId);

                foreach (var mes in chatMessages)
                {
                    webSocket.Send(mes.Body);
                }
            }
        }
Пример #3
0
        static async Task OnMessage(
            IWebSocketConnection connection,
            SessionManagementService sessionManagement,
            WebSocketConnectionManager webSocketConnectionManager,
            ApplicationDbContext applicationDbContext,
            string message)
        {
            var json      = JObject.Parse(message);
            var sessionId = json["sessionId"].ToString();

            var userId = sessionManagement.GetUserBySession(sessionId);
            var user   = applicationDbContext.ApplicationUsers.Find(userId);

            webSocketConnectionManager.AddSocketConnection(sessionId, connection);

            foreach (var activeConnection in webSocketConnectionManager.GetAllActiveConnections())
            {
                await activeConnection.Send($"Пользователь {user.PhoneNumber} онлайн!");
            }

            await connection.Send("OK");
        }
Пример #4
0
        public void Handle(HttpListenerRequest request, HttpListenerResponse response)
        {
            using (var ms = new MemoryStream())
            {
                request.InputStream.CopyTo(ms);
                var content       = Encoding.UTF8.GetString(ms.ToArray());
                var junctionModel = JObject.Parse(content);

                var sessionId       = junctionModel["SessionId"].ToString();
                var chatRoomId      = junctionModel["ChatRoomId"].ToString();
                var userPhoneNumber = junctionModel["UserPhoneNumber"].ToString();

                var user = GetUserByPhoneNumber(userPhoneNumber);

                var sender = _sessionManagementService.GetUserBySession(sessionId);

                if (sender == null || chatRoomId == null || user == null)
                {
                    response.StatusCode = 404;

                    response.OutputStream.Write(Encoding.UTF8.GetBytes("NOT FOUND!"));
                    response.Close();
                    return;
                }

                var junction = new ApplicationUserChatRoomJunction()
                {
                    ApplicationUserId     = user.Id,
                    ChatRoomId            = Int32.Parse(chatRoomId),
                    ApplicationUserStatus = ApplicationUserInChatStatus.Active
                };

                _dbContext.ApplicationUserChatRooms.Add(junction);
                _dbContext.SaveChanges();

                response.StatusCode = 200;
                response.Close();
            }
        }
        public void Handle(HttpListenerRequest request, HttpListenerResponse response)
        {
            using (var ms = new MemoryStream())
            {
                request.InputStream.CopyTo(ms);
                var content       = Encoding.UTF8.GetString(ms.ToArray());
                var chatRoomModel = JObject.Parse(content);

                var title     = chatRoomModel["Title"].ToString();
                var type      = JsonConvert.DeserializeObject <ChatRoomType>(chatRoomModel["Type"].ToString());
                var sessionId = chatRoomModel["SessionId"].ToString();

                var userId = _sessionManagementService.GetUserBySession(sessionId);

                if (userId == null)
                {
                    response.StatusCode = 404;

                    response.OutputStream.Write(Encoding.UTF8.GetBytes("NOT FOUND!"));
                    response.Close();
                    return;
                }

                var mainChat = new ChatRoom()
                {
                    Title        = title,
                    ChatRoomType = type
                };

                _dbContext.ChatRooms.Add(mainChat);

                _dbContext.SaveChanges();

                response.OutputStream.Write(Encoding.UTF8.GetBytes(mainChat.Id.ToString()));
                response.StatusCode = 200;
                response.Close();
            }
        }
Пример #6
0
        public void Handle(HttpListenerRequest request, HttpListenerResponse response)
        {
            using (var ms = new MemoryStream())
            {
                request.InputStream.CopyTo(ms);
                var content       = Encoding.UTF8.GetString(ms.ToArray());
                var junctionModel = JObject.Parse(content);

                var sessionId = junctionModel["SessionId"].ToString();

                var userId = _sessionManagementService.GetUserBySession(sessionId);

                if (userId == null)
                {
                    response.StatusCode = 404;

                    response.OutputStream.Write(Encoding.UTF8.GetBytes("NOT FOUND!"));
                    response.Close();
                    return;
                }

                List <string> users = new List <string>();

                foreach (var user in _dbContext.ApplicationUsers)
                {
                    users.Add(user.PhoneNumber);
                }

                var jsonObject = JsonConvert.SerializeObject(users);


                response.OutputStream.Write(Encoding.UTF8.GetBytes(jsonObject));

                response.StatusCode = 200;
                response.Close();
            }
        }