cmc-web/packages/common/hooks/useWebsocket.ts

23 lines
609 B
TypeScript
Raw Permalink Normal View History

2024-08-20 12:11:31 +00:00
import { onUnmounted, ref } from 'vue'
import WebSocket from 'cmp-socket'
import { getToken } from 'utils/auth'
2024-08-21 01:17:14 +00:00
export default function (onmessage: { (data: any): void }) {
2024-08-20 12:11:31 +00:00
const protocol = location.protocol === 'http:' ? 'ws' : 'wss'
let webSocket = new WebSocket({
url: `${protocol}://${location.host}/api/sms/messageService`,
pingMsg: 'HeartBeat',
reConnectNum: 5,
params: getToken()
})
if (onmessage && typeof onmessage === 'function') {
webSocket.onmessage = onmessage
}
onUnmounted(() => {
webSocket.Destroy()
webSocket = null
})
return {
webSocket
}
}