Esempio n. 1
0
 /// <summary>
 /// Enviar uma mensagem para todos os users
 /// </summary>
 /// <param name="json">Mensagem a enviar</param>
 /// <returns></returns>
 public override async Task SendMessageToAllAsync(string json)
 {
     foreach (var pair in WebSocketConnectionManager.GetAll())
     {
         if (pair.Value.State == WebSocketState.Open)
         {
             await SendMessageAsync(pair.Value, json);
         }
     }
 }
Esempio n. 2
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());
                }
            }
        }