Example #1
0
 /// <summary>
 /// 一对一通信
 /// </summary>
 /// <param name="msg">Json对象数据</param>
 public void SendOneToOne(string msg)
 {
     if (!string.IsNullOrEmpty(msg))
     {
         try
         {
             MessageInfo message = JsonConvert.DeserializeObject <MessageInfo>(msg);
             var         dTime   = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
             var         to      = message.Addressee;
             var         cid     = Context.ConnectionId; //当前用户的ConnectionID
             var         tid     = "";                   //接收信息方的ConnectionID
             if (connections.Count(x => x.UserAgent == to) > 0)
             {
                 tid = connections.FirstOrDefault(x => x.UserAgent == to).ConnectionID;
                 Clients.Client(tid).ReceiveFromOneMessage(msg); //对方也能看到
             }
             Clients.Client(cid).ReceiveFromOneMessage(msg);     //我要也能看到
             //服务器调用newLog().
             IHubContext _hubContext = GlobalHost.ConnectionManager.GetHubContext <ChatHub>();
             _hubContext.Clients.User(to).newLog();
             ServiceSR ss = new ServiceSR();
             ss.WriteIntoDataBase(msg);
             ss.WriteLog(msg);
         }
         catch (Exception ex)
         {
             Clients.All.ErrorMessage(ex.Message.ToString());
         }
     }
 }
Example #2
0
 /// <summary>
 /// 群组通信
 /// </summary>
 /// <param name="msg">Json对象数据</param>
 public void SendOneToGroup(string msg)
 {
     if (!string.IsNullOrEmpty(msg))
     {
         try
         {
             MessageInfo message = JsonConvert.DeserializeObject <MessageInfo>(msg);
             Clients.Group(message.TypeID.ToString()).ReceiveFromGroupMessage(message);
             ServiceSR ss = new ServiceSR();
             ss.WriteIntoDataBase(msg);
             ss.WriteLog(msg);
         }
         catch (Exception ex)
         {
             Clients.All.ErrorMessage(ex.Message.ToString());
         }
     }
 }