websocket.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // export default {
  2. // ws:{},
  3. // setWs: function(newWs) {
  4. // this.ws = newWs
  5. // }
  6. // }
  7. // // <!--引入store,用于管理socket推送来的消息-->
  8. // import store from '../store'
  9. // // <!--封装websocket对象-->
  10. // const WS = {
  11. // $ws:null, // webscoket实例
  12. // wsUrl: 'http://132.232.92.186:8888/api/chatHub', // websocket链接地址
  13. // // <!--初始化webSocket-->
  14. // createWS:function(){
  15. // if('WebSocket' in window){
  16. // this.$ws = new WebSocket(this.wsURl)
  17. // this.$ws.onopen = this.wsOpen
  18. // this.$ws.onmessage = this.wsMessage
  19. // this.$ws.onerror = this.wsError
  20. // this.$ws.onclose = this.wsClose
  21. // } else {
  22. // alert('当前浏览器不支持webSocket')
  23. // }
  24. // },
  25. // // <!--webSocket 打开-->
  26. // wsOpen:function() {
  27. // this.$ws.send('连接成功')
  28. // console.log('== websocket open ==')
  29. // // <!--开始心跳-->
  30. // heartBeat.start()
  31. // },
  32. // // <!--websocket 接收到服务器消息-->
  33. // wsMessage:function(msg) {
  34. // console.log('== websocket message ==', msg)
  35. // // <!--接受到消息,重置心跳-->
  36. // heartBeat.reset()
  37. // store.commit('SET_WS_MSG', msg.data)
  38. // },
  39. // // <!--websocket 发生错误-->
  40. // wsError: function(err){
  41. // console.log('== websocket error ==', err)
  42. // },
  43. // // <!--websocket 关闭连接-->
  44. // wsClose: function(event){
  45. // console.log('== websocket close ==', event)
  46. // }
  47. // }
  48. // // <!--webSocket 心跳-->
  49. // const heartBeat = {
  50. // timeout:3000, // 心跳重连时间
  51. // timeoutObj:null, // 定时器
  52. // reset:function(){
  53. // clearInterval(this.timeoutObj)
  54. // console.log('websocket 心跳')
  55. // WS.start()
  56. // },
  57. // start:function(){
  58. // this.timeoutObj = setTimeout(function(){
  59. // if(WS.$ws.readyState === 1){
  60. // WS.$ws.send('HeartBeat')
  61. // }
  62. // },this.timeout)
  63. // }
  64. // }
  65. // export default WS