cmc-web/packages/common/mixins/webSocket.js

30 lines
699 B
JavaScript
Raw Permalink Normal View History

2024-08-20 12:11:31 +00:00
import WebSocket from 'cmp-socket'
import { getToken } from 'utils/auth'
export default {
data() {
return {
webSocket: ''
}
},
created() {
const protocol = location.protocol === 'http:' ? 'ws' : 'wss'
this.webSocket = new WebSocket({
url: `${protocol}://${location.host}/api/sms/messageService`,
commonFun: this.messageCommonFun,
pingMsg: 'HeartBeat',
reConnectNum: 5,
params: getToken()
})
if (this.onmessage && typeof this.onmessage === 'function') {
this.webSocket.onmessage = this.onmessage
}
},
destroyed() {
this.webSocket.Destroy()
this.webSocket = null
},
methods: {
messageCommonFun() {}
}
}