Ejemplo n.º 1
0
 public void Add(SysSignalR item)
 {
     using (var db = new ApplicationDb())
     {
         db.SysSignalRs.Add(item);
         db.SaveChanges();
     }
 }
Ejemplo n.º 2
0
        protected override Task OnReceived(IRequest request, string connectionId, string data)
        {
            var item = JsonConvert.DeserializeObject <DataMessage>(data);

            //message = Sanitizer.GetSafeHtmlFragment(message);
            item.Message = Encoder.HtmlEncode(item.Message);
            //message = Encoder.JavaScriptEncode(message);

            ////替换表情成图片链接
            item.Message = item.Message.Replace("[biaoqing]", "<img src='");
            item.Message = item.Message.Replace("[/biaoqing]", "' />");

            if (string.IsNullOrEmpty(item.Message))
            {
                return(null);
            }

            if (item.UserId.HasValue)
            {
                var sysSignalR = new SysSignalR
                {
                    GroupId      = GroupId,
                    EnterpriseId = _enterpriseId,
                    GroupName    = _groupName,
                    Message      = item.Message,
                    UserId       = _userId,
                    UserId1      = item.UserId.Value
                };
                sysSignalR.UserName  = _iSysUserService.GetDisplayName(sysSignalR.UserId);
                sysSignalR.UserName1 = _iSysUserService.GetDisplayName(item.UserId.Value);

                _iSysSignalRService.Add(sysSignalR);


                //收件人
                //查询用户的connectionid
                var onlineconnectionId =
                    _iSysSignalROnlineService.GetAll()
                    .Where(a => a.UserId == item.UserId.Value)
                    .OrderByDescending(a => a.CreatedDate)
                    .Select(a => a.ConnectionId)
                    .FirstOrDefault();

                if (!string.IsNullOrEmpty(onlineconnectionId))
                {
                    sysSignalR.UserName = "******"javascript:;\" onclick=\"$('#UserId').val('" + sysSignalR.UserId + "');\">" +
                                          sysSignalR.UserName + "</a>" +
                                          " 对 <a href=\"javascript:;\" onclick=\"$('#UserId').val('" + sysSignalR.UserId1 + "');\">" + sysSignalR.UserName1 + "</a> 说";
                    Connection.Send(onlineconnectionId, JsonConvert.SerializeObject(sysSignalR));
                }
                else
                {
                    var user = _iSysUserService.GetById(item.UserId.Value);
                    if (!string.IsNullOrEmpty(user.Email))
                    {
                        Email.SendEmail(user.Email, "您收到 " + sysSignalR.UserName + " 发来的消息", sysSignalR.Message);
                        sysSignalR.Message = sysSignalR.Message + " (对方不在线,直接发送到对方邮箱中。)";
                    }
                    else
                    {
                        sysSignalR.Message = sysSignalR.Message + " (对方不在线)";
                    }
                }

                sysSignalR.UserName = "******"javascript:;\" onclick=\"$('#UserId').val('" + sysSignalR.UserId + "');\">" +
                                      sysSignalR.UserName + "</a>" +
                                      " 对 <a href=\"javascript:;\" onclick=\"$('#UserId').val('" + sysSignalR.UserId1 + "');\">" + sysSignalR.UserName1 + "</a> 说";
                //发件人
                Connection.Send(connectionId, JsonConvert.SerializeObject(sysSignalR));
            }
            else
            {
                //保存沟通记录
                var sysSignalR = new SysSignalR
                {
                    GroupId      = GroupId,
                    EnterpriseId = _enterpriseId,
                    GroupName    = _groupName,
                    Message      = item.Message,
                    UserId       = _userId
                };

                sysSignalR.UserName = _iSysUserService.GetDisplayName(sysSignalR.UserId);

                _iSysSignalRService.Add(sysSignalR);

                sysSignalR.UserName = "******"javascript:;\" onclick=\"$('#UserId').val('" + sysSignalR.UserId + "');\">" +
                                      sysSignalR.UserName + "</a>" +
                                      " 对 <a href=\"javascript:;\" onclick=\"$('#UserId').val('');\">大家</a> 说";
                Groups.Send(_groupName, JsonConvert.SerializeObject(sysSignalR));
            }

            return(base.OnReceived(request, connectionId, data));
        }