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