group.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import { Message } from 'element-ui';
  2. import { rtc, publishStream, closeVideo, createData } from '@/views/live/common';
  3. export {
  4. initSDK,
  5. sendMsg,
  6. getLiveStat,
  7. createScript,
  8. downloadWebSDK,
  9. closeVideo,
  10. handsDown,
  11. chatRoll,
  12. pauseAudio,
  13. playAudio,
  14. pauseVideo,
  15. playVideo
  16. } from '@/views/live/common';
  17. /**
  18. * 取消订阅远程流
  19. */
  20. export function unSubscribeStream(unSubStream) {
  21. rtc.unSubscribeStream({
  22. unSubStream,
  23. success(str) {
  24. console.log('取消订阅流成功', str);
  25. },
  26. fail(str) {
  27. console.log(str);
  28. }
  29. });
  30. }
  31. /**
  32. * 初始化监听事件
  33. */
  34. export function initListener(vue) {
  35. rtc.on('login_success', data => {
  36. console.log('登录成功', data);
  37. Message({
  38. message: '登录成功',
  39. type: 'success'
  40. });
  41. });
  42. rtc.on('login_failed', data => {
  43. console.log('登录失败', data);
  44. Message({
  45. message: '登录失败:' + JSON.stringify(data),
  46. type: 'warning'
  47. });
  48. });
  49. // 教师 必须在加入房间成功的事件回调里创建本地流
  50. rtc.on('conference_join', () => {
  51. console.log('加入房间成功');
  52. rtc.handsUp({
  53. success: str => {
  54. console.log('举手成功', str);
  55. console.log('创建本地流推流');
  56. rtc.createLocalStream({
  57. streamName: 'picture',
  58. createData: createData(),
  59. success(stream) {
  60. vue.localStream = true;
  61. console.log('创建本地流成功', stream);
  62. vue.hasVideo = stream.hasVideo;
  63. vue.hasAudio = stream.hasAudio;
  64. // 创建本地流成功,将流展示到id为 student 的dom元素盒子中
  65. stream.show('group-local');
  66. publishStream('picture'); // 如果需要立即推流,执行 publish 方法
  67. },
  68. fail(data) {
  69. vue.localStream = false;
  70. console.log('创建本地流失败,应用层处理', data);
  71. // 创建本地流失败,应用层处理
  72. Message({
  73. type: 'error',
  74. message: '创建本地流失败:' + data
  75. });
  76. }
  77. });
  78. },
  79. fail: data => {
  80. console.log('举手失败', data);
  81. }
  82. });
  83. });
  84. rtc.on('conference_join_failed', err => {
  85. // 加入房间失败 err为错误原因
  86. console.log('加入房间失败', err);
  87. });
  88. // 新增订阅流事件
  89. rtc.on('allow_sub', stream => {
  90. console.log('新增订阅流事件');
  91. if (stream.isMixed()) {
  92. console.log('是混合流,不订阅');
  93. } else {
  94. // 订阅远程流
  95. rtc.trySubscribeStream({
  96. tryStream: stream,
  97. success: stream => {
  98. // 订阅流成功
  99. let streamType = stream.streamType();
  100. console.log('订阅流成功', streamType);
  101. if (streamType === 10) {
  102. vue.streamList.push(stream);
  103. }
  104. if (streamType === 0) {
  105. stream.show('live', 'contain'); // 将流显示到指定 id 的元素中
  106. vue.is_teacher_in_group = true;
  107. }
  108. },
  109. fail(err) {
  110. console.log('订阅流失败', err);
  111. }
  112. });
  113. }
  114. });
  115. // 推流前查询直播状态失败,导致没有推流
  116. rtc.on('local_stream_publish_failed', () => {
  117. console.log('推流前查询直播状态失败,导致没有推流');
  118. Message({
  119. message: '推流前查询直播状态失败,导致没有推流',
  120. type: 'warning'
  121. });
  122. });
  123. // 房间全量信息事件(人员进出时广播)
  124. rtc.on('room_context', roomData => {
  125. vue.roomContext = JSON.parse(roomData);
  126. console.log('房间全量信息事件(人员进出时广播)', JSON.parse(roomData));
  127. });
  128. rtc.on('publish_stream', str => {
  129. console.log('直播已开启', str);
  130. Message({
  131. type: 'success',
  132. message: '直播已开启'
  133. });
  134. vue.liveStat = true;
  135. });
  136. rtc.on('end_stream', str => {
  137. console.log('直播已关闭', str);
  138. Message({
  139. type: 'success',
  140. message: '直播已关闭'
  141. });
  142. vue.liveStat = false;
  143. });
  144. // 单个用户配置监听
  145. rtc.on('switch_user_settings', settingData => {
  146. console.log('单个用户配置监听', JSON.parse(settingData));
  147. });
  148. // 人员列表事件(人员麦序变化时广播)
  149. rtc.on('speak_context', speakData => {
  150. vue.speakData = JSON.parse(speakData);
  151. console.log('人员列表事件(人员麦序变化时广播)', JSON.parse(speakData));
  152. });
  153. rtc.on('switch_settings', data => {
  154. console.log('房间设置事件', JSON.parse(data)); // 房间设置事件
  155. });
  156. rtc.on('publishStreamErr', data => {
  157. console.log('推流意外终止:' + data.streamName);
  158. // 直播开启状态下,尝试重推这条流
  159. });
  160. rtc.on('kick_out', function () {
  161. console.log('自己被踢出房间');
  162. });
  163. // 视频无法自动播放
  164. rtc.on('playError', data => {
  165. console.log('视频无法自动播放', data);
  166. });
  167. // 监听通知移除流事件
  168. rtc.on('stream_removed', stream => {
  169. console.log('监听通知移除流事件');
  170. if (stream.streamType() === 0) {
  171. vue.is_teacher_in_group = false;
  172. }
  173. let num = vue.streamList.findIndex(el => el.id() === stream.id());
  174. if (num !== -1) {
  175. vue.streamList.splice(num, 1);
  176. }
  177. });
  178. // 停止订阅流
  179. rtc.on('unSub', stream => {
  180. console.log('停止订阅流');
  181. unSubscribeStream(stream);
  182. });
  183. /**
  184. * 排麦监听事件
  185. */
  186. // 监听自己被邀请事件
  187. rtc.on('inviteUp', uid => {
  188. console.log('监听自己被邀请事件', uid);
  189. });
  190. rtc.on('mcDown', () => {
  191. closeVideo('picture');
  192. });
  193. rtc.on('videoModeChange', data => {
  194. console.log('连麦音视频模式更新成功监听回调', data.settings.video_mode);
  195. vue.roomInfo.video_mode = data.settings.video_mode;
  196. });
  197. rtc.on('createLocalStream', () => {
  198. console.log('创建本地流推流');
  199. });
  200. /**
  201. * 监听聊天事件
  202. */
  203. rtc.on('chat_message', data => {
  204. let dat = JSON.parse(data);
  205. // 敏感词过滤:如果发送的聊天消息被系统判定包含敏感词,则只有发送者能收到本条消息,房间内其他人都不会收到这条聊天消息。
  206. // 如果返回消息中有 isFilterChat 字段(消息不包含敏感词返回数据中无isFilterChat字段),且isFilterChat的值为1,则说明该消息包含敏感字,除发送者外其他人不会收到这条消息。
  207. if (dat.isFilterChat && dat.isFilterChat === 1) {
  208. return;
  209. }
  210. vue.chatList.push(dat);
  211. });
  212. rtc.on('allowChatChange', function (data) {
  213. let msg = data.settings.allow_chat ? '开言' : '禁言';
  214. Message({
  215. type: 'success',
  216. message: `全体${msg}成功`
  217. });
  218. });
  219. // 接收自定义消息
  220. rtc.on('publish_message', data => {});
  221. }