zhangjiaqing
8 天以前 1cef3adee31c6934c0da4b4f0b8a6f5ac03b364f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 入驻小区
 **/
(function(vc) {
    vc.extends({
        data: {
            parkingAreaControlInfo: {
                _currentTab: 'parkingAreaControlCarInout',
                boxId: '',
                paId: '',
                inMachineId: '',
                outMachineId: ''
            }
        },
        _initMethod: function() {
            $that.parkingAreaControlInfo.boxId = vc.getParam('boxId');
            $that.parkingAreaControlInfo.paId = vc.getParam('paId');
 
            $that._initParkingAreaWs();
            vc.emit('parkingAreaControlVideo', 'notify', {
                boxId: $that.parkingAreaControlInfo.boxId,
 
            });
        },
        _initEvent: function() {
            vc.on('parkingAreaControl', 'notify', function(_param) {
                vc.copyObject(_param, $that.parkingAreaControlInfo);
            })
        },
        methods: {
            changeTab: function(_tab) {
                $that.parkingAreaControlInfo._currentTab = _tab;
                vc.emit(_tab, 'switch', {
                    boxId: $that.parkingAreaControlInfo.boxId,
                    paId: $that.parkingAreaControlInfo.paId
                })
            },
            _initParkingAreaWs: function() {
                let clientId = vc.uuid();
                let heartCheck = {
                    timeout: 30000, // 9分钟发一次心跳,比server端设置的连接时间稍微小一点,在接近断开的情况下以通信的方式去重置连接时间。
                    serverTimeoutObj: null,
                    pingTime: new Date().getTime(),
                    reset: function() {
                        clearTimeout(this.serverTimeoutObj);
                        return this;
                    },
                    start: function() {
                        let self = this;
                        this.serverTimeoutObj = setInterval(function() {
                            if (websocket.readyState == 1) {
                                console.log("连接状态,发送消息保持连接");
                                let _pingTime = new Date().getTime();
                                //保护,以防 异常
                                if (_pingTime - self.pingTime < 15 * 1000) {
                                    return;
                                }
                                websocket.send("{'cmd':'ping'}");
                                self.pingTime = _pingTime;
                                heartCheck.reset().start(); // 如果获取到消息,说明连接是正常的,重置心跳检测
                            } else {
                                console.log("断开状态,尝试重连");
                                $that._initParkingAreaWs();
                            }
                        }, this.timeout)
                    }
                }
                let _protocol = window.location.protocol;
                let url = '';
                if (_protocol.startsWith('https')) {
                    url =
                        "wss://" + window.location.host + "/ws/parkingBox/" +
                        $that.parkingAreaControlInfo.boxId + "/" + clientId;
                } else {
                    url =
                        "ws://" + window.location.host + "/ws/parkingBox/" +
                        $that.parkingAreaControlInfo.boxId + "/" + clientId;
                    // url =
                    //     "ws://demo.homecommunity.cn:8008/ws/parkingArea/" +
                    //     $that.parkingAreaControlInfo.boxId + "/" + clientId;
                }
                if ("WebSocket" in window) {
                    websocket = new WebSocket(url);
                } else if ("MozWebSocket" in window) {
                    websocket = new MozWebSocket(url);
                } else {
                    websocket = new SockJS(url);
                }
                //连接发生错误的回调方法
                websocket.onerror = function(_err) {
                    console.log("初始化失败", _err);
                    this.$notify.error({
                        title: "错误",
                        message: "连接失败,请检查网络"
                    });
                };
                //连接成功建立的回调方法
                websocket.onopen = function() {
                    heartCheck.reset().start();
                    console.log("ws初始化成功");
                };
                //接收到消息的回调方法
                websocket.onmessage = function(event) {
                    heartCheck.reset().start();
                    console.log("event", event);
                    let _data = event.data;
                    try {
                        _data = JSON.parse(_data);
                    } catch (err) {
                        return;
                    }
                    vc.emit('parkingAreaControlCarInout', 'notify', {
                        data: _data,
                        parkingAreaControl: $that.parkingAreaControlInfo
                    });
                    vc.emit('parkingAreaControlFee', 'notify', _data);
                    vc.emit('parkingAreaControlTempCarAuthConfirm', 'notify', _data);
 
                };
                //连接关闭的回调方法
                websocket.onclose = function() {
                    console.log("初始化失败");
                    //$that._initParkingAreaWs();
                    this.$notify.error({
                        title: "错误",
                        message: "连接关闭,请刷新浏览器"
                    });
                };
                //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
                window.onbeforeunload = function() {
                    websocket.close();
                };
            },
 
        }
    });
})(window.vc);