This commit is contained in:
2021-10-28 15:32:09 +08:00
commit 0a73304eea
475 changed files with 122605 additions and 0 deletions

50
src/api/ws.js Normal file
View File

@@ -0,0 +1,50 @@
export default (obj) => {
// 'ws://192.168.37.82:8081/dump-api/webSocketServer'
const websocket = new WebSocket(obj.link)
// 连接发生错误的回调方法
websocket.onerror = function () {
if (obj.errCb) {
obj.errCb()
}
}
// 连接成功建立的回调方法
websocket.onopen = () => {
if (obj.linkCb) {
obj.linkCb()
}
}
// 接收到消息的回调方法
websocket.onmessage = (e) => {
if (obj.cb) {
obj.cb(e.data)
}
}
// 连接关闭的回调方法
websocket.onclose = function () {
// 视情况是否进行提示
console.log('WebSocket连接关闭,请检查网络设置')
}
// 监听窗口关闭事件当窗口关闭时主动去关闭websocket连接防止连接还没断开就关闭窗口server端会抛异常。
window.onbeforeunload = function () {
closeWebSocket()
}
// 关闭WebSocket连接
function closeWebSocket () {
// 关闭操作 待补充
websocket.close()
}
return {
instance: websocket,
send: (msg) => {
websocket.send(msg)
},
close: closeWebSocket
}
}