先下载: npm install websocket
配置文件 websocket.ts
/* eslint-disable @typescript-eslint/no-explicit-any */
let websock: any = null // 定义websock变量
// const url = 'ws://127.0.0.1:8080'
const url = 'ws://192.168.3.136:8082/zygk/ws/init.ws?userId=1' // 设定链接地址
// const websocketonopen = () => { // 连接建立之后执行send方法发送数据
// // const actions = { test: '12345' }
// // websocketsend(JSON.stringify(actions))
// }
const websocketonerror = () => { // 连接建立失败重连
initWebSocket()
}
const websocketonmessage = (e: any) => { // 数据接收
window.dispatchEvent(new CustomEvent('onmessageWS', {
detail: {
data: JSON.parse(e.data)
}
}))
// const redata = JSON.parse(e.data)
// // const redata = e
// console.log(redata)
}
// const websocketsend = (Data: any) => {