在该项目中websocket 就是一个接口,通过字符串的拼接拼一些参数
let url = `${wsUrl}/api/io/lineup/${id}/${uid}?TENANT_CODE=${tenantCode}&USERNAME=${username}&LAB_LESSION_UID=${labLessionUid}&LAB_LESSION_NAME=${labLessonName}&EXP_UID=${expUid}&EXP_NAME=${expName}`
this.socket = new WebSocket(url)
调用了websocket的四个状态,
// 当连接关闭时要调用的事件监听器。
this.socket.onclose = ({ reason }) => {
if (reason)
this.$message.warning(reason)
}
// 发生错误时要调用的事件监听器。
this.socket.onerror = (event) => {
window.console.log('onerror', event)
this.$message.error('WebSocket 连接异常!')
}
// 从服务器接收到消息时要调用的事件监听器。
this.socket.onmessage = ({ data }) => {
//一些业务逻辑之类的,因为只有当WebSocket 返回消息才能表示通信成功了,之前可以监听到连接成功,但是没有通信,就是可能是参数传错了.
}
// 打开连接时要调用的事件监听器
this.socket.onopen = e => {
window.console.log('onopen', e, this.socket)
}
/**
* WebSocket。请求处理当前WebSocket的连接状态
* 0连接插座已经创建。连接尚未打开。
* 1打开连接已经打开,可以通信了。
* 2正在关闭连接。
* 3关闭连接已关闭或无法打开。
*/