{"size": 1546, "ext": "js", "max_stars_repo_path": "src/renderer/util/Huya/socket.js", "max_stars_repo_name": "npmmirror/Barrage-helper", "max_stars_repo_stars_event_min_datetime": "2019-09-20T07:41:13.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-04T05:37:13.000Z", "max_issues_repo_path": "src/renderer/util/Huya/socket.js", "max_issues_repo_name": "npmmirror/Barrage-helper", "max_issues_repo_issues_event_min_datetime": "2021-01-28T19:46:04.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-25T18:41:03.000Z", "max_forks_repo_path": "src/renderer/util/Huya/socket.js", "max_forks_repo_name": "npmmirror/Barrage-helper", "max_forks_repo_forks_event_min_datetime": "2019-07-05T02:03:50.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-18T06:41:32.000Z"}
{"max_stars_count": 27, "max_issues_count": 7, "max_forks_count": 28, "avg_line_length": 22.7352941176, "max_line_length": 49, "alphanum_fraction": 0.5717981889}
import axios from 'axios';
import mitt from 'mitt';
import { server } from 'root/config';

function HuyaSocket(doName, roomId) {
  const mitter = mitt();
  this.on = mitter.on;

  axios.get(`${server.host}/${doName}/${roomId}`)
    .then((res) => {
      const url = res.data;
      const socket = new WebSocket(url);
      let timer = null;
      socket.onopen = (event) => {
        mitter.emit('success', event);
        timer = setInterval(() => {
          socket.send('ping');
        }, 15000);
      };

      socket.onmessage = (event) => {
        const json = JSON.parse(event.data);
        if (json.statusCode === 200) {
          mitter.emit('message', json.data);
        } else {
          // 错误处理
          mitter.emit('errorMessage', json);
        }
      };

      socket.onerror = (err) => {
        mitter.emit('error', err);
      };

      socket.onclose = (e) => {
        clearInterval(timer);
        mitter.emit('close', e);
      };

      this.close = socket.close.bind(socket);
    })
    .catch((err) => {
      mitter.emit('fail', err);
    });
}

HuyaSocket.apiList = {
  getMessageNotice: '普通弹幕接口',
  getSendItemNotice: '礼物接口',
  getVipEnterBannerNotice: '高级用户进场信息接口',
};

HuyaSocket.BARRAGE_MSG = 'getMessageNotice';
HuyaSocket.GIFT_MSG = 'getSendItemNotice';
HuyaSocket.VIP_MSG = 'getVipEnterBannerNotice';

// 所有事件
// 普通弹幕接口、礼物接口、高级用户接口
HuyaSocket.eventList = {
  success: '连接成功',
  fail: '连接失败',
  message: '收到正常消息',
  errorMessage: '收到错误消息',
  error: '发生错误',
  close: '关闭事件',
};
export default HuyaSocket;
