您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页前端websocket建立以及通信

前端websocket建立以及通信

来源:化拓教育网
前端websocket建⽴以及通信

// 页⾯建⽴socket socket(){

var that = this;

let token = localStorage.getItem(\"Admin-Token\"); let username = localStorage.getItem(\"user\"); let id = that.deviceId;

that.websocket = new window.WebSocket(`${process.env.VUE_APP_websocketUrl}/websocket/${username}/${id}`,[token]); //连接关闭的回调⽅法

that.websocket.onclose = that.websocketonclose; // 连接错误

that.websocket.onerror = that.websocketonerror; //连接成功时的回调⽅法

that.websocket.onopen = that.websocketonopen; //接收到消息的回调⽅法

that.websocket.onmessage = that.websocketonmessage; },

// socket关闭

websocketonclose(){

console.log('连接关闭!') },

// socket连接错误 websocketonerror(){

console.log('连接错误!'); var that = this; that.socket(); },

// 连接成功时的回调⽅法 websocketonopen(){ console.log('连接成功!') var that = this;

let actions = {\"id\":that.deviceId};

that.websocketsend(JSON.stringify(actions)); },

//接收到消息的回调⽅法 websocketonmessage(msg){

console.log(msg.data, 'ws:data') },

//数据发送

websocketsend(Data){

this.websock.send(Data); },

  

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务