connectTask.service.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { Injectable } from '@angular/core';
  2. import { MessageService } from './message.service';
  3. import * as Parse from 'parse';
  4. @Injectable({
  5. providedIn: 'root',
  6. })
  7. export class ConnectTaskService {
  8. anchorChannelName?: string;
  9. msChannelName: string = 'user_connect_room'; // 主播在线上报频道
  10. onlineUserList: Array<string> = []; // 在线用户列表
  11. isSubscribe: boolean = false;
  12. constructor(private msgSer: MessageService) {}
  13. async init() {
  14. // 初始化消息服务,所有用户静默登录
  15. await this.msgSer.initRTM(this.msChannelName);
  16. await this.anchorOnline();
  17. this.getOnlieUserList(this.msChannelName, 'MESSAGE');
  18. }
  19. reset(){
  20. this.onlineUserList = [];
  21. this.isSubscribe = false;
  22. }
  23. /* 主播上线 */
  24. async anchorOnline() {
  25. let profile = JSON.parse(localStorage.getItem('profile') || '');
  26. const uid = Parse.User.current()?.id!;
  27. if (profile?.identyType == 'anchor' && !this.isSubscribe) {
  28. let nowChannes = await this.getWhereNow(uid);
  29. console.log('用户已订阅频道:', nowChannes);
  30. // if (!nowChannes.includes(this.msChannelName)) {
  31. console.log('订阅成功');
  32. /* 主播订阅主播频道 */
  33. this.msgSer.subscribeMessage(this.msChannelName); //主播开启并订阅自己的聊天频道
  34. // }
  35. // if (!nowChannes.includes(uid)) {
  36. this.msgSer.subscribeMessage(uid, { message: true }); //开启并订阅自己的聊天频道
  37. // }
  38. }
  39. }
  40. /* 获取用户当前所在频道 */
  41. async getWhereNow(userId: string): Promise<Array<string>> {
  42. let channes: Array<string> = [];
  43. try {
  44. const whereNowResult = await this.msgSer.rtmClient.presence.whereNow(
  45. userId
  46. );
  47. const { channels, totalChannel } = whereNowResult;
  48. channels.forEach((channelInfo: any) => {
  49. const { channelName, channelType } = channelInfo;
  50. channes.push(channelName);
  51. });
  52. return channes;
  53. } catch (status: any) {
  54. const { operation, reason, errorCode } = status;
  55. console.error(
  56. `${operation} failed, the error code is ${errorCode}, because of: ${reason}.`
  57. );
  58. return [];
  59. }
  60. }
  61. /* 获取在线用户列表 */
  62. async getOnlieUserList(
  63. channelName: string,
  64. page?: string,
  65. channelType?: string
  66. ) {
  67. if (this.onlineUserList.length > 0) return;
  68. console.log('获取在线用户列表');
  69. const options: any = {
  70. includedUserId: true,
  71. includedState: true,
  72. };
  73. if(page) options.page = page;
  74. try {
  75. const result = await this.msgSer.rtmClient.presence.whoNow(
  76. channelName,
  77. channelType ?? 'MESSAGE',
  78. options
  79. );
  80. // console.log(result);
  81. // 如果 nextPage 存在,下一次调用 whoNow 时,需将 nextPage 的值填入 whoNowOptions 的 page 字段
  82. const { totalOccupancy, occupants, nextPage } = result;
  83. occupants.forEach((userInfo: any) => {
  84. const { states, userId, statesCount } = userInfo;
  85. this.onlineUserList.push(userId);
  86. });
  87. if (nextPage) this.getOnlieUserList(channelName, nextPage, channelType);
  88. } catch (status: any) {
  89. const { operation, reason, errorCode } = status;
  90. console.error(
  91. `${operation} failed, ErrorCode: ${errorCode}, due to: ${reason}.`
  92. );
  93. }
  94. }
  95. }