vite-uni-dev-tool 1.1.0 → 1.2.0
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/README.md +8 -0
- package/dist/const.cjs +1 -1
- package/dist/const.d.ts +1 -0
- package/dist/const.d.ts.map +1 -1
- package/dist/const.js +1 -1
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +2 -2
- package/dist/i18n/locales/en.cjs +1 -1
- package/dist/i18n/locales/en.d.ts +5 -0
- package/dist/i18n/locales/en.d.ts.map +1 -1
- package/dist/i18n/locales/en.js +1 -1
- package/dist/i18n/locales/zh-Hans.cjs +1 -1
- package/dist/i18n/locales/zh-Hans.d.ts +5 -0
- package/dist/i18n/locales/zh-Hans.d.ts.map +1 -1
- package/dist/i18n/locales/zh-Hans.js +1 -1
- package/dist/modules/devIntercept/index.cjs +9 -9
- package/dist/modules/devIntercept/index.d.ts +1 -1
- package/dist/modules/devIntercept/index.d.ts.map +1 -1
- package/dist/modules/devIntercept/index.js +9 -9
- package/dist/modules/devStore/index.cjs +1 -1
- package/dist/modules/devStore/index.d.ts.map +1 -1
- package/dist/modules/devStore/index.js +1 -1
- package/dist/plugins/uniDevTool/transform/transformMain.cjs +3 -3
- package/dist/plugins/uniDevTool/transform/transformMain.d.ts +2 -1
- package/dist/plugins/uniDevTool/transform/transformMain.d.ts.map +1 -1
- package/dist/plugins/uniDevTool/transform/transformMain.js +3 -3
- package/dist/plugins/uniDevTool/transform/transformVue.cjs +31 -25
- package/dist/plugins/uniDevTool/transform/transformVue.d.ts +2 -1
- package/dist/plugins/uniDevTool/transform/transformVue.d.ts.map +1 -1
- package/dist/plugins/uniDevTool/transform/transformVue.js +30 -24
- package/dist/plugins/uniDevTool/uniDevTool.cjs +3 -3
- package/dist/plugins/uniDevTool/uniDevTool.d.ts +3 -1
- package/dist/plugins/uniDevTool/uniDevTool.d.ts.map +1 -1
- package/dist/plugins/uniDevTool/uniDevTool.js +3 -3
- package/dist/type.d.ts +3 -0
- package/dist/type.d.ts.map +1 -1
- package/dist/v3/DevTool/components/BluetoothList/BluetoothItem.vue +199 -199
- package/dist/v3/DevTool/components/BluetoothList/BluetoothTool.vue +730 -730
- package/dist/v3/DevTool/components/BluetoothList/index.vue +167 -167
- package/dist/v3/DevTool/components/CaptureScreen/index.vue +109 -109
- package/dist/v3/DevTool/components/ConsoleList/ConsoleItem.vue +230 -225
- package/dist/v3/DevTool/components/ConsoleList/RunJSInput.vue +247 -247
- package/dist/v3/DevTool/components/ConsoleList/index.vue +171 -171
- package/dist/v3/DevTool/components/ConsoleList/staticTips.ts +1145 -1145
- package/dist/v3/DevTool/components/DevToolTitle/index.vue +24 -24
- package/dist/v3/DevTool/components/DevToolWindow/DevToolOverlay.vue +197 -197
- package/dist/v3/DevTool/components/DevToolWindow/hooks/dataUtils.ts +48 -48
- package/dist/v3/DevTool/components/DevToolWindow/hooks/useDevToolData.ts +387 -387
- package/dist/v3/DevTool/components/DevToolWindow/hooks/useDevToolHandlers.ts +629 -629
- package/dist/v3/DevTool/components/DevToolWindow/hooks/useDevToolOverlay.ts +201 -197
- package/dist/v3/DevTool/components/ElEvent/ElEventItem.vue +105 -105
- package/dist/v3/DevTool/components/ElEvent/index.vue +106 -106
- package/dist/v3/DevTool/components/Instance/components/InstanceTreeNode.vue +265 -265
- package/dist/v3/DevTool/components/Instance/flatten.ts +226 -226
- package/dist/v3/DevTool/components/Instance/index.vue +94 -94
- package/dist/v3/DevTool/components/Instance/registry.ts +49 -49
- package/dist/v3/DevTool/components/Instance/transformTree.ts +375 -375
- package/dist/v3/DevTool/components/Instance/transformTreeCtx.ts +268 -268
- package/dist/v3/DevTool/components/Instance/typing.d.ts +43 -43
- package/dist/v3/DevTool/components/InstanceDetail/index.vue +485 -485
- package/dist/v3/DevTool/components/JsonDetail/index.vue +70 -70
- package/dist/v3/DevTool/components/NFCList/NFCItem.vue +112 -112
- package/dist/v3/DevTool/components/NFCList/NFCTool.vue +454 -454
- package/dist/v3/DevTool/components/NFCList/const.ts +56 -56
- package/dist/v3/DevTool/components/NFCList/index.vue +94 -94
- package/dist/v3/DevTool/components/NetworkList/InterceptConfig.vue +624 -624
- package/dist/v3/DevTool/components/NetworkList/InterceptItem.vue +140 -140
- package/dist/v3/DevTool/components/NetworkList/NetworkDetail.vue +287 -287
- package/dist/v3/DevTool/components/NetworkList/NetworkIntercept.vue +88 -88
- package/dist/v3/DevTool/components/NetworkList/NetworkItem.vue +163 -163
- package/dist/v3/DevTool/components/NetworkList/NetworkSend.vue +589 -589
- package/dist/v3/DevTool/components/NetworkList/const.ts +4 -4
- package/dist/v3/DevTool/components/NetworkList/hooks/useNetworkForm.ts +86 -86
- package/dist/v3/DevTool/components/NetworkList/index.vue +160 -160
- package/dist/v3/DevTool/components/NetworkList/utils.ts +101 -101
- package/dist/v3/DevTool/components/Performance/index.vue +498 -498
- package/dist/v3/DevTool/components/Performance/modules/PerformanceMetrics.vue +153 -153
- package/dist/v3/DevTool/components/Performance/modules/usePerformanceChart.ts +460 -460
- package/dist/v3/DevTool/components/Performance/modules/usePerformanceData.ts +258 -258
- package/dist/v3/DevTool/components/PiniaList/index.vue +93 -93
- package/dist/v3/DevTool/components/RunJS/index.vue +148 -148
- package/dist/v3/DevTool/components/ScanCodeList/ScanCodeItem.vue +97 -97
- package/dist/v3/DevTool/components/ScanCodeList/index.vue +100 -100
- package/dist/v3/DevTool/components/SettingButton/index.vue +45 -45
- package/dist/v3/DevTool/components/SettingList/index.vue +218 -218
- package/dist/v3/DevTool/components/SettingList/modules/SettingBarrage.vue +304 -304
- package/dist/v3/DevTool/components/SettingList/modules/SettingDevTool.vue +212 -212
- package/dist/v3/DevTool/components/SettingList/modules/SettingInfo.vue +157 -157
- package/dist/v3/DevTool/components/SettingList/modules/SettingLanguage.vue +74 -74
- package/dist/v3/DevTool/components/SettingList/modules/SettingLog.vue +230 -230
- package/dist/v3/DevTool/components/SettingList/typing.d.ts +2 -2
- package/dist/v3/DevTool/components/SourceCode/Line.vue +127 -127
- package/dist/v3/DevTool/components/SourceCode/parseCode.ts +609 -609
- package/dist/v3/DevTool/components/StorageList/index.vue +174 -174
- package/dist/v3/DevTool/components/TransferList/TransferDetail.vue +268 -268
- package/dist/v3/DevTool/components/VuexList/index.vue +84 -84
- package/dist/v3/DevTool/index.vue +1 -0
- package/dist/v3/components/AppTransition/index.vue +176 -176
- package/dist/v3/components/AutoSizer/index.vue +192 -192
- package/dist/v3/components/AutoSizer/index1.vue +184 -184
- package/dist/v3/components/AutoSizer/utils.ts +49 -49
- package/dist/v3/components/Barrage/BarrageItem.vue +137 -137
- package/dist/v3/components/Barrage/index.vue +202 -202
- package/dist/v3/components/CircularButton/index.vue +84 -84
- package/dist/v3/components/CustomSwiper/CustomSwiperItem.vue +49 -49
- package/dist/v3/components/CustomSwiper/index.vue +104 -104
- package/dist/v3/components/DevErrorBoundary/index.vue +380 -0
- package/dist/v3/components/Empty/index.vue +29 -29
- package/dist/v3/components/FilterSelect/index.vue +179 -179
- package/dist/v3/components/JsonPretty/components/Brackets/index.vue +27 -27
- package/dist/v3/components/JsonPretty/components/Carets/index.vue +59 -59
- package/dist/v3/components/JsonPretty/components/CheckController/index.vue +136 -136
- package/dist/v3/components/JsonPretty/components/TreeNode/index.vue +387 -387
- package/dist/v3/components/JsonPretty/hooks/useClipboard.ts +21 -21
- package/dist/v3/components/JsonPretty/hooks/useError.ts +21 -21
- package/dist/v3/components/JsonPretty/type.ts +127 -127
- package/dist/v3/components/JsonPretty/utils/index.ts +169 -169
- package/dist/v3/components/MovableContainer/index.vue +8 -4
- package/dist/v3/components/Pick/index.vue +322 -322
- package/dist/v3/components/Tag/index.vue +113 -113
- package/dist/v3/components/VirtualList/AutoSize.vue +40 -40
- package/dist/v3/components/VirtualList/index.vue +416 -416
- package/dist/v3/hooks/useBluetooth/index.ts +561 -561
- package/dist/v3/hooks/useContainerStyle.ts +153 -153
- package/dist/v3/hooks/useNFC/index.ts +107 -107
- package/dist/v3/hooks/useNFC/typing.d.ts +396 -396
- package/dist/v3/hooks/useNFC/useNFCAndroid.ts +966 -966
- package/dist/v3/hooks/useNFC/useNFCMpWeiXin.ts +812 -812
- package/dist/v3/hooks/useNFC/utils.ts +754 -754
- package/dist/v3/hooks/useRequest/index.ts +586 -586
- package/dist/v3/hooks/useRequest/utils.ts +267 -267
- package/dist/v3/hooks/useScanCode/index.ts +206 -206
- package/dist/v3/hooks/useWebsocket/index.ts +253 -253
- package/dist/v3/styles/theme.ts +12 -12
- package/package.json +9 -1
|
@@ -1,253 +1,253 @@
|
|
|
1
|
-
import { ref, onUnmounted, type Ref } from 'vue';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* WebSocket 连接状态枚举
|
|
5
|
-
*/
|
|
6
|
-
export enum ReadyState {
|
|
7
|
-
Connecting = 0, // 连接中
|
|
8
|
-
Open = 1, // 已连接
|
|
9
|
-
Closing = 2, // 关闭中
|
|
10
|
-
Closed = 3, // 已关闭
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* useWebsocket 配置参数
|
|
15
|
-
*/
|
|
16
|
-
export interface Options {
|
|
17
|
-
/** 重连尝试次数限制,默认为 3 */
|
|
18
|
-
reconnectLimit?: number;
|
|
19
|
-
/** 重连时间间隔 (ms),默认为 3000 */
|
|
20
|
-
reconnectInterval?: number;
|
|
21
|
-
/** 是否开启心跳检查,默认为 false */
|
|
22
|
-
heartbeat?: boolean;
|
|
23
|
-
/** 心跳检查间隔 (ms),默认为 30000 */
|
|
24
|
-
heartbeatInterval?: number;
|
|
25
|
-
/** 心跳发送的内容,默认为 'ping' */
|
|
26
|
-
heartbeatMessage?: string | ArrayBuffer;
|
|
27
|
-
/** 是否手动控制连接,为 true 时需手动调用 connect(),默认为 false */
|
|
28
|
-
manual?: boolean;
|
|
29
|
-
/** 连接开启时的回调 */
|
|
30
|
-
onOpen?: (
|
|
31
|
-
instance: UniApp.SocketTask,
|
|
32
|
-
res: UniApp.OnSocketOpenCallbackResult,
|
|
33
|
-
) => void;
|
|
34
|
-
/** 连接关闭时的回调 */
|
|
35
|
-
onClose?: (event: UniApp.OnSocketCloseOptions) => void;
|
|
36
|
-
/** 收到消息时的回调 */
|
|
37
|
-
onMessage?: (message: UniApp.OnSocketMessageCallbackResult) => void;
|
|
38
|
-
/** 连接发生错误时的回调 */
|
|
39
|
-
onError?: (event: UniApp.GeneralCallbackResult) => void;
|
|
40
|
-
/** WebSocket 子协议 */
|
|
41
|
-
protocols?: string[];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* useWebsocket 返回结果
|
|
46
|
-
*/
|
|
47
|
-
export interface Result {
|
|
48
|
-
/** 最近收到的一条消息 */
|
|
49
|
-
latestMessage: Ref<UniApp.OnSocketMessageCallbackResult | undefined>;
|
|
50
|
-
/** 当前连接状态 */
|
|
51
|
-
readyState: Ref<ReadyState>;
|
|
52
|
-
/** 手动连接方法 */
|
|
53
|
-
connect: () => void;
|
|
54
|
-
/** 手动断开方法 */
|
|
55
|
-
disconnect: () => void;
|
|
56
|
-
/** 发送消息方法 */
|
|
57
|
-
sendMessage: (data: string | ArrayBuffer) => void;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 封装 uni.connectSocket 的 hook,参考 ahooks useWebSocket
|
|
62
|
-
*
|
|
63
|
-
* @param url WebSocket 地址
|
|
64
|
-
* @param options 配置项
|
|
65
|
-
*/
|
|
66
|
-
const useWebsocket = (url: string, options: Options = {}): Result => {
|
|
67
|
-
const {
|
|
68
|
-
reconnectLimit = 3,
|
|
69
|
-
reconnectInterval = 3000,
|
|
70
|
-
heartbeat = false,
|
|
71
|
-
heartbeatInterval = 30000,
|
|
72
|
-
heartbeatMessage = 'ping',
|
|
73
|
-
manual = false,
|
|
74
|
-
onOpen,
|
|
75
|
-
onClose,
|
|
76
|
-
onMessage,
|
|
77
|
-
onError,
|
|
78
|
-
protocols,
|
|
79
|
-
} = options;
|
|
80
|
-
|
|
81
|
-
// 状态定义
|
|
82
|
-
const readyState = ref<ReadyState>(ReadyState.Closed);
|
|
83
|
-
const latestMessage = ref<UniApp.OnSocketMessageCallbackResult>();
|
|
84
|
-
const socketTask = ref<UniApp.SocketTask | null>(null);
|
|
85
|
-
|
|
86
|
-
// 内部辅助变量
|
|
87
|
-
let reconnectCount = 0;
|
|
88
|
-
let reconnectTimer: any = null;
|
|
89
|
-
let heartbeatTimer: any = null;
|
|
90
|
-
let isManualClose = false;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* 清除心跳定时器
|
|
94
|
-
*/
|
|
95
|
-
const clearHeartbeat = () => {
|
|
96
|
-
if (heartbeatTimer) {
|
|
97
|
-
clearInterval(heartbeatTimer);
|
|
98
|
-
heartbeatTimer = null;
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 开始心跳
|
|
104
|
-
*/
|
|
105
|
-
const startHeartbeat = () => {
|
|
106
|
-
if (!heartbeat) return;
|
|
107
|
-
clearHeartbeat();
|
|
108
|
-
heartbeatTimer = setInterval(() => {
|
|
109
|
-
if (readyState.value === ReadyState.Open && socketTask.value) {
|
|
110
|
-
// console.log('[useWebsocket] Heartbeat Ping');
|
|
111
|
-
socketTask.value.send({
|
|
112
|
-
data: heartbeatMessage,
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
}, heartbeatInterval);
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* 执行连接逻辑
|
|
120
|
-
*/
|
|
121
|
-
const connect = () => {
|
|
122
|
-
// 如果有正在等待的重连,先清除
|
|
123
|
-
if (reconnectTimer) {
|
|
124
|
-
clearTimeout(reconnectTimer);
|
|
125
|
-
reconnectTimer = null;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// 清理旧的心跳
|
|
129
|
-
clearHeartbeat();
|
|
130
|
-
|
|
131
|
-
// 如果当前已有任务,先尝试关闭
|
|
132
|
-
if (socketTask.value) {
|
|
133
|
-
socketTask.value.close({});
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
readyState.value = ReadyState.Connecting;
|
|
137
|
-
isManualClose = false;
|
|
138
|
-
|
|
139
|
-
// 创建 Uni Socket 任务
|
|
140
|
-
const task = uni.connectSocket({
|
|
141
|
-
url,
|
|
142
|
-
protocols,
|
|
143
|
-
complete: () => {},
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
socketTask.value = task;
|
|
147
|
-
|
|
148
|
-
// 监听开启事件
|
|
149
|
-
task.onOpen((res) => {
|
|
150
|
-
readyState.value = ReadyState.Open;
|
|
151
|
-
reconnectCount = 0; // 重连计数归零
|
|
152
|
-
startHeartbeat(); // 开始心跳
|
|
153
|
-
onOpen?.(task, res);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
// 监听消息事件
|
|
157
|
-
task.onMessage((res) => {
|
|
158
|
-
latestMessage.value = res;
|
|
159
|
-
onMessage?.(res);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
// 监听错误事件
|
|
163
|
-
task.onError((res) => {
|
|
164
|
-
readyState.value = ReadyState.Closed;
|
|
165
|
-
clearHeartbeat();
|
|
166
|
-
onError?.(res);
|
|
167
|
-
|
|
168
|
-
// 只有非手动关闭才会尝试重连
|
|
169
|
-
if (!isManualClose) {
|
|
170
|
-
reconnect();
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
// 监听关闭事件
|
|
175
|
-
task.onClose((res) => {
|
|
176
|
-
readyState.value = ReadyState.Closed;
|
|
177
|
-
clearHeartbeat();
|
|
178
|
-
onClose?.(res);
|
|
179
|
-
|
|
180
|
-
// 如果非用户手动关闭,尝试重连
|
|
181
|
-
if (!isManualClose) {
|
|
182
|
-
reconnect();
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* 重连逻辑
|
|
189
|
-
*/
|
|
190
|
-
const reconnect = () => {
|
|
191
|
-
if (reconnectCount < reconnectLimit && !isManualClose) {
|
|
192
|
-
reconnectTimer = setTimeout(() => {
|
|
193
|
-
reconnectCount++;
|
|
194
|
-
connect();
|
|
195
|
-
}, reconnectInterval);
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* 断开连接
|
|
201
|
-
*/
|
|
202
|
-
const disconnect = () => {
|
|
203
|
-
isManualClose = true;
|
|
204
|
-
reconnectCount = reconnectLimit; // 标记停止重连循环
|
|
205
|
-
clearHeartbeat();
|
|
206
|
-
if (reconnectTimer) {
|
|
207
|
-
clearTimeout(reconnectTimer);
|
|
208
|
-
reconnectTimer = null;
|
|
209
|
-
}
|
|
210
|
-
if (socketTask.value) {
|
|
211
|
-
socketTask.value.close({});
|
|
212
|
-
socketTask.value = null;
|
|
213
|
-
}
|
|
214
|
-
readyState.value = ReadyState.Closed;
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* 发送消息
|
|
219
|
-
* @param data 字符串或二进制数据
|
|
220
|
-
*/
|
|
221
|
-
const sendMessage = (data: string | ArrayBuffer) => {
|
|
222
|
-
if (readyState.value === ReadyState.Open && socketTask.value) {
|
|
223
|
-
socketTask.value.send({
|
|
224
|
-
data,
|
|
225
|
-
});
|
|
226
|
-
} else {
|
|
227
|
-
console.error(
|
|
228
|
-
'[useWebsocket] WebSocket is not open. Current state:',
|
|
229
|
-
readyState.value,
|
|
230
|
-
);
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
// 默认自动开始连接
|
|
235
|
-
if (!manual) {
|
|
236
|
-
connect();
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// 组件卸载时自动清理
|
|
240
|
-
onUnmounted(() => {
|
|
241
|
-
disconnect();
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
return {
|
|
245
|
-
latestMessage,
|
|
246
|
-
readyState,
|
|
247
|
-
connect,
|
|
248
|
-
disconnect,
|
|
249
|
-
sendMessage,
|
|
250
|
-
};
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
export default useWebsocket;
|
|
1
|
+
import { ref, onUnmounted, type Ref } from 'vue';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* WebSocket 连接状态枚举
|
|
5
|
+
*/
|
|
6
|
+
export enum ReadyState {
|
|
7
|
+
Connecting = 0, // 连接中
|
|
8
|
+
Open = 1, // 已连接
|
|
9
|
+
Closing = 2, // 关闭中
|
|
10
|
+
Closed = 3, // 已关闭
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* useWebsocket 配置参数
|
|
15
|
+
*/
|
|
16
|
+
export interface Options {
|
|
17
|
+
/** 重连尝试次数限制,默认为 3 */
|
|
18
|
+
reconnectLimit?: number;
|
|
19
|
+
/** 重连时间间隔 (ms),默认为 3000 */
|
|
20
|
+
reconnectInterval?: number;
|
|
21
|
+
/** 是否开启心跳检查,默认为 false */
|
|
22
|
+
heartbeat?: boolean;
|
|
23
|
+
/** 心跳检查间隔 (ms),默认为 30000 */
|
|
24
|
+
heartbeatInterval?: number;
|
|
25
|
+
/** 心跳发送的内容,默认为 'ping' */
|
|
26
|
+
heartbeatMessage?: string | ArrayBuffer;
|
|
27
|
+
/** 是否手动控制连接,为 true 时需手动调用 connect(),默认为 false */
|
|
28
|
+
manual?: boolean;
|
|
29
|
+
/** 连接开启时的回调 */
|
|
30
|
+
onOpen?: (
|
|
31
|
+
instance: UniApp.SocketTask,
|
|
32
|
+
res: UniApp.OnSocketOpenCallbackResult,
|
|
33
|
+
) => void;
|
|
34
|
+
/** 连接关闭时的回调 */
|
|
35
|
+
onClose?: (event: UniApp.OnSocketCloseOptions) => void;
|
|
36
|
+
/** 收到消息时的回调 */
|
|
37
|
+
onMessage?: (message: UniApp.OnSocketMessageCallbackResult) => void;
|
|
38
|
+
/** 连接发生错误时的回调 */
|
|
39
|
+
onError?: (event: UniApp.GeneralCallbackResult) => void;
|
|
40
|
+
/** WebSocket 子协议 */
|
|
41
|
+
protocols?: string[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* useWebsocket 返回结果
|
|
46
|
+
*/
|
|
47
|
+
export interface Result {
|
|
48
|
+
/** 最近收到的一条消息 */
|
|
49
|
+
latestMessage: Ref<UniApp.OnSocketMessageCallbackResult | undefined>;
|
|
50
|
+
/** 当前连接状态 */
|
|
51
|
+
readyState: Ref<ReadyState>;
|
|
52
|
+
/** 手动连接方法 */
|
|
53
|
+
connect: () => void;
|
|
54
|
+
/** 手动断开方法 */
|
|
55
|
+
disconnect: () => void;
|
|
56
|
+
/** 发送消息方法 */
|
|
57
|
+
sendMessage: (data: string | ArrayBuffer) => void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 封装 uni.connectSocket 的 hook,参考 ahooks useWebSocket
|
|
62
|
+
*
|
|
63
|
+
* @param url WebSocket 地址
|
|
64
|
+
* @param options 配置项
|
|
65
|
+
*/
|
|
66
|
+
const useWebsocket = (url: string, options: Options = {}): Result => {
|
|
67
|
+
const {
|
|
68
|
+
reconnectLimit = 3,
|
|
69
|
+
reconnectInterval = 3000,
|
|
70
|
+
heartbeat = false,
|
|
71
|
+
heartbeatInterval = 30000,
|
|
72
|
+
heartbeatMessage = 'ping',
|
|
73
|
+
manual = false,
|
|
74
|
+
onOpen,
|
|
75
|
+
onClose,
|
|
76
|
+
onMessage,
|
|
77
|
+
onError,
|
|
78
|
+
protocols,
|
|
79
|
+
} = options;
|
|
80
|
+
|
|
81
|
+
// 状态定义
|
|
82
|
+
const readyState = ref<ReadyState>(ReadyState.Closed);
|
|
83
|
+
const latestMessage = ref<UniApp.OnSocketMessageCallbackResult>();
|
|
84
|
+
const socketTask = ref<UniApp.SocketTask | null>(null);
|
|
85
|
+
|
|
86
|
+
// 内部辅助变量
|
|
87
|
+
let reconnectCount = 0;
|
|
88
|
+
let reconnectTimer: any = null;
|
|
89
|
+
let heartbeatTimer: any = null;
|
|
90
|
+
let isManualClose = false;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 清除心跳定时器
|
|
94
|
+
*/
|
|
95
|
+
const clearHeartbeat = () => {
|
|
96
|
+
if (heartbeatTimer) {
|
|
97
|
+
clearInterval(heartbeatTimer);
|
|
98
|
+
heartbeatTimer = null;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 开始心跳
|
|
104
|
+
*/
|
|
105
|
+
const startHeartbeat = () => {
|
|
106
|
+
if (!heartbeat) return;
|
|
107
|
+
clearHeartbeat();
|
|
108
|
+
heartbeatTimer = setInterval(() => {
|
|
109
|
+
if (readyState.value === ReadyState.Open && socketTask.value) {
|
|
110
|
+
// console.log('[useWebsocket] Heartbeat Ping');
|
|
111
|
+
socketTask.value.send({
|
|
112
|
+
data: heartbeatMessage,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}, heartbeatInterval);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 执行连接逻辑
|
|
120
|
+
*/
|
|
121
|
+
const connect = () => {
|
|
122
|
+
// 如果有正在等待的重连,先清除
|
|
123
|
+
if (reconnectTimer) {
|
|
124
|
+
clearTimeout(reconnectTimer);
|
|
125
|
+
reconnectTimer = null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 清理旧的心跳
|
|
129
|
+
clearHeartbeat();
|
|
130
|
+
|
|
131
|
+
// 如果当前已有任务,先尝试关闭
|
|
132
|
+
if (socketTask.value) {
|
|
133
|
+
socketTask.value.close({});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
readyState.value = ReadyState.Connecting;
|
|
137
|
+
isManualClose = false;
|
|
138
|
+
|
|
139
|
+
// 创建 Uni Socket 任务
|
|
140
|
+
const task = uni.connectSocket({
|
|
141
|
+
url,
|
|
142
|
+
protocols,
|
|
143
|
+
complete: () => {},
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
socketTask.value = task;
|
|
147
|
+
|
|
148
|
+
// 监听开启事件
|
|
149
|
+
task.onOpen((res) => {
|
|
150
|
+
readyState.value = ReadyState.Open;
|
|
151
|
+
reconnectCount = 0; // 重连计数归零
|
|
152
|
+
startHeartbeat(); // 开始心跳
|
|
153
|
+
onOpen?.(task, res);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// 监听消息事件
|
|
157
|
+
task.onMessage((res) => {
|
|
158
|
+
latestMessage.value = res;
|
|
159
|
+
onMessage?.(res);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// 监听错误事件
|
|
163
|
+
task.onError((res) => {
|
|
164
|
+
readyState.value = ReadyState.Closed;
|
|
165
|
+
clearHeartbeat();
|
|
166
|
+
onError?.(res);
|
|
167
|
+
|
|
168
|
+
// 只有非手动关闭才会尝试重连
|
|
169
|
+
if (!isManualClose) {
|
|
170
|
+
reconnect();
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// 监听关闭事件
|
|
175
|
+
task.onClose((res) => {
|
|
176
|
+
readyState.value = ReadyState.Closed;
|
|
177
|
+
clearHeartbeat();
|
|
178
|
+
onClose?.(res);
|
|
179
|
+
|
|
180
|
+
// 如果非用户手动关闭,尝试重连
|
|
181
|
+
if (!isManualClose) {
|
|
182
|
+
reconnect();
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 重连逻辑
|
|
189
|
+
*/
|
|
190
|
+
const reconnect = () => {
|
|
191
|
+
if (reconnectCount < reconnectLimit && !isManualClose) {
|
|
192
|
+
reconnectTimer = setTimeout(() => {
|
|
193
|
+
reconnectCount++;
|
|
194
|
+
connect();
|
|
195
|
+
}, reconnectInterval);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 断开连接
|
|
201
|
+
*/
|
|
202
|
+
const disconnect = () => {
|
|
203
|
+
isManualClose = true;
|
|
204
|
+
reconnectCount = reconnectLimit; // 标记停止重连循环
|
|
205
|
+
clearHeartbeat();
|
|
206
|
+
if (reconnectTimer) {
|
|
207
|
+
clearTimeout(reconnectTimer);
|
|
208
|
+
reconnectTimer = null;
|
|
209
|
+
}
|
|
210
|
+
if (socketTask.value) {
|
|
211
|
+
socketTask.value.close({});
|
|
212
|
+
socketTask.value = null;
|
|
213
|
+
}
|
|
214
|
+
readyState.value = ReadyState.Closed;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 发送消息
|
|
219
|
+
* @param data 字符串或二进制数据
|
|
220
|
+
*/
|
|
221
|
+
const sendMessage = (data: string | ArrayBuffer) => {
|
|
222
|
+
if (readyState.value === ReadyState.Open && socketTask.value) {
|
|
223
|
+
socketTask.value.send({
|
|
224
|
+
data,
|
|
225
|
+
});
|
|
226
|
+
} else {
|
|
227
|
+
console.error(
|
|
228
|
+
'[useWebsocket] WebSocket is not open. Current state:',
|
|
229
|
+
readyState.value,
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
// 默认自动开始连接
|
|
235
|
+
if (!manual) {
|
|
236
|
+
connect();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// 组件卸载时自动清理
|
|
240
|
+
onUnmounted(() => {
|
|
241
|
+
disconnect();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
return {
|
|
245
|
+
latestMessage,
|
|
246
|
+
readyState,
|
|
247
|
+
connect,
|
|
248
|
+
disconnect,
|
|
249
|
+
sendMessage,
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
export default useWebsocket;
|
package/dist/v3/styles/theme.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
export const THEME_MAIN_COLORS: Record<string, string> = {
|
|
2
|
-
light: '#9254de',
|
|
3
|
-
dark: '#9254de',
|
|
4
|
-
eye: '#6c71c4',
|
|
5
|
-
midnight: '#38bdf8',
|
|
6
|
-
gulf: '#2dd4bf',
|
|
7
|
-
lava: '#f97316',
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export const getThemeMainColor = (theme: string = 'light') => {
|
|
11
|
-
return THEME_MAIN_COLORS[theme] || THEME_MAIN_COLORS.light;
|
|
12
|
-
};
|
|
1
|
+
export const THEME_MAIN_COLORS: Record<string, string> = {
|
|
2
|
+
light: '#9254de',
|
|
3
|
+
dark: '#9254de',
|
|
4
|
+
eye: '#6c71c4',
|
|
5
|
+
midnight: '#38bdf8',
|
|
6
|
+
gulf: '#2dd4bf',
|
|
7
|
+
lava: '#f97316',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const getThemeMainColor = (theme: string = 'light') => {
|
|
11
|
+
return THEME_MAIN_COLORS[theme] || THEME_MAIN_COLORS.light;
|
|
12
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-uni-dev-tool",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "vite-uni-dev-tool, debug, uni-app, 一处编写,到处调试",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vite",
|
|
@@ -33,6 +33,14 @@
|
|
|
33
33
|
"types": "./dist/core.d.ts",
|
|
34
34
|
"import": "./dist/core.js"
|
|
35
35
|
},
|
|
36
|
+
"./hooks/*": {
|
|
37
|
+
"types": "./dist/v3/hooks/*/index.d.ts",
|
|
38
|
+
"import": "./dist/v3/hooks/*/index.ts"
|
|
39
|
+
},
|
|
40
|
+
"./dist/v3/hooks/*": {
|
|
41
|
+
"types": "./dist/v3/hooks/*/index.d.ts",
|
|
42
|
+
"import": "./dist/v3/hooks/*/index.ts"
|
|
43
|
+
},
|
|
36
44
|
"./dist/*": "./dist/*",
|
|
37
45
|
"./shims-uni.d.ts": "./dist/shims-uni.d.ts"
|
|
38
46
|
},
|