liuyg
2022-02-21 e3dcaeb0180a4e37347ac9ceb44c8a59ae7af37f
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
// uniapp版本websocket总控制
 
function Wst(url) {
    this.url = url || "";
    this.ws = "";
    this.ourControl = true; //总控
    this.state = false;
    this.timeIntval = false;
    this.timeIntvalTime = 5000;
    this.WebSocketValue = "";
    this.backValue = function(res) {
        console.log(res, "未设置msg返回函数");
    };
    this.getws = "";
    this.Data = "";
    this.heartBeat = "";
    this.loginData = "";
 
    this.ids = '';
    return this;
}
 
Wst.prototype.$callBack = function(getValue, getState, getws, ids) {
    this.backValue = function(res) {
        if (!getValue && !getState) {
            console.log(res, "未设置msg返回函数");
        } else {
            if (getValue) {
                getValue(res);
            }
            if (getState) {
                getState(this.state);
            }
        }
    };
    this.getws = getws;
    this.ids = ids;
    return this;
};
 
Wst.prototype.$connect = function(Data) {
    this.ourControl = true;
    let that = this;
    this.Data = Data;
    this.loginData = {
        id: this.ids,
        type: 'login'
    };
    this.heartBeat = 'ping';
    this.ws = uni.connectSocket({
        url: that.url,
        success(data) {
            // console.log(data, "websocket连接成功");
        },
    });
    // console.log(that.url)
    this.ws.onOpen((res) => {
        // 注:只有连接正常打开中 ,才能正常成功发送消息
        that.ws.send({
            data: JSON.stringify(that.loginData),
            async success(res) {
                // console.log(res, "登入成功"); //设置登入成功状态 并开启心跳
                that.state = true;
                that.$heartbeat();
            },
        });
        // 注:只有连接正常打开中 ,才能正常收到消息
        that.ws.onMessage((res) => {
            // console.log("收到服务器内容:", JSON.parse(res.data));
            let d = res.data;
            try {
                d = JSON.parse(res.data);
            } catch (e) {}
            that.WebSocketValue = d;
            // that.state = true;
            // this.$heartbeat();
            that.backValue(d);
        });
    });
    // this.ws.onClose((e) => {
    //     console.log(11111111111111111)
    //     that.state = false;
    //     that.backValue(e);
    // });
    // this.$heartbeat();
    this.getws(this.ws);
    return this;
};
 
Wst.prototype.$heartbeat = function() {
    let that = this;
    let open = function() {
        // console.log('开启心跳');
 
        that.ws.send({
            data: that.heartBeat,
        });
    };
    let rel = function() {
        console.log('重新连接');
        that.$connect(that.Data);
    };
    if (that.timeIntval) {
        clearInterval(that.timeIntval);
    }
    that.timeIntval = setInterval(() => {
        if (!this.ourControl) {
            return;
        }
        // console.log(that.state)
        if (that.state) {
            open();
        } else {
            rel();
        }
    }, that.timeIntvalTime);
};
 
Wst.prototype.$outws = function() {
    this.ourControl = false;
    clearInterval(this.timeIntval);
    this.timeIntval = false;
    this.WebSocketValue = {};
    this.state = false;
    // this.backValue({});
};
 
export default Wst;