diff --git a/src/components/EWebSocket/index.js b/src/components/EWebSocket/index.js new file mode 100644 index 00000000..bf9b71cc --- /dev/null +++ b/src/components/EWebSocket/index.js @@ -0,0 +1,5 @@ +import EWebSocket from './src/basic/index'; + +export default { + EWebSocket +} diff --git a/src/components/EWebSocket/src/basic/index.js b/src/components/EWebSocket/src/basic/index.js new file mode 100644 index 00000000..469d713d --- /dev/null +++ b/src/components/EWebSocket/src/basic/index.js @@ -0,0 +1,8 @@ + +import EWebSocket from "./webSocket" + +EWebSocket.install = function install(Vue) { + Vue.component(EWebSocket.name, EWebSocket); +}; + +export default EWebSocket diff --git a/src/components/EWebSocket/src/basic/webSocket.js b/src/components/EWebSocket/src/basic/webSocket.js new file mode 100644 index 00000000..672f85b7 --- /dev/null +++ b/src/components/EWebSocket/src/basic/webSocket.js @@ -0,0 +1,94 @@ + +export default { + name: 'EWebSocket', + props: { + wsServiceUrl: { + type: String, + default: '' + }, + closeSleepTime: { + type: Number, + default: 10000 + } + }, + data() { + return { + stompClient: null, + socket_flag: false, + timeout_flag: null + } + }, + watch: { + wsServiceUrl() { + if (this.stompClient) { + this.closeSocket(); + } + this.connection(); + } + }, + methods: { + /** + * 创建 ws 对象 + * @returns + */ + connection() { + if (this.stompClient) { + return; + } + if (!this.wsServiceUrl) { + return; + } + this.stompClient = new WebSocket(`${this.wsServiceUrl}`); + this.stompClient.onopen = this.socket_open; + this.stompClient.onmessage = this.socket_message; + this.stompClient.onclose = this.socket_close; + }, + socket_open(evt) { + console.log("ws-open:", evt); + this.$emit('open', evt) + }, + /** + * ws message + * @param {*} evt + */ + socket_message(evt) { + console.log("wsljcg:=", evt); + const data = JSON.parse(evt.data); + this.$emit('message_http', evt); + this.$emit('message', data) + }, + /** + * ws close + * @param {*} e + */ + socket_close(e) { + this.stompClient = null; + if (this.socket_flag) { + this.socket_flag = false; + let _this = this; + _this.timeout_flag = setTimeout(function () { + _this.socket_flag = true; + _this.connection(); + }, Number(this.closeSleepTime)); + } + }, + /** + * ws 前端 销毁 + */ + closeSocket() { + if (this.stompClient) { + console.log('ws-close!!!') + this.stompClient.close(); + } + this.socket_flag = false; + this.stompClient = null; + clearTimeout(this.timeout_flag); + }, + }, + render() { + return () + }, + destroyed() { + this.closeSocket(); + }, +} diff --git a/src/components/EWebSocket/src/index b/src/components/EWebSocket/src/index new file mode 100644 index 00000000..e69de29b diff --git a/src/components/ParamWrap/deviceParam.vue b/src/components/ParamWrap/deviceParam.vue index ab901afb..19e48260 100644 --- a/src/components/ParamWrap/deviceParam.vue +++ b/src/components/ParamWrap/deviceParam.vue @@ -10,11 +10,22 @@ - + + > - 暂无参数信息 @@ -41,34 +51,34 @@ export default { data() { return { list: [], - ruleForm: {} + ruleForm: {}, }; }, methods: { getParams() { - this.list = [] - const newArr = getTypeParam(this.typeKeys) + this.list = []; + const newArr = getTypeParam(this.typeKeys); if (newArr) { - newArr.forEach(v => { - this.list.push(Object.assign({}, v)) + newArr.forEach((v) => { + this.list.push(Object.assign({}, v)); }); } this.$forceUpdate(); }, setList(data) { - this.list = data + this.list = data; }, getResult() { return this.list; - } + }, }, watch: { typeKeys(val) { if (val) { this.getParams(); } - } - } + }, + }, };