vue使用WebSocket

    技术2022-07-11  107

    mounted(){ this.init() }, methods:{ init: function () { // console.log("typeof",typeof(WebSocket)) if(typeof(WebSocket) === "undefined"){ alert("您的浏览器不支持WebSocket") }else{ // console.log("支持WebSocket") let ws='' // 实例化socket ws = new WebSocket('ws://121.40.165.18:8800') // 监听socket连接 ws.onopen = this.open // 监听socket消息 // ws.onmessage = function (MessageEvent) { // this.total=MessageEvent.data // console.log("推送内容--",this.total) // } ws.onmessage = this.msg // 监听socket错误信息 ws.onerror = this.error } }, open: function () { console.log("socket连接成功") }, msg:function(MessageEvent){ console.log("MessageEvent",MessageEvent.data) this.total=MessageEvent.data }, error: function () { Message({ message: "ws连接错误", type: 'error', duration: 5 * 1000 }); // console.log("连接错误") }, send: function () { this.socket.send(params) }, close: function () { console.log("socket已经关闭") }, }
    Processed: 0.009, SQL: 9