signalR.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // 官方文档:https://docs.microsoft.com/zh-cn/aspnet/core/signalr/javascript-client?view=aspnetcore-6.0&viewFallbackFrom=aspnetcore-2.2&tabs=visual-studio
  2. import * as signalR from '@microsoft/signalr';
  3. import mittBus from '@/utils/mitt';
  4. export default {
  5. // signalR对象
  6. SR: null as any,
  7. // 失败连接重试次数
  8. failNum: 4,
  9. baseUrl: 'http://218.6.151.146:50100/hubs/hotline',
  10. groupName: '',
  11. init() {
  12. const token = sessionStorage.getItem('token');
  13. const connection = new signalR.HubConnectionBuilder()
  14. .withUrl(this.baseUrl, {accessTokenFactory: () => '', skipNegotiation: true, transport: 1})
  15. .withAutomaticReconnect() //自动重新连接
  16. .configureLogging(signalR.LogLevel.Warning)
  17. .build();
  18. this.SR = connection;
  19. // 断线重连
  20. connection.onclose(async () => {
  21. console.log('当前链接状态:', connection.state);
  22. // 建议用户重新刷新浏览器
  23. await this.start();
  24. });
  25. connection.onreconnected(() => {
  26. console.log('断线重连成功');
  27. location.reload();
  28. /*ElNotification({
  29. type: 'success',
  30. title: '提示',
  31. message: `断线重连成功`,
  32. });*/
  33. });
  34. // 服务端推送消息
  35. connection.on('Send', (message: any) => {
  36. mittBus.emit('Send', message);// 通知
  37. });
  38. // 服务端推送消息
  39. connection.on('BsDataShowArea1', (message: any) => {
  40. // console.log(`工单办理`, message);
  41. mittBus.emit('BsDataShowArea1', message);// 通知
  42. });
  43. // 服务端推送消息
  44. connection.on('BsDataShowArea3', (message: any) => {
  45. mittBus.emit('BsDataShowArea3', message);// 通知
  46. });
  47. // 服务端推送消息
  48. connection.on('BsDataShowArea8', (message: any) => {
  49. mittBus.emit('BsDataShowArea8', message);// 通知
  50. });
  51. // 服务端推送消息
  52. connection.on('SeatState', (message: any) => {
  53. mittBus.emit('SeatState', message);// 通知
  54. });
  55. // 服务端推送消息
  56. connection.on('BsSeatStateDataShowArea1', (message: any) => {
  57. mittBus.emit('BsSeatStateDataShowArea1', message);// 通知
  58. });
  59. // 服务端推送消息
  60. connection.on('BsSeatStateDataShowArea2', (message: any) => {
  61. mittBus.emit('BsSeatStateDataShowArea2', message);// 通知
  62. });
  63. // 服务端推送消息
  64. connection.on('BsSeatStateDataShowArea3', (message: any) => {
  65. mittBus.emit('BsSeatStateDataShowArea3', message);// 通知
  66. });
  67. // 服务端推送消息
  68. connection.on('BsSeatStateDataShowArea4', (message: any) => {
  69. mittBus.emit('BsSeatStateDataShowArea4', message);// 通知
  70. });
  71. // 服务端推送消息
  72. connection.on('ordercountstatistics', (message: any) => {
  73. mittBus.emit('ordercountstatistics', message);// 通知
  74. });
  75. // 服务端推送消息
  76. connection.on('orderHandlingDetail', (message: any) => {
  77. mittBus.emit('orderHandlingDetail', message);// 通知
  78. });
  79. },
  80. /**
  81. * @description 调用 this.signalR.start().then(async () => { await this.SR.invoke("method")})
  82. * state Connected 已连接 Connecting 正在连接 Disconnected 断开连接 Reconnecting 正在重新连接
  83. * @returns
  84. */
  85. async start() {
  86. try {
  87. //使用async和await 或 promise的then 和catch 处理来自服务端的异常
  88. await this.SR.start();
  89. console.log('当前链接状态:', this.SR.state);
  90. return Promise.resolve();
  91. } catch (error: any) {
  92. this.failNum--;
  93. if (this.failNum > 0 && this.SR.state === 'Disconnected') {
  94. //断开链接重新链接
  95. /*ElNotification({
  96. type: 'warning',
  97. title: '提示',
  98. message: `断开连接了,正在重连, 剩余自动重连次数${this.failNum} 次,如未重连成功,请刷新浏览器重试`,
  99. });*/
  100. setTimeout(async () => {
  101. await this.SR.start();
  102. }, 5000);
  103. }
  104. return Promise.reject(error);
  105. }
  106. },
  107. /**
  108. * @description 加入分组
  109. * @param groupName string 分组名称
  110. * @params {string} CallCenter 呼叫中心
  111. * @params BigScreenDataShow 大屏数据展示
  112. * @returns
  113. */
  114. async joinGroup(groupName: string) {
  115. if (this.SR.state === 'Connected') {
  116. // 判断是否已经建立链接 如果没有 先链接
  117. await this.SR.invoke('JoinGroupUnauthAsync', {GroupName: groupName});
  118. } else {
  119. // 等待链接成功再发送
  120. if (this.SR.state === 'Connecting') {
  121. setTimeout(async () => {
  122. await this.SR.invoke('JoinGroupUnauthAsync', {GroupName: groupName});
  123. }, 500)
  124. return
  125. }
  126. await this.start().then(async () => {
  127. setTimeout(async () => {
  128. await this.SR.invoke('JoinGroupUnauthAsync', {GroupName: groupName});
  129. }, 500)
  130. }).catch((err) => {
  131. console.log(err);
  132. })
  133. }
  134. },
  135. /**
  136. * @description 离开分组
  137. * @param groupName string
  138. * @params CallCenter 呼叫中心
  139. * @params BigScreenDataShow 大屏数据展示
  140. * @returns
  141. */
  142. async leaveGroup(groupName: string) {
  143. if (this.SR.state === 'Connected') {
  144. // 判断是否已经建立链接 如果没有 先链接
  145. await this.SR.invoke('LeaveGroupUnauthAsync', {GroupName: groupName});
  146. } else {
  147. // 等待链接成功再发送
  148. if (this.SR.state === 'Connecting') {
  149. setTimeout(async () => {
  150. await this.SR.invoke('LeaveGroupUnauthAsync', {GroupName: groupName});
  151. }, 500)
  152. return
  153. }
  154. await this.start().then(async () => {
  155. setTimeout(async () => {
  156. await this.SR.invoke('JoinGroupUnauthAsync', {GroupName: groupName});
  157. }, 500)
  158. }).catch((err) => {
  159. console.log(err);
  160. })
  161. }
  162. },
  163. /**
  164. * @description 关闭链接
  165. * @returns
  166. */
  167. async stop() {
  168. try {
  169. //使用async和await 或 promise的then 和catch 处理来自服务端的异常
  170. await this.SR.stop();
  171. console.log(this.SR.state, '断开链接');
  172. } catch (error) {
  173. console.log('signalR', error);
  174. }
  175. },
  176. };