165 lines
2.8 KiB
JavaScript
165 lines
2.8 KiB
JavaScript
//Socket连接
|
|
|
|
const targetUrl = process.env.BASE_TARGET_URL.replace("/", "");
|
|
|
|
|
|
import EventBus from './eventBus.js'
|
|
let ws = null
|
|
let heartTimer = null
|
|
let timer = null
|
|
let lockReconnect = false
|
|
let token = null
|
|
const reconnectCountMax = 200
|
|
let reconnectCount = 0
|
|
let isConnect = false
|
|
let SERVER_IP = targetUrl.replaceAll("\"", "");
|
|
|
|
//服务器推送消息
|
|
function response(event) {
|
|
if (event.type !== 'message') {
|
|
onCloseHandler()
|
|
return
|
|
}
|
|
let wsContent
|
|
try {
|
|
wsContent = JSON.parse(event.data)
|
|
} catch {
|
|
onCloseHandler()
|
|
return
|
|
}
|
|
if (wsContent.type) {
|
|
if (wsContent.data && wsContent.data.code === -1) {
|
|
onCloseHandler()
|
|
} else {
|
|
switch (wsContent.type) {
|
|
//心跳回复
|
|
case 'Heartbeat-Reply': {
|
|
EventBus.emit('Heartbeat-Reply', wsContent.content)
|
|
break
|
|
}
|
|
//在线用户推送
|
|
case 'Online-UserIds': {
|
|
EventBus.emit('Online-UserIds', wsContent.content)
|
|
break
|
|
}
|
|
//接收消息
|
|
case 'Message-Receive': {
|
|
EventBus.emit('Message-Receive', wsContent.content)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
onCloseHandler()
|
|
}
|
|
}
|
|
|
|
|
|
//连接Socket
|
|
function connect(tokenStr) {
|
|
if (isConnect || ws) return
|
|
isConnect = true
|
|
token = tokenStr
|
|
try {
|
|
const wsIp = import.meta.env.VITE_SOCKET_URL
|
|
ws = new WebSocket(`ws://${SERVER_IP}:18666/infra/ws?token=${token}`)
|
|
|
|
ws.onopen = () => {
|
|
console.log('Socket连接成功!')
|
|
clearTimer()
|
|
sendHeartPack()
|
|
}
|
|
|
|
ws.onmessage = response
|
|
ws.onclose = onCloseHandler
|
|
ws.onerror = onCloseHandler
|
|
} catch {
|
|
onCloseHandler()
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//发送消息
|
|
function send(msg) {
|
|
if (ws && ws.readyState === WebSocket.OPEN) ws.send(msg)
|
|
}
|
|
|
|
|
|
|
|
//开启心跳,上报在线状态
|
|
const sendHeartPack = () => {
|
|
heartTimer = setInterval(() => {
|
|
let data = {
|
|
msgType:"Heartbeat",
|
|
msgContent:"我在线,时间:"+ new Date().getTime()
|
|
}
|
|
let wsdata = {
|
|
type: "app-message-send",
|
|
content: JSON.stringify(data)
|
|
}
|
|
send(JSON.stringify(wsdata));
|
|
}, 10000)
|
|
}
|
|
|
|
|
|
|
|
const onCloseHandler = () => {
|
|
clearHeartPackTimer()
|
|
if (ws) {
|
|
ws.close()
|
|
ws = null
|
|
}
|
|
isConnect = false
|
|
if (lockReconnect) return
|
|
lockReconnect = true
|
|
if (timer) {
|
|
clearTimeout(timer)
|
|
timer = null
|
|
}
|
|
if (reconnectCount >= reconnectCountMax) {
|
|
reconnectCount = 0
|
|
return
|
|
}
|
|
if (token) {
|
|
timer = setTimeout(() => {
|
|
connect(token)
|
|
reconnectCount++
|
|
lockReconnect = false
|
|
}, 5000)
|
|
}
|
|
}
|
|
|
|
|
|
//清除心跳定时器
|
|
const clearHeartPackTimer = () => {
|
|
console.log('Socket连接关闭中')
|
|
if (heartTimer) {
|
|
clearInterval(heartTimer)
|
|
heartTimer = null
|
|
}
|
|
}
|
|
|
|
//清除Timer定时器
|
|
const clearTimer = () => {
|
|
if (timer) {
|
|
clearInterval(timer)
|
|
timer = null
|
|
}
|
|
}
|
|
|
|
//断开Socket连接
|
|
const disConnect = () => {
|
|
clearHeartPackTimer()
|
|
token = null
|
|
if (ws) {
|
|
ws.close()
|
|
ws = null
|
|
}
|
|
isConnect = false
|
|
}
|
|
|
|
export default {
|
|
connect,
|
|
disConnect
|
|
} |