0
点赞
收藏
分享

微信扫一扫

websocket 封装及使用

草原小黄河 2022-04-21 阅读 104
websocket

简单了解一下 websocket

以下是创建一个新的webSocket对象的API:

var Socket = new WebSocket(url, [protocal] );

webSocket属性:

属性说明
Socket.readyStatereadyState的代表的ReadOnly属性的连接状态。 它可以有以下值: 一个0值表示该连接尚未建立。 值为1表示连接建立和沟通是可能的。 值为2表示连接是通过将结束握手。 值为3表示连接已关闭或无法打开。
Socket.bufferedAmount读属性的bufferedAmount代表文本的字节数,utf - 8的排队使用send()方法。

webSocket事件:

事件处理程序说明
openSocket.onopen此事件发生在套接字建立连接。
messageSocket.onmessage此事件发生时,客户端收到来自服务器的数据。
errorSocket.onerror此事件发生时有任何通信错误。
closeSocket.onclose此事件发生在连接关闭。

webSocket方法:

方法说明
Socket.send()send(data)方法用来连接传输数据。
Socket.close()close()方法将被用于终止任何现有的连接。

简单例子

<template>
  <div class="test">

  </div>
</template>

<script>
  export default {
    name : 'test',
    data() {
      return {
        websock: null,
      }
    },
    created() {
      this.initWebSocket();
    },
    destroyed() {
      this.websock.close() //离开路由之后断开websocket连接
    },
    methods: {
      initWebSocket(){ //初始化weosocket
        const wsuri = "ws://127.0.0.1:8080";
        this.websock = new WebSocket(wsuri);
        this.websock.onmessage = this.websocketonmessage;
        this.websock.onopen = this.websocketonopen;
        this.websock.onerror = this.websocketonerror;
        this.websock.onclose = this.websocketclose;
      },
      websocketonopen(){ //连接建立之后执行send方法发送数据
        let actions = {"test":"12345"};
        this.websocketsend(JSON.stringify(actions));
      },
      websocketonerror(){//连接建立失败重连
        this.initWebSocket();
      },
      websocketonmessage(e){ //数据接收
        const redata = JSON.parse(e.data);
      },
      websocketsend(Data){//数据发送
        this.websock.send(Data);
      },
      websocketclose(e){  //关闭
        console.log('断开连接',e);
      },
    },
  }
</script>
<style lang='less'>
 
</style>

封装 websocke

let Socket = ''
let setIntervalWesocketPush = null

/**
 * 建立websocket连接
 * @param {string} url ws地址
 */
export const createSocket = url => {
  Socket && Socket.close()
  if (!Socket) {
    console.log('建立websocket连接')
    Socket = new WebSocket(url)
    Socket.onopen = onopenWS
    Socket.onmessage = onmessageWS
    Socket.onerror = onerrorWS
    Socket.onclose = oncloseWS
  } else {
    console.log('websocket已连接')
  }
}

/**打开WS之后发送心跳 */
const onopenWS = () => {
  sendPing()
}

/**连接失败重连 */
const onerrorWS = () => {
  Socket.close()
  clearInterval(setIntervalWesocketPush)
  console.log('连接失败重连中')
  if (Socket.readyState !== 3) {
    Socket = null
    createSocket()
  }
}

/**WS数据接收统一处理 */
const onmessageWS = e => {
  window.dispatchEvent(new CustomEvent('onmessageWS', {
    detail: {
      data: e.data
    }
  }))
}

/**
 * 发送数据但连接未建立时进行处理等待重发
 * @param {any} message 需要发送的数据
 */
const connecting = message => {
  setTimeout(() => {
    if (Socket.readyState === 0) {
      connecting(message)
    } else {
      Socket.send(JSON.stringify(message))
    }
  }, 1000)
}

/**
 * 发送数据
 * @param {any} message 需要发送的数据
 */
export const sendWSPush = message => {
  if (Socket !== null && Socket.readyState === 3) {
    Socket.close()
    createSocket()
  } else if (Socket.readyState === 1) {
    Socket.send(JSON.stringify(message))
  } else if (Socket.readyState === 0) {
    connecting(message)
  }
}

/**断开重连 */
const oncloseWS = () => {
  clearInterval(setIntervalWesocketPush)
  console.log('websocket已断开....正在尝试重连')
  if (Socket.readyState !== 2) {
    Socket = null
    createSocket()
  }
}
/**发送心跳
 * @param {number} time 心跳间隔毫秒 默认5000
 * @param {string} ping 心跳名称 默认字符串ping
 */
export const sendPing = (time = 5000, ping = 'ping') => {
  clearInterval(setIntervalWesocketPush)
  Socket.send(ping)
  setIntervalWesocketPush = setInterval(() => {
    Socket.send(ping)
  }, time)
}

websocket 使用

npm install @sven0706/websocket -S
// 在main.js或需要使用的地方引入并建立连接
import { createSocket } from '@sven0706/websocket'
createSocket('wss://api.baidu.com')


// 发送消息
import { sendWSPush } from '@sven0706/websocket'
sendWSPush(data)

// 接收消息
const getsocketData = e => {  // 创建接收消息函数
  const data = e && e.detail.data
  console.log(data)
}
// 注册监听事件
window.addEventListener('onmessageWS', getsocketData)

// 在需要的时候卸载监听事件,比如离开页面
window.removeEventListener('onmessageWS', getsocketData)

API

/**
 * 建立websocket连接
 * @param {string} url ws地址
 */
createSocket(url)

/**
 * 发送数据
 * @param {any} message 需要发送的数据
 */
 sendWSPush(message)

/**发送心跳
 * @param {number} time 心跳间隔毫秒 默认5000
 * @param {string} ping 心跳名称 默认字符串ping
 */
 sendPing()
举报

相关推荐

0 条评论