|
@@ -20,55 +20,6 @@ export default {
|
|
|
websocket:null
|
|
|
}
|
|
|
},
|
|
|
- created() {
|
|
|
- setTimeout(() => {
|
|
|
- this.initWebSocket()
|
|
|
-
|
|
|
- }, 2000)
|
|
|
- },
|
|
|
- methods: {
|
|
|
- initWebSocket() {
|
|
|
- let name = this.$store.state.user.name
|
|
|
- let url = process.env.VUE_APP_BASE_API.replace("https://", "wss://").replace("http://", "ws://") + "/websocket/device/" + name;
|
|
|
- this.websocket = new WebSocket(url);
|
|
|
- this.websocket.addEventListener('beforeSend', function (event) {
|
|
|
- event.target.setRequestHeader('Authorization', 'Bearer ' + this.$store.state.user.token);
|
|
|
- });
|
|
|
- this.websocket.onopen = this.websocketonopen;
|
|
|
- this.websocket.onerror = this.websocketonerror;
|
|
|
- this.websocket.onmessage = this.websocketonmessage;
|
|
|
- this.websocket.onclose = this.websocketclose;
|
|
|
- },
|
|
|
- // 连接成功后调用
|
|
|
- websocketonopen: function () {
|
|
|
- console.log("WebSocket连接成功");
|
|
|
- },
|
|
|
- // 发生错误时调用
|
|
|
- websocketonerror: function (e) {
|
|
|
- console.log("WebSocket连接发生错误");
|
|
|
- },
|
|
|
- websocketonmessage: function (e) {
|
|
|
- const notify = JSON.parse(e.data)
|
|
|
- console.log(e.data)
|
|
|
- if (notify.status=="1"){
|
|
|
- this.$notify.info({
|
|
|
- title: notify.deviceId,
|
|
|
- message: notify.content,
|
|
|
- showClose: false
|
|
|
- });
|
|
|
- }else {
|
|
|
- this.$notify.error({
|
|
|
- title: notify.deviceId,
|
|
|
- message: notify.content,
|
|
|
- duration: 0
|
|
|
- });
|
|
|
- }
|
|
|
- },
|
|
|
- // 关闭连接时调用
|
|
|
- websocketclose: function (e) {
|
|
|
- console.log("connection closed (" + e.code + ")");
|
|
|
- }
|
|
|
- }
|
|
|
};
|
|
|
</script>
|
|
|
<style scoped>
|