欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js封裝一個(gè)websocket實(shí)現(xiàn)及使用詳解

 更新時(shí)間:2023年07月04日 11:34:11   作者:Sven0706  
這篇文章主要為大家介紹了js封裝一個(gè)websocket實(shí)現(xiàn)示例及使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

創(chuàng)建websocket.js

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已連接')
  }
}
/**打開(kāi)WS之后發(fā)送心跳 */
const onopenWS = () => {
  sendPing()
}
/**連接失敗重連 */
const onerrorWS = () => {
  Socket.close()
  clearInterval(setIntervalWesocketPush)
  console.log('連接失敗重連中')
  if (Socket.readyState !== 3) {
    Socket = null
    createSocket()
  }
}
/**WS數(shù)據(jù)接收統(tǒng)一處理 */
const onmessageWS = e => {
  window.dispatchEvent(new CustomEvent('onmessageWS', {
    detail: {
      data: e.data
    }
  }))
}
/**
 * 發(fā)送數(shù)據(jù)但連接未建立時(shí)進(jìn)行處理等待重發(fā)
 * @param {any} message 需要發(fā)送的數(shù)據(jù)
 */
const connecting = message => {
  setTimeout(() => {
    if (Socket.readyState === 0) {
      connecting(message)
    } else {
      Socket.send(JSON.stringify(message))
    }
  }, 1000)
}
/**
 * 發(fā)送數(shù)據(jù)
 * @param {any} message 需要發(fā)送的數(shù)據(jù)
 */
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)
  }
}
/**斷開(kāi)重連 */
const oncloseWS = () => {
  clearInterval(setIntervalWesocketPush)
  console.log('websocket已斷開(kāi)....正在嘗試重連')
  if (Socket.readyState !== 2) {
    Socket = null
    createSocket()
  }
}
/**發(fā)送心跳
 * @param {number} time 心跳間隔毫秒 默認(rèn)5000
 * @param {string} ping 心跳名稱(chēng) 默認(rèn)字符串ping
 */
export const sendPing = (time = 5000, ping = 'ping') => {
  clearInterval(setIntervalWesocketPush)
  Socket.send(ping)
  setIntervalWesocketPush = setInterval(() => {
    Socket.send(ping)
  }, time)
}

下載 (也可復(fù)制源碼,放在本地,使用方式相同)

npm install @sven0706/websocket -S

使用

// 在main.js或需要使用的地方引入并建立連接
import { createSocket } from '@sven0706/websocket'
createSocket('wss://api.baidu.com')
// 發(fā)送消息
import { sendWSPush } from '@sven0706/websocket'
sendWSPush(data)
// 接收消息
const getsocketData = e => {  // 創(chuàng)建接收消息函數(shù)
  const data = e && e.detail.data
  console.log(data)
}
// 注冊(cè)監(jiān)聽(tīng)事件
window.addEventListener('onmessageWS', getsocketData)
// 在需要的時(shí)候卸載監(jiān)聽(tīng)事件,比如離開(kāi)頁(yè)面
window.removeEventListener('onmessageWS', getsocketData)

API

僅三個(gè)api, 且一般只需要用到`createSocket`, `sendWSPush`

/**
 * 建立websocket連接
 * @param {string} url ws地址
 */
createSocket(url)
/**
 * 發(fā)送數(shù)據(jù)
 * @param {any} message 需要發(fā)送的數(shù)據(jù)
 */
 sendWSPush(message)
/**發(fā)送心跳
 * @param {number} time 心跳間隔毫秒 默認(rèn)5000
 * @param {string} ping 心跳名稱(chēng) 默認(rèn)字符串ping
 */
 sendPing()

以上就是js封裝一個(gè)websocket實(shí)現(xiàn)及使用詳解的詳細(xì)內(nèi)容,更多關(guān)于js封裝websocket的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論