Ejemplo n.º 1
0
        /// <summary>
        /// Receber mensagens
        /// </summary>
        /// <param name="username">Nome do user</param>
        /// <param name="socket"></param>
        /// <param name="result"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public override async Task ReceiveAsync(string username, WebSocket socket, WebSocketReceiveResult result, Dictionary <string, string> values)
        {
            if (!values.ContainsKey("chat") || !values.ContainsKey("message") || !values.ContainsKey("time"))
            {
                await WebSocketConnectionManager.RemoveSocket(WebSocketConnectionManager.GetId(socket));
            }
            else
            {
                DateTime time = DateTime.Parse(values["time"]);
                var      chat = await ServiceChat.NewMessage(Convert.ToInt32(values["chat"]), username, values["message"], time);

                var user = await ServiceUser.GetUserById(chat.IdUser);

                if (chat != null && user != null)
                {
                    string json = jsonBuilder(user.Name, username, chat.IdChat, chat.Text, chat.Time);
                    //
                    await SendMessageToAllAsync(json.ToString());
                }
            }
        }