|
@@ -17,9 +17,82 @@ export default {
|
|
|
titleTemplate: title => {
|
|
|
return title ? `${title} - ${process.env.VUE_APP_TITLE}` : process.env.VUE_APP_TITLE
|
|
|
},
|
|
|
- websocket:null
|
|
|
+ websocket:null,
|
|
|
+ interval:null,
|
|
|
+ index:null,
|
|
|
+ heartbeat : null,
|
|
|
+ retryStatus:false
|
|
|
}
|
|
|
},
|
|
|
+ methods: {
|
|
|
+ initWebSocket() {
|
|
|
+ console.log("正在进行连接设备日志反馈WS")
|
|
|
+ let url = process.env.VUE_APP_BASE_API.replace("https://", "wss://").replace("http://", "ws://") + "/websocket/device/"+ this.index;
|
|
|
+ this.websocket = new WebSocket(url);
|
|
|
+ this.websocket.onopen = this.websocketonopen;
|
|
|
+ this.websocket.onmessage = this.websocketonmessage;
|
|
|
+ this.websocket.onerror = this.websocketonerror;
|
|
|
+ // this.websocket.
|
|
|
+ this.websocket.onclose = this.websocketclose;
|
|
|
+ },
|
|
|
+ // 连接成功后调用
|
|
|
+ websocketonopen: function () {
|
|
|
+ console.log("WebSocket连接成功");
|
|
|
+ clearInterval(this.interval)
|
|
|
+ const websocket = this.websocket;
|
|
|
+ this.retryStatus =false;
|
|
|
+ this.heartbeat = setInterval(function () {
|
|
|
+ websocket.send("T")
|
|
|
+ }, 5000);
|
|
|
+ },
|
|
|
+ // 发生错误时调用
|
|
|
+ websocketonerror: function (e) {
|
|
|
+ console.log("WebSocket连接发生错误");
|
|
|
+ clearInterval(this.heartbeat)
|
|
|
+ if (!this.retryStatus){
|
|
|
+ this.interval = setInterval(()=>{
|
|
|
+ this.initWebSocket()
|
|
|
+ },5000)
|
|
|
+ this.retryStatus = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ },
|
|
|
+ websocketonmessage: function (e) {
|
|
|
+ const notify = JSON.parse(e.data)
|
|
|
+ if (notify.type=="notify"){
|
|
|
+ if (notify.content.status=="1"){
|
|
|
+ this.$notify.info({
|
|
|
+ title: notify.content.deviceName,
|
|
|
+ message: notify.content.content,
|
|
|
+ showClose: false
|
|
|
+ });
|
|
|
+ }else {
|
|
|
+ this.$notify.error({
|
|
|
+ title: notify.content.deviceName,
|
|
|
+ message: notify.content.content,
|
|
|
+ duration: 0
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // 关闭连接时调用
|
|
|
+ websocketclose: function (e) {
|
|
|
+ console.log("connection closed (" + e.code + ")");
|
|
|
+ clearInterval(this.heartbeat)
|
|
|
+ if (!this.retryStatus){
|
|
|
+ this.interval = setInterval(()=>{
|
|
|
+ this.initWebSocket()
|
|
|
+ },5000)
|
|
|
+ this.retryStatus = true;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ },
|
|
|
+ created() {
|
|
|
+ this.index = Math.round(Math.random() * 100000000)
|
|
|
+ setTimeout(()=>{
|
|
|
+ this.initWebSocket()
|
|
|
+ },5000)
|
|
|
+ }
|
|
|
};
|
|
|
</script>
|
|
|
<style scoped>
|