export default {
  webSocket: Object,
  endData: Object,
  data: Object,
  type: 'a',
  baseUrl: '',
  runmessage: function (event) {
  },
  start: function (returnMsgFun, error) {
    try {
      this.webSocket = new WebSocket(this.baseUrl)
      let _this = this
      this.webSocket.onmessage = function (event) {
        if (event.data.indexOf('$*$') !== '-1') {
          let str = event.data.substring(3)
          event.data1 = str
        }
        _this.data = event
        _this.runmessage(event)
        if (typeof returnMsgFun === 'function') {
          returnMsgFun(event)
        }
      }
      this.webSocket.onerror = function (event) {
        if (typeof error === 'function') {
          error({ data: '没有发现设备' })
        }
      }
    } catch (e) {
      let _this = this
      this.webSocket.onmessage = function (event) {
        if (event.data.indexOf('$*$') !== '-1') {
          let str = event.data.substring(3)
          event.data1 = str
        }
        _this.data = event
        _this.runmessage(event)
      }
      if (typeof error === 'function') {
        error({ data: '没有发现设备' })
      }
    }
  },
  cat: function () {
    this.endData = this.data
    // this.sendMsg('bStopPlay')
  },
  sendMsg: function (msg) {
    try {
      this.webSocket.send(msg)
    } catch (e) {
      console.log(e, '9999991')
    }
  },
  over: function () {
    this.webSocket.send('bStopPlay()')
  },
  close: function () {
    this.webSocket.send('bStopPlay()')
  }
}
