feat(web-antd): 实现电表数据实时推送功能
- 新增 WebSocket 服务,用于接收实时推送的电表数据
This commit is contained in:
@@ -72,5 +72,5 @@ export function queryTree(params?: any) {
|
||||
* 获取水/电/气表当前读数/状态
|
||||
*/
|
||||
export function currentReading(params?: any) {
|
||||
return requestClient.get<MeterInfoVO[]>(`/property/meterInfo/currentReading`, { params })
|
||||
return requestClient.get<void>(`/property/meterInfo/currentReading`, { params })
|
||||
}
|
162
apps/web-antd/src/api/websocket.ts
Normal file
162
apps/web-antd/src/api/websocket.ts
Normal file
@@ -0,0 +1,162 @@
|
||||
import { useAccessStore } from '@vben/stores';
|
||||
|
||||
interface WebSocketCallbacks {
|
||||
onOpen?: (event: Event) => void;
|
||||
onMessage?: (event: MessageEvent) => void;
|
||||
onError?: (error: Event) => void;
|
||||
onClose?: (event: CloseEvent) => void;
|
||||
}
|
||||
|
||||
export default class WebSocketService {
|
||||
private webSocket: WebSocket | null = null;
|
||||
private webSocketURL: string = '';
|
||||
|
||||
// 默认回调函数
|
||||
private onOpenCallback: (event: Event) => void;
|
||||
private onMessageCallback: (event: MessageEvent) => void;
|
||||
private onErrorCallback: (error: Event) => void;
|
||||
private onCloseCallback: (event: CloseEvent) => void;
|
||||
|
||||
constructor(callbacks?: WebSocketCallbacks) {
|
||||
// 设置回调函数,使用自定义回调或默认回调
|
||||
this.onOpenCallback = callbacks?.onOpen || this.defaultOnOpen;
|
||||
this.onMessageCallback = callbacks?.onMessage || this.defaultOnMessage;
|
||||
this.onErrorCallback = callbacks?.onError || this.defaultOnError;
|
||||
this.onCloseCallback = callbacks?.onClose || this.defaultOnClose;
|
||||
}
|
||||
|
||||
// 初始化WebSocket连接
|
||||
initWebSocket(webSocketURL: string): void {
|
||||
this.webSocketURL = webSocketURL;
|
||||
try {
|
||||
this.webSocket = new WebSocket(webSocketURL);
|
||||
|
||||
this.webSocket.onopen = this.onOpenCallback;
|
||||
this.webSocket.onmessage = this.onMessageCallback;
|
||||
this.webSocket.onerror = this.onErrorCallback;
|
||||
this.webSocket.onclose = this.onCloseCallback;
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize WebSocket:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置onOpen回调并更新WebSocket事件处理器
|
||||
setOnOpenCallback(callback: (event: Event) => void): void {
|
||||
this.onOpenCallback = callback;
|
||||
if (this.webSocket) {
|
||||
this.webSocket.onopen = this.onOpenCallback;
|
||||
}
|
||||
}
|
||||
|
||||
// 设置onMessage回调并更新WebSocket事件处理器
|
||||
setOnMessageCallback(callback: (event: MessageEvent) => void): void {
|
||||
this.onMessageCallback = callback;
|
||||
if (this.webSocket) {
|
||||
this.webSocket.onmessage = this.onMessageCallback;
|
||||
}
|
||||
}
|
||||
|
||||
// 设置onError回调并更新WebSocket事件处理器
|
||||
setOnErrorCallback(callback: (error: Event) => void): void {
|
||||
this.onErrorCallback = callback;
|
||||
if (this.webSocket) {
|
||||
this.webSocket.onerror = this.onErrorCallback;
|
||||
}
|
||||
}
|
||||
|
||||
// 设置onClose回调并更新WebSocket事件处理器
|
||||
setOnCloseCallback(callback: (event: CloseEvent) => void): void {
|
||||
this.onCloseCallback = callback;
|
||||
if (this.webSocket) {
|
||||
this.webSocket.onclose = this.onCloseCallback;
|
||||
}
|
||||
}
|
||||
|
||||
// 默认连接建立成功的回调
|
||||
private defaultOnOpen(event: Event): void {
|
||||
console.log('WebSocket连接建立成功', event);
|
||||
}
|
||||
|
||||
// 默认收到服务器消息的回调
|
||||
private defaultOnMessage(event: MessageEvent): void {
|
||||
console.log('收到服务器消息', event.data);
|
||||
// 通常这里会解析数据并更新页面
|
||||
// const data = JSON.parse(event.data);
|
||||
// 根据消息类型处理不同业务...
|
||||
}
|
||||
|
||||
// 默认发生错误的回调
|
||||
private defaultOnError(error: Event): void {
|
||||
console.error('WebSocket连接错误', error);
|
||||
}
|
||||
|
||||
// 默认连接关闭的回调
|
||||
private defaultOnClose(event: CloseEvent): void {
|
||||
console.log('WebSocket连接关闭', event);
|
||||
}
|
||||
|
||||
// 关闭连接
|
||||
close(): void {
|
||||
if (this.webSocket) {
|
||||
this.webSocket.close();
|
||||
this.webSocket = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 发送消息
|
||||
sendMessage(message: string | object): void {
|
||||
if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) {
|
||||
const dataToSend = typeof message === 'string' ? message : JSON.stringify(message);
|
||||
this.webSocket.send(dataToSend);
|
||||
} else {
|
||||
console.error('WebSocket连接未就绪');
|
||||
}
|
||||
}
|
||||
|
||||
// 获取当前WebSocket状态
|
||||
getReadyState(): number | null {
|
||||
return this.webSocket ? this.webSocket.readyState : null;
|
||||
}
|
||||
|
||||
// 获取WebSocket URL
|
||||
getWebSocketURL(): string {
|
||||
return this.webSocketURL;
|
||||
}
|
||||
|
||||
// 重新连接
|
||||
reconnect(): void {
|
||||
if (this.webSocketURL) {
|
||||
this.close();
|
||||
this.initWebSocket(this.webSocketURL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 创建一个可导出的WebSocket实例
|
||||
let globalWebSocketService: WebSocketService | null = null;
|
||||
|
||||
/**
|
||||
* 初始化WebSocket连接的可导出方法
|
||||
* @param url WebSocket服务器地址
|
||||
* @param callbacks 回调函数对象(可选)
|
||||
* @returns WebSocketService实例
|
||||
*/
|
||||
export function initWebSocket(callbacks?: WebSocketCallbacks): void {
|
||||
if (!globalWebSocketService) {
|
||||
globalWebSocketService = new WebSocketService(callbacks);
|
||||
}
|
||||
const accessStore = useAccessStore();
|
||||
const clinetId = import.meta.env.VITE_GLOB_APP_CLIENT_ID;
|
||||
const api = import.meta.env.VITE_GLOB_API_URL;
|
||||
const host = window.location.host;
|
||||
const url = `ws://${host}${api}/resource/websocket?clientid=${clinetId}&Authorization=Bearer ${accessStore.accessToken}`;
|
||||
globalWebSocketService.initWebSocket(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全局WebSocket服务实例
|
||||
* @returns WebSocketService实例或null
|
||||
*/
|
||||
export function getWebSocketService(): WebSocketService | null {
|
||||
return globalWebSocketService;
|
||||
}
|
Reference in New Issue
Block a user