This commit is contained in:
2025-10-02 08:16:11 +01:00
parent 6d833c7d3b
commit 979728e6c8
30 changed files with 695 additions and 100 deletions
+4 -1
View File
@@ -73,7 +73,10 @@ export default function useWebSocket<
* # Connect to Websocket
*/
const connect = React.useCallback(() => {
const wsURL = url;
const domain = window.location.origin;
const wsURL = url.startsWith(`ws`)
? url
: domain.replace(/^http/, "ws") + ("/" + url).replace(/\/\//g, "/");
if (!wsURL) return;
let ws = new WebSocket(wsURL);
@@ -18,9 +18,10 @@ export default function useWebSocketEventHandler<
const dataEventListenerCallback = (e: Event) => {
const customEvent = e as CustomEvent;
const data = customEvent.detail.data as T | undefined;
const message = customEvent.detail.message as string | undefined;
const __msg = customEvent.detail.message as string | undefined;
if (data) setData(data);
if (message) setMessage(message);
if (__msg && typeof __msg == "string") setMessage(__msg);
};
const messageEventName: (typeof WebSocketEventNames)[number] =