xctc-utils 1.6.77 → 1.6.79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -38,6 +38,7 @@ const params = __importStar(require("./params"));
38
38
  const iframe = __importStar(require("./iframe"));
39
39
  const update = __importStar(require("./update"));
40
40
  const canvas_1 = __importDefault(require("./canvas"));
41
+ const socket_1 = __importDefault(require("./socket/socket"));
41
42
  const obj = {
42
43
  ...storage,
43
44
  ...utils,
@@ -52,6 +53,9 @@ const obj = {
52
53
  update: {
53
54
  ...update,
54
55
  },
56
+ socket: {
57
+ WebsocketHooks: socket_1.default
58
+ },
55
59
  canvas: {
56
60
  CanvasHooks: canvas_1.default,
57
61
  },
@@ -0,0 +1,18 @@
1
+ export default class WebsocketHooks {
2
+ private websocketUrl;
3
+ websocketSource: any;
4
+ private configCallBack;
5
+ constructor(url?: any, callbackConfig?: any);
6
+ get websocketInstance(): any;
7
+ formatProtocol(url: string): string;
8
+ bindValueFn(options: any, keys: string): void;
9
+ isEmptyObjectFn(obj: any): number | false;
10
+ createWebsocketFn(url?: any): void;
11
+ onOpenWebsocketFn(): void;
12
+ onMessageWebsocketFn(): void;
13
+ sendMessageWebsocketFn(message: any, callBack?: any): void;
14
+ destroyWebsocketFn(): void;
15
+ onCloseWebsocketFn(): void;
16
+ onErrorWebsocketFn(): void;
17
+ executeCallBackFn(keys: string, event: any): void;
18
+ }
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class WebsocketHooks {
4
+ constructor(url, callbackConfig) {
5
+ if (!url) {
6
+ console.error("请传入websocket连接地址");
7
+ }
8
+ this.websocketSource = "";
9
+ this.websocketUrl = this.formatProtocol(url);
10
+ this.configCallBack = callbackConfig;
11
+ this.createWebsocketFn();
12
+ }
13
+ get websocketInstance() {
14
+ return this.websocketSource;
15
+ }
16
+ formatProtocol(url) {
17
+ var _a;
18
+ let urlData = "";
19
+ const protocol = ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.protocol) || "";
20
+ if (protocol.includes("https")) {
21
+ if (url.includes("ws://")) {
22
+ urlData = url.replace("ws://", "wss://");
23
+ }
24
+ else if (url.includes("wss://")) {
25
+ urlData = url;
26
+ }
27
+ else {
28
+ urlData = 'wss://' + url;
29
+ }
30
+ }
31
+ else {
32
+ if (url.includes("wss://")) {
33
+ urlData = url.replace("wss://", "ws://");
34
+ }
35
+ else if (url.includes("ws://")) {
36
+ urlData = url;
37
+ }
38
+ else {
39
+ urlData = 'ws://' + url;
40
+ }
41
+ }
42
+ return urlData;
43
+ }
44
+ bindValueFn(options, keys) {
45
+ const _ = this;
46
+ if (_.isEmptyObjectFn(options)) {
47
+ if (options.hasOwnProperty(keys)) {
48
+ _[keys] = options[keys];
49
+ }
50
+ }
51
+ }
52
+ isEmptyObjectFn(obj) {
53
+ if (!obj) {
54
+ return false;
55
+ }
56
+ return Object.keys(obj).length;
57
+ }
58
+ createWebsocketFn(url) {
59
+ const _ = this;
60
+ if (url) {
61
+ _.websocketUrl = url;
62
+ }
63
+ _.websocketSource = new WebSocket(_.websocketUrl);
64
+ _.onOpenWebsocketFn();
65
+ _.onCloseWebsocketFn();
66
+ _.onErrorWebsocketFn();
67
+ _.onMessageWebsocketFn();
68
+ }
69
+ onOpenWebsocketFn() {
70
+ const _ = this;
71
+ if (_.websocketSource) {
72
+ _.websocketSource.addEventListener('open', (event) => {
73
+ _.executeCallBackFn("openCallBack");
74
+ });
75
+ }
76
+ else {
77
+ console.error("onOpenWebsocketFn::websocketSource is null");
78
+ }
79
+ }
80
+ onMessageWebsocketFn() {
81
+ const _ = this;
82
+ if (_.websocketSource) {
83
+ _.websocketSource.addEventListener('message', (event) => {
84
+ console.log("onMessageWebsocketFn:", event);
85
+ _.executeCallBackFn("messageCallBack", event);
86
+ });
87
+ }
88
+ else {
89
+ console.error("onMessageWebsocketFn::websocketSource is null");
90
+ }
91
+ }
92
+ // 0 (CONNECTING):表示连接正在建立过程中。
93
+ // 1 (OPEN):表示连接已经建立并且可以进行通信了。
94
+ // 2 (CLOSING):表示连接正在进行关闭过程。
95
+ // 3 (CLOSED):表示连接已经关闭,或者打开过程中失败了。
96
+ // 4 (UNKNOWN):这是一个非标准的状态,用于表示 readyState 的值未知或不被识别。
97
+ sendMessageWebsocketFn(message, callBack) {
98
+ const _ = this;
99
+ if (_.websocketSource) {
100
+ console.log("message==", message);
101
+ const readyState = _.websocketSource.readyState;
102
+ if (typeof callBack === 'function') {
103
+ callBack({ readyState: readyState });
104
+ }
105
+ if (readyState === 1) {
106
+ _.websocketSource.send(message);
107
+ }
108
+ else {
109
+ console.log('WebSocket is not open yet');
110
+ }
111
+ }
112
+ else {
113
+ console.error("sendMessageWebsocketFn::websocketSource is null");
114
+ }
115
+ }
116
+ destroyWebsocketFn() {
117
+ const _ = this;
118
+ if (_.websocketSource) {
119
+ _.websocketSource.close();
120
+ _.websocketSource = null;
121
+ }
122
+ else {
123
+ console.error("destroyWebsocketFn::websocketSource is null");
124
+ }
125
+ }
126
+ onCloseWebsocketFn() {
127
+ const _ = this;
128
+ if (_.websocketSource) {
129
+ _.websocketSource.addEventListener('close', (event) => {
130
+ _.executeCallBackFn("closeCallBack", event);
131
+ });
132
+ }
133
+ else {
134
+ console.error("onCloseWebsocketFn::websocketSource is null");
135
+ }
136
+ }
137
+ onErrorWebsocketFn() {
138
+ const _ = this;
139
+ if (_.websocketSource) {
140
+ _.websocketSource.addEventListener('error', (event) => {
141
+ _.executeCallBackFn("errorCallBack", event);
142
+ });
143
+ }
144
+ else {
145
+ console.error("onErrorWebsocketFn::websocketSource is null");
146
+ }
147
+ }
148
+ executeCallBackFn(keys, event) {
149
+ var _a;
150
+ const _ = this;
151
+ const callBack = (_a = _.configCallBack) === null || _a === void 0 ? void 0 : _a.hasOwnProperty(keys);
152
+ if (callBack) {
153
+ _.configCallBack[keys](event);
154
+ }
155
+ }
156
+ }
157
+ exports.default = WebsocketHooks;
@@ -1,2 +1,2 @@
1
1
  import 'whatwg-fetch';
2
- export declare function autoUpdateLastVersion(duration?: number, toast?: boolean): void;
2
+ export declare function autoUpdateLastVersion(duration?: number, toast?: boolean, reload?: boolean, callback?: any): void;
@@ -43,36 +43,51 @@ async function needUpdate() {
43
43
  autoUpdateLastSrcs = newScripts;
44
44
  return result;
45
45
  }
46
- function autoRefresh() {
46
+ function autoRefresh(reload, callback) {
47
47
  // 发起循环检测
48
48
  setTimeout(async () => {
49
49
  const willUpdate = await needUpdate();
50
+ autoUpdateCallback(callback, willUpdate);
50
51
  if (willUpdate) {
51
52
  if (autoUpdateToast) {
52
53
  alert('有新的版本更新');
53
54
  }
54
- location.reload();
55
+ if (reload) {
56
+ location.reload();
57
+ }
55
58
  }
56
- autoRefresh();
59
+ autoRefresh(reload, callback);
57
60
  }, autoUpdateDuration);
58
61
  }
59
- function autoUpdateLastVersion(duration, toast) {
62
+ function autoUpdateLastVersion(duration, toast, reload, callback) {
63
+ let reloadStatus = true;
60
64
  if (duration && Number(duration) >= 2000) {
61
65
  autoUpdateDuration = duration;
62
66
  }
63
67
  if (typeof toast === 'boolean') {
64
68
  autoUpdateToast = toast;
65
69
  }
70
+ if (typeof reload === 'boolean') {
71
+ reloadStatus = reload;
72
+ }
66
73
  // 先默认检测一遍
67
74
  setTimeout(async () => {
68
75
  const willUpdate = await needUpdate();
76
+ autoUpdateCallback(callback, willUpdate);
69
77
  if (willUpdate) {
70
78
  if (autoUpdateToast) {
71
79
  alert('有新的版本更新');
72
80
  }
73
- location.reload();
81
+ if (reloadStatus) {
82
+ location.reload();
83
+ }
74
84
  }
75
- autoRefresh();
85
+ autoRefresh(reloadStatus, callback);
76
86
  }, 200);
77
87
  }
78
88
  exports.autoUpdateLastVersion = autoUpdateLastVersion;
89
+ function autoUpdateCallback(callback, willUpdate) {
90
+ if (typeof callback === 'function') {
91
+ callback(willUpdate);
92
+ }
93
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "xctc-utils",
3
3
  "private": false,
4
- "version": "1.6.77",
4
+ "version": "1.6.79",
5
5
  "description": "localStorage存储\r ```\r sessionStorage存储\r ```\r crypto-js加密、解密\r ```\r 微信授权登录、微信分享\r ```\r 设备环境获取\r ```\r 是否是微信浏览器\r ```\r 时间戳转时间,字符串转时间戳",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",