websocket.js 2 KB
Newer Older
yanzhongrong's avatar
yanzhongrong committed
1 2 3 4 5 6 7
/* eslint-disable */
import { Message } from "element-ui"
import EventBus from '@/utils/bus'

export default function socket(handlerOptions) {
  let {
    onmessage,
neogcg's avatar
neogcg committed
8 9
    onopen,
    // socketUrl
yanzhongrong's avatar
yanzhongrong committed
10
  } = handlerOptions
dupengyu's avatar
dupengyu committed
11
  // let socketUrl = 'ws://101.126.159.207:8087/device/websocket/1'
dupengyu's avatar
dupengyu committed
12
  // let socketUrl = 'ws://192.168.0.122:8087/device/websocket/1'
dupengyu's avatar
dupengyu committed
13
  let socketUrl = 'ws://127.0.0.1:8087/device/websocket/1'
yanzhongrong's avatar
yanzhongrong committed
14 15 16 17 18 19 20 21 22
  let websocket
  if ("WebSocket" in window) {
    websocket = new WebSocket(socketUrl)
  } else {
    Message.warning("当前浏览器不支持WebSocket推送,请升级浏览器")
  }

  let heart = heartCheck(websocket)

dupengyu's avatar
dupengyu committed
23
  websocket.onerror = function () {
yanzhongrong's avatar
yanzhongrong committed
24 25 26 27
    console.info("Websocket服务器连接错误")
  }

  //连接成功建立的回调方法
dupengyu's avatar
dupengyu committed
28
  websocket.onopen = function () {
yanzhongrong's avatar
yanzhongrong committed
29 30 31 32 33 34
    console.info("Websocket连接成功")
    heart.start()
    onopen && onopen()
  }

  //接收到消息的回调方法
dupengyu's avatar
dupengyu committed
35
  websocket.onmessage = function (event) {
yanzhongrong's avatar
yanzhongrong committed
36 37 38 39 40 41
    let message = event.data //消息内容
    heart.reset() //重置心跳上传时间
    onmessage && onmessage(message) //消息业务处理
  }

  //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
dupengyu's avatar
dupengyu committed
42
  window.onbeforeunload = function () {
yanzhongrong's avatar
yanzhongrong committed
43 44 45
    websocket.close()
  }
  return websocket
neogcg's avatar
neogcg committed
46
}
yanzhongrong's avatar
yanzhongrong committed
47 48 49 50
function heartCheck(websocket) {
  return {
    timeout: 15000,
    timeoutObj: null,
dupengyu's avatar
dupengyu committed
51
    reset: function () {
yanzhongrong's avatar
yanzhongrong committed
52 53 54
      clearTimeout(this.timeoutObj)
      this.start()
    },
dupengyu's avatar
dupengyu committed
55 56
    start: function () {
      this.timeoutObj = setInterval(function () {
neogcg's avatar
neogcg committed
57
        // if(websocket.readyState==1){
yanzhongrong's avatar
yanzhongrong committed
58 59
        websocket.send("HeartBeat")
        console.info("Websocket发送心跳:HeartBeat")
neogcg's avatar
neogcg committed
60
        // }
dupengyu's avatar
dupengyu committed
61

yanzhongrong's avatar
yanzhongrong committed
62
      }, this.timeout)
neogcg's avatar
neogcg committed
63
    },
yanzhongrong's avatar
yanzhongrong committed
64
  }
neogcg's avatar
neogcg committed
65
}
yanzhongrong's avatar
yanzhongrong committed
66 67
//消息处理
export function receiveMessage(message) {
dupengyu's avatar
dupengyu committed
68 69 70 71 72 73
  // 消息接收后处理逻辑 接口访问 
  if (message != '连接成功') {
    let obj = JSON.parse(JSON.parse(message))
    EventBus.$emit('dialogAlarm', obj)
    EventBus.$emit('autioPlay', obj)
  }
yanzhongrong's avatar
yanzhongrong committed
74
}