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="url">URL</param>
        /// <param name="userID">编号</param>
        /// <param name="pwd">密码</param>
        public void Connect(string url, string userID, string pwd)
        {
            try
            {
                object ip = string.Empty;
                Context.Request.Environment.TryGetValue("server.RemoteIpAddress", out ip);
                if (ip != null)
                {
                    url = ip.ToString();
                }
                var userlist = GetDataUser(userID, pwd);
                if (userlist.Count > 0)
                {
                    var id = Context.ConnectionId;
                    if (connections.Count(x => x.ConnectionID == id && x.UserAgent == userID) == 0)
                    {
                        connections.Add(new ConnectionPool
                        {
                            UserAgent    = userID,
                            ConnectionID = id,
                            ConnectUrl   = url,
                            ConnectTime  = DateTime.Now
                        });
                        _users[userID] = id;
                        _clients[id]   = userID;

                        Clients.Caller.onConnected(id, userID, url);
                        Clients.Client(id).onNewUserConnected(id, userID);
                        //Clients.AllExcept(id).onNewUserConnected(id,userID);
                    }
                    else
                    {
                        Clients.Caller.onConnected(id, userID, url);
                        Clients.Client(id).onExistUserConnected(id, userID);
                        //Clients.AllExcept(id).onExistUserConnected(id,userID);
                    }
                    //Clients.All.addMessage(Newtonsoft.Json.JsonConvert.SerializeObject(conUser));//输出当前所有连接.
                    ServiceSR ss = new ServiceSR();
                    ss.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(connections));
                }
                else
                {
                    Clients.All.ErrorMessage("错误用户信息.");
                }
            }
            catch (Exception ex)
            {
                Clients.All.ErrorMessage(ex.Message.ToString());
            }
        }
Example #3
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());
         }
     }
 }