zhanghua
2025-07-16 7c20fd15b7fbc2bd5756b39d5ab655cc849ffcc3
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// 与客户端交互方法列表
import wsPlugins from './plugins/index';
// const ReWebSocket = require('reconnecting-websocket');
// websocket重连
import ReWebSocket from '../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs';
// const Bowser = require('bowser');
// 浏览器信息获取
import Bowser from "../node_modules/bowser/es5";
const browser = Bowser.getParser(window.navigator.userAgent);
 
interface callbacks {
    // 连接客户端状态
    connectResult: any,
    // 登录客户端状态
    loginResult: any
}
 
 
export default class Ws {
    [x: string]: any;
    // 连接地址
    static url: string = '';
    // 传入的配置项(方法)
    static opts: Object = {};
    // 密码加密公钥
    static publicKey = '';
    // 是否连接客户端
    private isConnectSuccessQt: Boolean = false;
    // 是否登录客户端
    private isLoginSuccess: Boolean = false;
    // 唯一实例
    private static _instance: any = null;
    // 当前连接的webSocket
    webSocket: any;
    // 最大重连数
    reConnectCount: number = 3;
    // 连接失败次数
    connectFailCount: number = 0;
    // 重连完成标识
    connectEnd: Boolean = false;
    // 登录完成标识
    loginEnd: Boolean = false;
    // 最大重登数
    reLoginCount: number = 1;
    // 连接失败次数
    loginFailCount: number = 0;
    // 当前websocket登录配置
    config: Object = {};
    // 登陆IP
    loginIp: string;
    // 登陆端口
    loginPort: string;
    // 登录用户名
    userName: string;
    // 登录密码,密码与token二选一
    userPwd: string;
    // 登录token,密码与token二选一
    token: string;
    // 用户标识符
    userCode: Number = 0;
    // 回调函数
    private callback: callbacks;
    // 创建的控件ID列表
    ids: Array<String> = [];
    // 创建的控件列表
    ctrls: Array<String> = [];
    // 心跳
    heartBeatTimer: any = null;
    // 监听的方法表
    listerns: any;
    constructor({
        url= 'ws://localhost:1234',
        publicKey= 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDbEpPpxpLJft4W9YZj8bRh2bYYZshBEsKOlxgyn11rlEyTasjBSZRV9aj33tvQ2T55izH0fWl+dL/dLZChawFrlGDcH8JuWge2xYMgII9mggcYa0UiQ7pLXJ9ivXZ/cOY3HzrRQdR7dGTSNn3Z0Ctbns6mLgvlA2r3qMNs/8wHBwIDAQAB',
        reConnectCount = 3,
        reLoginCount = 1,
        loginIp= location.hostname,
        loginPort= location.port,
        userName = '',
        userPwd = '',
        token = '',
        callback = {
            connectResult: null,
            loginResult: null
        }
    } : {
        url: string,
        publicKey: string,
        reConnectCount: number,
        reLoginCount: number,
        loginIp: string,
        loginPort: string,
        userName: string,
        userPwd: string,
        token: string
        callback: callbacks,
    }) {
        // if (/https/.test(location.protocol)) {
        //     url = `wss:${url}`;
        // } else {
        //     url = `ws:${url}`;
        // }
        // url = `ws://localhost:1234`;
 
        this.url = url;
        this.userCode = new Date().valueOf();
        this.webSocket = null;
        this.reConnectCount = reConnectCount;
        this.reLoginCount = reLoginCount;
        this.publicKey = publicKey;
        this.loginIp = loginIp;
        this.loginPort = loginPort;
        this.userName = userName;
        this.userPwd = userPwd;
        this.token = token;
        this.callback = callback;
        
        this.connectFailCount = this.connectFailCount;
 
        // 基础操作所需功能或属性
        this.ids = [];
        this.ctrls = [];
 
        // 是否连接客户端
        this.isConnectSuccessQt = false;
        // 是否登陆客户端
        this.isLoginSuccess = false;
        this.heartBeatTimer = null;
 
        // 初始化配置参数
        this.initConfig();
 
        // 连接客户端
        this.connectQt();
 
        // 获取与客户端交互方法列表
        const plugins = [];
        Object.keys(wsPlugins).forEach(item => {
            if(hasKey(wsPlugins, item)){
                plugins.push(wsPlugins[item]);
            }
        });
        // 与客户端通讯功能注册
        usePlugin(plugins);
 
        // 用户注册监听事件表
        this.listerns = new Map();
 
        // 传入登录配置,则登录
        if(this.userName) {
            this.detectConnectQt().then((res: Boolean) => {
                if(res) {
                    // 登陆客户端
                    this.loginClient();
                    // 心跳保活
                    this._heartbeat();
                }
            });
        }
    }
    /**
     * @description 获得实例对象
     */
    public static getInstance(options: any):Ws {
        if(!this._instance) {
            this._instance = new Ws(options);
        }
        return this._instance;
    }
    /**
     * @description 用户注册监听事件
     * @params {String} eventType 事件名称
     * @params {any} callback 回调函数
     */
    on(eventType: String, callback: any) {
        this.listerns.set(eventType, callback);
    }
    /**
     * @description 用户取消监听事件
     * @params {String} eventType 事件名称
     */
    off(eventType: string) {
        delete this.listerns[eventType];
    }
    /**
     * @description 发送消息给客户端
     * @params {String} method 事件名称
     * @params {Object} data 传输消息的数据内容
     */
    emit(method: any, data: { method: any; }) {
        const { webSocket } = this;
        data.method = method;
        // 不需要判断登录和连接的方法过滤
        let filterList = ['heartbeat', 'login', 'logout', 'browserInfo'];
        if(filterList.includes(method)) {
            webSocket.send(JSON.stringify(data));
            return;
        }
        return new Promise((resolve, reject) => {
            this.detectConnectQt().then(con => {
                if(con) {
                    this.detectLoginClient().then(login => {
                        if(login) {
                            webSocket.send(JSON.stringify(data));
                            resolve(true);
                        } else {
                            // 登录失败
                            reject(2);
                        }
                    })
                } else {
                    // 连接失败
                    reject(1);
                }
            })
        })
    }
    /*
    * 初始化配置
    */
    initConfig() {
        this.config = {
            userName: this.userName,
            userCode: this.userCode,
            loginPort: this.loginPort,
            loginIp: this.loginIp,
            userPwd: this.userPwd,
            token: this.token,
        };
        // 浏览器信息
        const browserInfo = {
            name: '',
            version: '',
            platform: ''
        };
        browserInfo.name = browser.getBrowserName().toLowerCase();
        browserInfo.version = browser.getBrowser().version.toLowerCase();
        browserInfo.platform =
            browser._ua.indexOf('Win64') >= 0 || browser._ua.indexOf('Wow64') >= 0
                ? 'win64'
                : 'win32';
        this.config['browser'] = browserInfo;
    }
    /**
     * @description 连接客户端
     */
    connectQt() {
        this.connectEnd = false;
        // 连接客户端
        this.webSocket = new ReWebSocket(this.url, '', {
            maxRetries: this.reConnectCount
        });
        this.addEvents();
    }
    /**
     * @description 检测连接客户端状态
     */
    detectConnectQt() {
        let _this = this;
        return new Promise((resolve, reject) => {
            if(!this.connectEnd) { // 连接中
                let _interval = setInterval(() => {
                    if(_this.connectEnd) {
                        clearInterval(_interval);
                        resolve(_this.isConnectSuccessQt);
                    }
                }, 50)
            } else {
                resolve(_this.isConnectSuccessQt);
            }
        })
    }
    /**
     * @description 检测登录客户端状态
     */
    detectLoginClient() {
        let _this = this;
        return new Promise((resolve, reject) => {
            if(!this.loginEnd) { // 连接中
                let _interval = setInterval(() => {
                    if(_this.loginEnd) {
                        clearInterval(_interval);
                        resolve(_this.isLoginSuccess);
                    }
                }, 50)
            } else {
                resolve(_this.isLoginSuccess);
            }
        })
    }
    /**
     * @description 登录客户端
     * @params {Object} config 登录相关配置
     */
    login(config: Object) {
        this.loginEnd = false;
        Object.assign(this.config, config);
        // 未连接客户端
        if(!this.isOpen()) {
            // 连接失败
            return false
        } else {
            // 若已登录,先注销
            if (this.isLoginSuccess) {
                this.logout();
            }
            // 登陆客户端
            this.loginClient();
            // 心跳保活
            this._heartbeat();
        }
    }
    /**
     * @description 连接客户端完成后登陆
     */
    // loginAfterConnectEnd() {
    //     // 未连接客户端
    //     if(!this.isOpen()) {
    //         // 连接失败
    //         return false
    //     } else {
    //         // 若已登录,先注销
    //         if (this.isLoginSuccess) {
    //             this.logout();
    //         }
    //         // 登陆客户端
    //         this.loginClient();
    //         // 心跳保活
    //         this._heartbeat();
    //     }
    // }
    /**
     * @description 登出客户端
     */
    logout() {
        // 退出客户端
        this.logoutClient();
        this.isLoginSuccess = false;
        if (typeof this.callback.loginResult === 'function') {
            this.callback.loginResult.call(this, this.isLoginSuccess);
        }
    }
    /**
     * @description 添加websocket/window监听事件
     */
    addEvents() {
        const webSocket = this.webSocket;
        webSocket.addEventListener('open', () => this.onOpen());
        webSocket.addEventListener('message', (e: any) => this.onMessage(e));
        webSocket.addEventListener('error', () => this.onError());
        window.addEventListener('resize', () => this.reLocatedPosition());
        window.addEventListener('scroll', () => this.reLocatedPosition());
    }
    /**
     * @description 移除websocket/window监听事件
     */
    removeEvents() {
        const webSocket = this.webSocket;
        webSocket.removeEventListener('open', this.onOpen);
        webSocket.removeEventListener('message', this.onMessage);
        webSocket.removeEventListener('error', this.onError);
        webSocket.removeEventListener('resize', this.reLocatedPosition);
        webSocket.removeEventListener('scroll', this.reLocatedPosition);
    }
    /**
     * @description 连接客户端成功事件
     */
    onOpen() {
        this.isConnectSuccessQt = true;
        this.connectEnd = true;
        if (typeof this.callback.connectResult === 'function') {
            this.callback.connectResult.call(this, this.isConnectSuccessQt);
        }
    }
    /**
     * @description 接收客户端消息
     * @params {Object} event 接收客户端的消息数据
     */
    onMessage(event: { data: string; }) {
        try {
            const data = JSON.parse(event.data);
            const { method } = data;
            const callback = this.listerns.get(method);
            if (method === 'loginState') {
                this.loginFailCount ++;
                if(this.loginFailCount < this.reLoginCount + 1) { // 登录未到最大次数
                    if(data.params.loginResult === 0) { // 登录成功
                        this.isLoginSuccess = data.params.loginResult === 0;
                        if (typeof this.callback.loginResult === 'function') {
                            this.callback.loginResult.call(this, this.isLoginSuccess);
                        }
                    } else { // 登录失败
                        this.loginClient();
                    }
                } else { // 登录达到最大次数
                    this.loginEnd = true;
                    this.isLoginSuccess = data.params.loginResult === 0;
                    if (typeof this.callback.loginResult === 'function') {
                        this.callback.loginResult.call(this, this.isLoginSuccess);
                    }
                }
            }
            if (callback) {
                if (method === 'loginState') {
                    callback(this.isLoginSuccess);
                } else if (method === 'createCtrlResult') {
                    callback(data.params.array);
                } else {
                    callback(data);
                }
            }
        } catch (e) {
            // console.error('error', e);
        }
    }
    /**
     * @description 客户端发生错误事件
     */
    onError() {
        this.isConnectSuccessQt = false;
        clearTimeout(this.heartbeatTimer);
        this.connectFailCount ++;
        if(this.connectFailCount === this.reConnectCount + 1) {
            this.connectEnd = true;
            if (typeof this.callback.connectResult === 'function') {
                this.callback.connectResult.call(this, this.isConnectSuccessQt);
            }
        }
    }
    /**
     * @description 判断是否成功连接客户端
     */
    isOpen() {
        if (!this.webSocket) return false;
        return this.webSocket.readyState === 1;
    }
    /**
     * @description 心跳事件
     */
    _heartbeat() {
        this.heartbeat();
        clearTimeout(this.heartbeatTimer);
        this.heartbeatTimer = setTimeout(() => {
            this._heartbeat();
        }, 10000);
    }
    /**
     * @description 获取当前浏览器缩放和滚动条信息
     */
    getScrollInfo() {
        let ratio = detectZoom();
        let scrollX = window.pageXOffset;
        let scrollY = window.pageYOffset;
        var hasscrollbary = hasScrollbarY();
        var hasscrollbarx = hasScrollbarX();
        var scrollbarWidth = getScrollbarWidth();
        let scrollXH = hasscrollbarx ? scrollbarWidth : 0;
        let scrollYW = hasscrollbary ? scrollbarWidth : 0;
        return { ratio, scrollX, scrollY, scrollXH, scrollYW };
    }
}
 
function promisify(func: Function) {
    return new    Promise((resolve, reject) => {
        func('1', (data) => resolve(data))
    })
}
 
/**
 * @description 判断对象是否含有某属性
 * @params {Object} obj 对象
 * @params {String} key 属性key
 */
function hasKey<O>(obj: O, key: keyof any): key is keyof O {
    return key in obj
}
 
/**
 * @description 与客户端通讯功能注册
 * @params {Array} plugins 功能列表
 */
function usePlugin(plugins: any[]) {
    plugins.forEach((plugin) => {
        Object.getOwnPropertyNames(plugin).forEach(prop => {
            Ws.prototype[prop] = plugin[prop];
        });
    });
}
 
function detectZoom() {
    var ratio = 0,
        screen = window.screen,
        ua = navigator.userAgent.toLowerCase();
 
    if (window.devicePixelRatio !== undefined) {
        ratio = window.devicePixelRatio;
    } else if (~ua.indexOf('msie')) {
        if (screen['deviceXDPI'] && screen['logicalXDPI']) {
            ratio = screen['deviceXDPI'] / screen['logicalXDPI'];
        }
    } else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
        ratio = window.outerWidth / window.innerWidth;
    }
 
    if (ratio) {
        ratio = Math.round(ratio * 100);
    }
    return ratio;
}
 
function hasScrollbarY() {
    return (
        document.body.scrollHeight >
        (window.innerHeight || document.documentElement.clientHeight)
    );
}
 
function hasScrollbarX() {
    return (
        document.body.scrollWidth >
        (window.innerWidth || document.documentElement.clientWidth)
    );
}
 
function getScrollbarWidth() {
    var scrollDiv = document.createElement('div');
    scrollDiv.style.cssText =
        'width: 99px; height: 99px; overflow: scroll; position: absolute; top: -9999px;';
    document.body.appendChild(scrollDiv);
    var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
    document.body.removeChild(scrollDiv);
 
    return scrollbarWidth;
}