| | |
| | | * 发起websocket连接 |
| | | * @param {Object} agentData 需要向后台传递的参数数据 |
| | | */ |
| | | function websocketSend (agentData) { |
| | | export function websocketSend (agentData) { |
| | | // 加延迟是为了尽量让ws连接状态变为OPEN |
| | | setTimeout(() => { |
| | | // 添加状态判断,当为OPEN时,发送消息 |
| | |
| | | // 发给后端的数据需要字符串化 |
| | | websock.send(JSON.stringify(agentData)) |
| | | console.log('连接成功'); |
| | | |
| | | } |
| | | if (websock.readyState === websock.CLOSED) { // websock.CLOSED = 3 |
| | | console.log('websock.readyState=3') |
| | |
| | | } |
| | | // 建立ws连接 |
| | | function websocketOpen (e) { |
| | | // console.log('ws连接成功') |
| | | console.log('ws连接成功') |
| | | } |
| | | |
| | | // 初始化weosocket |
| | |
| | | // const requstWsUrl = wsUrl + '?' + token |
| | | const requstWsUrl = wsUrl |
| | | websock = new WebSocket(requstWsUrl) |
| | | |
| | | websock.onmessage = function (e) { |
| | | console.log('websocketonMessage'); |
| | | websocketonmessage(e) |
| | | } |
| | | |
| | | websock.onopen = function () { |
| | | websocketOpen() |
| | | } |