index.js 1.82 KB
import RTCClient from './rtc-client';
import axios from "axios";
import stomp from "./stomp";

import './assets/style.css'

if (!AgoraRTS.checkSystemRequirements()) {
  alert('Your web browser not support AgoraRTS!')
} else {
  console.log('check success')
}



$(() => {
  let rtc = new RTCClient()

  let subscribes = [];
  let sessionId = '';
  let loading = false;
  let token = '';
  let noticeInfo = {};

  function createWatch() {
    subscribes = [];
    subscribes.push({
      topic: '/broadcast/live',
      callback: (data) => {
        if (data.body) {
          let msg = JSON.parse(data.body)
          if (msg.type === 'changeSid') {
            sessionId = msg.content;
            console.log(msg.content);
            rtc.join(sessionId).then(() => {
              console.log('加入')
            })
          }
          if (msg.type === 'overLive') {
            sessionId = '';
            rtc.leave();
            getNotice();
          }
        }
      },
    });
    init();
  }

  function init() {
    if (subscribes.length > 0) {
      stomp.disconnect();
      stomp.connect(subscribes);
    }
  }

  createWatch();

  axios.post('https://openday.console.etoneiot.tech/openday-boot/sys/mLogin', {
    username: 'admin',
    password: 'admin123'
  }).then(r => {
    if (r.data.code === 200) {
      token = r.data.result.token;
      getNotice();
    }
  })

  function getNotice() {
    axios.get('https://openday.console.etoneiot.tech/openday-boot/openday/notice', {
      headers: {
        'X-Access-Token': token
      }
    }).then(r => {
      if (r.data.code === 200) {
        noticeInfo = r.data.result;
        sessionId = r.data.result.sessionId;
        rtc.join(sessionId).then(() => {
          console.log('加入')
        })
      } else {
        noticeInfo = {};
        sessionId = '';
      }
    })
  }

})