Esempio n. 1
0
        private void HandleClientJoinGroup(int connectionId, byte[] data)
        {
            try
            {
                IRepository repository = _serviceProvider.GetRequiredService <IRepository>();
                ChatApplicationUserLogic chatApplicationUserLogic = new ChatApplicationUserLogic(repository);

                ByteBuffer buffer = new ByteBuffer();
                buffer.WriteBytes(data);
                int packetIdentify = buffer.ReadInteger();
                //In the server side you now send a string as next so you have to read out the string as next.
                string appId             = buffer.ReadString();
                string userEmail         = buffer.ReadString();
                string groupCreatorEmail = buffer.ReadString();

                (bool, string)response = chatApplicationUserLogic.AddUserToGroup(appId, userEmail, groupCreatorEmail);

                ServerTcp.SendJoinGroupResponseToClient(response.Item1, response.Item2);
            }
            catch (Exception e)
            {
                //todo: log error
                Console.WriteLine(e);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Create a group if the group does not exist
        /// </summary>
        /// <param name="connectionid"></param>
        /// <param name="data"></param>
        private void HandleClientCreateGroup(int connectionid, byte[] data)
        {
            try
            {
                IRepository    repository     = _serviceProvider.GetRequiredService <IRepository>();
                ChatGroupLogic chatGroupLogic = new ChatGroupLogic(repository);

                //Creates a new instance of the buffer to read out the packet.
                ByteBuffer buffer = new ByteBuffer();
                //writes the packet into a list to make it available to read it out.
                buffer.WriteBytes(data);
                //Todo INFO: You always have to read out the data as you sent it.
                //In this case you always have to first to read out the packet identifier.
                int packetIdentify = buffer.ReadInteger();
                //In the server side you now send a string as next so you have to read out the string as next.
                string appId     = buffer.ReadString();
                string userEmail = buffer.ReadString();
                (bool, string)response = chatGroupLogic.CreateNewChatGroup(appId, userEmail);

                int responseStatus = response.Item1 ? (int)BooleanStatus.True : (int)BooleanStatus.False;
                //Send create group response to the client
                ServerTcp.SendCreateGroupResponse(connectionid, responseStatus, response.Item2);
            }
            catch (Exception e)
            {
                //todo: Log error
                Console.WriteLine(e);
            }
        }
Esempio n. 3
0
        public static void InitServer(ChatMessageSingletonService chatMessageSingletonService, IMapper mapper, IServiceProvider serviceProvider)
        {
            ServerHandleData serverHandleData = new ServerHandleData(chatMessageSingletonService, mapper, serviceProvider);
            ServerTcp        serverTcp        = new ServerTcp();

            Text.WriteLine("Loading server ...", TextType.DEBUG);

            int start = GetTickCount();

            InitClients();
            serverHandleData.InitPacketsFromClient();

            serverTcp.InitServer();

            int end = GetTickCount();

            Text.WriteLine("Server loaded in {0} ms", TextType.DEBUG, end - start);
        }
Esempio n. 4
0
        private void HandleChatMessageFromClient(int connectionId, byte[] data)
        {
            try
            {
                //Creates a new instance of the buffer to read out the packet.
                ByteBuffer buffer = new ByteBuffer();
                //writes the packet into a list to make it avaiable to read it out.
                buffer.WriteBytes(data);
                //Todo INFO: You always have to read out the data as you sent it.
                //In this case you always have to first to read out the packet identifier.
                int packetIdentify = buffer.ReadInteger();
                //In the server side you now send a string as next so you have to read out the string as next.
                string chatJsonString = buffer.ReadString();

                //todo: convert to chat model and Save this chat in the singleton

                ChatMessageViewModel chatMessage = JsonConvert.DeserializeObject <ChatMessageViewModel>(chatJsonString);


                //todo: remove this message later
                string messageToDisplay = $"App ID: {chatMessage.AppId} Message: {chatMessage.Message} " +
                                          $"User ID: {chatMessage.UserEmail} " +
                                          $"Group Type: {Enum.GetName(typeof(GroupType), chatMessage.GroupType) } " +
                                          $"Date: {DateTime.UtcNow}";

                string messageJsonString = JsonConvert.SerializeObject(chatMessage);
                //Send message to other clients
                ServerTcp.SendChatMessageToClient(connectionId, messageJsonString);

                //Save Chat Message
                SaveChatMessage(chatMessage);

                //print out the string msg you did send from the server.
                Text.WriteLine($"Pckt ID: {packetIdentify} - Message: {chatMessage} ", TextType.INFO);
            }
            catch (Exception e)
            {
                Text.WriteLine($"Error occured in ServerHandleData:HandleChatMessageFromClient  with message {e.Message}", TextType.ERROR);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Gets chat history from singleton and sends it to the client
        /// </summary>
        /// <param name="connectionid"></param>
        /// <param name="data"></param>
        private void HandleChatHistoryFromClient(int connectionid, byte[] data)
        {
            //Creates a new instance of the buffer to read out the packet.
            ByteBuffer buffer = new ByteBuffer();

            //writes the packet into a list to make it avaiable to read it out.
            buffer.WriteBytes(data);
            //Todo INFO: You always have to read out the data as you sent it.
            //In this case you always have to first to read out the packet identifier.
            int packetIdentify = buffer.ReadInteger();
            //In the server side you now send a string as next so you have to read out the string as next.
            string appId = buffer.ReadString();

            //get the chat message history by application Id
            List <ChatMessage> chatMessageHistory = _chatMessageSingletonService.GetChatMessageHistoryByChatApplication(appId);


            //todo: use auto mapper to convert this to a viewmodel
            List <ChatMessageViewModel> chatMessageViewModels = _mapper.Map <List <ChatMessageViewModel> >(chatMessageHistory);
            string chatMessageHistoryJsonString = JsonConvert.SerializeObject(chatMessageViewModels);

            //Send chat history to client with connection id
            ServerTcp.SendChatMessageHistoryToClient(connectionid, chatMessageHistoryJsonString);
        }