WebSocket: Get data in real time for use in VUE
realTimeGetLog(){ var socket; Let that=this if(typeof(WebSocket) == "undefined") {console.log(" your browser does not support WebSocket"); } else {/ / implemented WebSocket object, specify the server address and port to connect Connect the socket = new WebSocket (" ws: / / 192.168.12.50:8080 / WebSocket "); Onopen = function() {console.log("Socket open "); //socket.send(" This is the message from the client "+ location.href + new Date()); }; Onmessage = function(MSG) {// Trigger the event here,dosomething.... console.log(msg.data); contLog(Object.assign({})).then(res => { if (res.code == '200') { that.rizhilist = res.data; } else { this.$message.error(res.msg); }}) // The discovery message enters the start processing front-end trigger logic}; Onclose = function() {console.log(" socket closed "); }; Onerror = function() {alert(" socket error "); // Try to refresh the page}}},Copy the code
In Mounted, call realTimeGetLog(). The WebSocket is automatically triggered