You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
5.9 KiB
C#

10 months ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using DSWeb.SoftMng.BLL;
using DSWeb.Areas.CommMng.Models;
using Newtonsoft.Json;
using DSWeb.MvcShipping.DAL.MsSysParamSet;
using DSWeb.Areas.MvcShipping.Helper;
using DSWeb.EntityDA;
namespace DSWeb
{
[HubName("chatHub")]
public class ChatHub : Hub
{
// 静态属性
public static List<UserInfo> OnlineUsers = new List<UserInfo>(); // 在线用户列表
static readonly IHubContext HubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();//取得Hub上下文
/// <summary>
/// 登录连线
/// </summary>
/// <param name="userId">用户Id</param>
/// <param name="userName">用户名</param>
public void Connect(string userId, string userName, string userFace)
{
var connnectId = Context.ConnectionId;
if (OnlineUsers.Count(x => x.ConnectionId == connnectId) == 0)
{
if (OnlineUsers.Any(x => x.UserId == userId))
{
var items = OnlineUsers.Where(x => x.UserId == userId).ToList();
foreach (var item in items)
{
Clients.AllExcept(connnectId).onUserDisconnected(item.ConnectionId, item.UserName);
}
OnlineUsers.RemoveAll(x => x.UserId == userId);
}
//添加在线人员
OnlineUsers.Add(new UserInfo
{
ConnectionId = connnectId,
UserId = userId,
UserName = userName,
UserFace = userFace,
LastLoginTime = DateTime.Now
});
}
// 所有客户端同步在线用户
Clients.All.onConnected(connnectId, userName, OnlineUsers);
}
/// <summary>
/// 发送私聊
/// </summary>
/// <param name="toUserId">接收方用户连接ID</param>
/// <param name="data">内容</param>
public void SendPrivateMessage(string toUserId, SYSMessagemb model)
{
//var fromUserId = Context.ConnectionId;
// var toUser = OnlineUsers.FirstOrDefault(x => x.ConnectionId == toUserId);
var toUser = OnlineUsers.FirstOrDefault(x => x.UserId == toUserId);
//var fromUser = OnlineUsers.FirstOrDefault(x => x.ConnectionId == fromUserId);
sys_messageBLL mbll = new sys_messageBLL();
var msgModel = mbll.GetModel(model.GID);
//_bll.Add(model);
/*判断是否接收推送*/
op_message_configBLL _bllc = new op_message_configBLL();
int count = _bllc.GetRecordCount(string.Format("UserCode='{0}' and PId in (select GId from op_message_module where Name ='{1}') and IsPush=0", model.RECEIVER, model.NAME));
if (toUser != null && count == 0)
{
//服务端直接发送消息(S->B)
HubContext.Clients.Client(toUserId).receivePrivateMessage("admin", "系统消息", msgModel);
// send to caller user
// Clients.Caller.sendPrivateMessage(toUserId, fromUser.UserName, message);
}
//else
//{
// //表示对方不在线(仅限B->S->B)注:调用来自请求客户端的方法
// //Clients.Caller.absentSubscriber();
//}
}
/// <summary>
/// 公告广播
/// </summary>
/// <param name="model">公告内容</param>
/// <param name="action">添加或修改</param>
public void SendNotice(SoftMng.Model.op_Notice model, string action, string ids = "")
{
if (model != null)
{
var result = new
{
model.GID,
model.Title,
CreateTime = ((DateTime)model.CreateTime).ToString("yyyy-MM-dd HH:mm")
};
//服务端直接发送消息(S->B)
HubContext.Clients.All.receiveNotice(result, action);
}
else
//服务端直接发送消息(S->B)删除
HubContext.Clients.All.receiveNotice(ids, action);
}
/// <inheritdoc />
/// <summary>
/// 断线时调用
/// </summary>
/// <param name="stopCalled"></param>
/// <returns></returns>
public override Task OnDisconnected(bool stopCalled)
{
var user = OnlineUsers.FirstOrDefault(u => u.ConnectionId == Context.ConnectionId);
// 判断用户是否存在,存在则删除
if (user == null) return base.OnDisconnected(stopCalled);
Clients.All.onUserDisconnected(user.ConnectionId, user.UserName); //调用客户端用户离线通知
// 删除用户
OnlineUsers.Remove(user);
return base.OnDisconnected(stopCalled);
}
public bool SendMessage(string userId, string messageId)
{
var toUser = OnlineUsers.FirstOrDefault(x => x.UserId == userId);
sys_messageBLL mbll = new sys_messageBLL();
var msgModel = mbll.GetModel(messageId);
//op_message_configBLL _bllc = new op_message_configBLL();
//int count = _bllc.GetRecordCount(string.Format("UserCode='{0}' and PId in (select GId from op_message_module where Name ='{1}') and IsPush=0", msgModel.RECEIVER, msgModel.NAME));
if (toUser != null && msgModel != null)
{
//服务端直接发送消息(S->B)
HubContext.Clients.Client(toUser.ConnectionId).receivePrivateMessage("admin", "系统消息", msgModel);
return true;
}
return false;
}
}
}