using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Threading.Tasks;
namespace Quanjiang.DigitalSchool.IMServerService
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.04.01
/// 描 述:即使通信服务(可供客户端调用的方法开头用小写)
///
[HubName("ChatsHub")]
public class Chats : Hub
{
#region 重载Hub方法
///
/// 建立连接
///
///
public override Task OnConnected()
{
AddOnline();
return base.OnConnected();
}
///
/// 断开连接
///
/// 是否是客户端主动断开:true是,false超时断开
///
public override Task OnDisconnected(bool stopCalled)
{
RemoveOnline();
return base.OnDisconnected(stopCalled);
}
///
/// 重新建立连接
///
///
public override Task OnReconnected()
{
AddOnline();
return base.OnReconnected();
}
#endregion
#region 客户端操作
///
/// 添加在线用户
///
public void AddOnline()
{
string clientId = Context.ConnectionId;
string userId = GetUserId();
Groups.Add(clientId, userId);
}
///
/// 移除在线用户
///
public void RemoveOnline()
{
string clientId = Context.ConnectionId;
string userId = GetUserId();
Groups.Remove(clientId, userId);
}
///
/// 发送消息
///
/// 对方UserId
/// 消息
/// 是否系统消息0不是1是
public void SendMsg(string toUserId, string msg, int isSystem)
{
string userId = GetUserId();
Clients.Group(toUserId).RevMsg(userId, msg, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), isSystem);
}
///
/// 发送消息
///
/// 我的UserId
/// 对方UserId
/// 消息
/// 是否系统消息0不是1是
public void SendMsg2(string myUserId, string toUserId, string msg, int isSystem)
{
Clients.Group(toUserId).RevMsg(myUserId, msg, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), isSystem);
}
#endregion
#region 推送消息
public void PushMessage(string msg)
{
Clients.All.broadcastMessage(msg);
}
#endregion
#region 一般公用方法
///
/// 获取登录用户Id
///
///
private string GetUserId()
{
string userId = "";
if (Context.QueryString["userId"] != null)
{
userId = Context.QueryString["userId"];
}
return userId;
}
#endregion
}
}