首先创建一个socket.ts文件封装公共方法
import i18n from '@/locales'
import store from '@/store'
import {
ElMessage } from 'element-plus'
import {
Base64 } from 'js-base64'
const language = store.state.users.language
const token = store.state.users.authorization
interface socket {
websocket: any
connectURL: string
socket_open: boolean
hearbeat_timer: any
hearbeat_interval: number
is_reonnect: boolean
reconnect_count: number
reconnect_current: number
ronnect_number: number
reconnect_timer: any
reconnect_interval: number
init: (receiveMessage: Function | null) => any
receive: (message: any) => void
heartbeat: () => void
send: (data: any, callback?: any) => void
close: () => void
reconnect: () => void
}
const socket: socket = {
websocket: null,
connectURL: `${
process.env.VUE_APP_SOCEKT_URL}/websocket/v1/${
language