initRealTimeChartOption(row) {
console.log(row,'3333')
},
initWebSocket(row) {
this.dialogRestart = true
//初始化weosocket
const wsurl = process.env.VUE_APP_BASE_API_CQ + `/ws/1`;
this.websock = new WebSocket(wsurl); //实例化socket wsurl是连接地址
this.websock.onopen = this.websocketonopen(row); // 监听socket连接
this.websock.onerror = this.websocketonerror; // 监听socket错误信息
this.websock.onmessage = this.websocketonmessage; // 数据接收
this.websock.onclose = this.websocketclose; //关闭连接
},
websocketonopen(row) {
console.log(this.websock.onopen,'oooooooooooooo')
console.log("WebSocket连接成功");
this.initRealTimeChartOption(row);//连接成功之后调用接口
// this.websocketsend();
},
websocketonerror(e) {
//错误
console.log("WebSocket连接发生错误:", e);
},
websocketonmessage(e) {
console.log(e,'1111111111111')
//数据接收
const redata = JSON.parse(e.data);
console.log(redata)
}
//注意:长连接我们是后台直接1秒推送一条数据,
//但是点击某个列表时,会发送给后台一个标识,后台根据此标识返回相对应的数据,
//这个时候数据就只能从一个出口出,所以让后台加了一个键,例如键为1时,是每隔1秒推送的数据,为2时是发送标识后再推送的数据,以作区分
Object.prototype.toString.call(redata) == "[object Object]" && Object.keys(redata).length > 0 && console.log("接收数据:", redata);
},
websocketsend(e) {
console.log(e,'222')
//数据发送
// this.websock.send(111)
},
websocketclose(e) {
//关闭
console.log("connection closed (" + e.code + ")");
},
websock的简单使用
最新推荐文章于 2024-07-23 12:35:43 发布