Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

121 строка
3.5 KiB

  1. using Microsoft.AspNet.SignalR;
  2. using Microsoft.AspNet.SignalR.Hubs;
  3. using System;
  4. using System.Threading.Tasks;
  5. namespace Quanjiang.DigitalSchool.IMServerService
  6. {
  7. /// <summary>
  8. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  9. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  10. /// 创建人:陈彬彬
  11. /// 日 期:2017.04.01
  12. /// 描 述:即使通信服务(可供客户端调用的方法开头用小写)
  13. /// </summary>
  14. [HubName("ChatsHub")]
  15. public class Chats : Hub
  16. {
  17. #region 重载Hub方法
  18. /// <summary>
  19. /// 建立连接
  20. /// </summary>
  21. /// <returns></returns>
  22. public override Task OnConnected()
  23. {
  24. AddOnline();
  25. return base.OnConnected();
  26. }
  27. /// <summary>
  28. /// 断开连接
  29. /// </summary>
  30. /// <param name="stopCalled">是否是客户端主动断开:true是,false超时断开</param>
  31. /// <returns></returns>
  32. public override Task OnDisconnected(bool stopCalled)
  33. {
  34. RemoveOnline();
  35. return base.OnDisconnected(stopCalled);
  36. }
  37. /// <summary>
  38. /// 重新建立连接
  39. /// </summary>
  40. /// <returns></returns>
  41. public override Task OnReconnected()
  42. {
  43. AddOnline();
  44. return base.OnReconnected();
  45. }
  46. #endregion
  47. #region 客户端操作
  48. /// <summary>
  49. /// 添加在线用户
  50. /// </summary>
  51. public void AddOnline()
  52. {
  53. string clientId = Context.ConnectionId;
  54. string userId = GetUserId();
  55. Groups.Add(clientId, userId);
  56. }
  57. /// <summary>
  58. /// 移除在线用户
  59. /// </summary>
  60. public void RemoveOnline()
  61. {
  62. string clientId = Context.ConnectionId;
  63. string userId = GetUserId();
  64. Groups.Remove(clientId, userId);
  65. }
  66. /// <summary>
  67. /// 发送消息
  68. /// </summary>
  69. /// <param name="toUserId">对方UserId</param>
  70. /// <param name="msg">消息</param>
  71. /// <param name="isSystem">是否系统消息0不是1是</param>
  72. public void SendMsg(string toUserId, string msg, int isSystem)
  73. {
  74. string userId = GetUserId();
  75. Clients.Group(toUserId).RevMsg(userId, msg, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), isSystem);
  76. }
  77. /// <summary>
  78. /// 发送消息
  79. /// </summary>
  80. /// <param name="myUserId">我的UserId</param>
  81. /// <param name="toUserId">对方UserId</param>
  82. /// <param name="msg">消息</param>
  83. /// <param name="isSystem">是否系统消息0不是1是</param>
  84. public void SendMsg2(string myUserId, string toUserId, string msg, int isSystem)
  85. {
  86. Clients.Group(toUserId).RevMsg(myUserId, msg, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), isSystem);
  87. }
  88. #endregion
  89. #region 推送消息
  90. public void PushMessage(string msg)
  91. {
  92. Clients.All.broadcastMessage(msg);
  93. }
  94. #endregion
  95. #region 一般公用方法
  96. /// <summary>
  97. /// 获取登录用户Id
  98. /// </summary>
  99. /// <returns></returns>
  100. private string GetUserId()
  101. {
  102. string userId = "";
  103. if (Context.QueryString["userId"] != null)
  104. {
  105. userId = Context.QueryString["userId"];
  106. }
  107. return userId;
  108. }
  109. #endregion
  110. }
  111. }