websocket实战

92 阅读1分钟

1,安装依赖

npm install ws

2,页面实例对应编码

let that = this;
let url = window.location.hostname;
const ws = new WebSocket(`ws://${url}${VUE_APP_API_DSP}messageListener
ws.onopen = function(e) {    console.log("判断是否连接成功");};    
ws.onmessage = function(e) {      
console.log("返回数据",e);      
if (!Number(JSON.parse(e.data).data)) return false;      
let obj = JSON.parse(e.data);      
that.$store.commit("changeMsgNum", obj.data);    
};