Add base debug WS route
This commit is contained in:
@@ -14,6 +14,7 @@ import { HomeRoute } from "./routes/HomeRoute";
|
|||||||
import { MatrixAuthCallback } from "./routes/MatrixAuthCallback";
|
import { MatrixAuthCallback } from "./routes/MatrixAuthCallback";
|
||||||
import { MatrixLinkRoute } from "./routes/MatrixLinkRoute";
|
import { MatrixLinkRoute } from "./routes/MatrixLinkRoute";
|
||||||
import { NotFoundRoute } from "./routes/NotFoundRoute";
|
import { NotFoundRoute } from "./routes/NotFoundRoute";
|
||||||
|
import { WSDebugRoute } from "./routes/WSDebugRoute";
|
||||||
import { BaseLoginPage } from "./widgets/auth/BaseLoginPage";
|
import { BaseLoginPage } from "./widgets/auth/BaseLoginPage";
|
||||||
import BaseAuthenticatedPage from "./widgets/dashboard/BaseAuthenticatedPage";
|
import BaseAuthenticatedPage from "./widgets/dashboard/BaseAuthenticatedPage";
|
||||||
|
|
||||||
@@ -43,6 +44,7 @@ export function App(): React.ReactElement {
|
|||||||
<Route path="matrix_link" element={<MatrixLinkRoute />} />
|
<Route path="matrix_link" element={<MatrixLinkRoute />} />
|
||||||
<Route path="matrix_auth_cb" element={<MatrixAuthCallback />} />
|
<Route path="matrix_auth_cb" element={<MatrixAuthCallback />} />
|
||||||
<Route path="tokens" element={<APITokensRoute />} />
|
<Route path="tokens" element={<APITokensRoute />} />
|
||||||
|
<Route path="wsdebug" element={<WSDebugRoute />} />
|
||||||
<Route path="*" element={<NotFoundRoute />} />
|
<Route path="*" element={<NotFoundRoute />} />
|
||||||
</Route>
|
</Route>
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
15
matrixgw_frontend/src/api/WsApi.ts
Normal file
15
matrixgw_frontend/src/api/WsApi.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { APIClient } from "./ApiClient";
|
||||||
|
|
||||||
|
export type WsMessage = {
|
||||||
|
type: string;
|
||||||
|
[k: string]: any;
|
||||||
|
};
|
||||||
|
|
||||||
|
export class WsApi {
|
||||||
|
/**
|
||||||
|
* Get WebSocket URL
|
||||||
|
*/
|
||||||
|
static get WsURL(): string {
|
||||||
|
return APIClient.backendURL() + "/ws";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -256,7 +256,7 @@ function SyncThreadStatus(): React.ReactElement {
|
|||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const interval = setInterval(loadStatus, 1000);
|
const interval = setInterval(loadStatus, 1000);
|
||||||
|
|
||||||
() => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
54
matrixgw_frontend/src/routes/WSDebugRoute.tsx
Normal file
54
matrixgw_frontend/src/routes/WSDebugRoute.tsx
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { WsApi, type WsMessage } from "../api/WsApi";
|
||||||
|
import { useSnackbar } from "../hooks/contexts_provider/SnackbarProvider";
|
||||||
|
import { time } from "../utils/DateUtils";
|
||||||
|
import { MatrixGWRouteContainer } from "../widgets/MatrixGWRouteContainer";
|
||||||
|
|
||||||
|
const State = {
|
||||||
|
Closed: "Closed",
|
||||||
|
Connected: "Connected",
|
||||||
|
Error: "Error",
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
type TimestampedMessages = WsMessage & { time: number };
|
||||||
|
|
||||||
|
export function WSDebugRoute(): React.ReactElement {
|
||||||
|
const snackbar = useSnackbar();
|
||||||
|
|
||||||
|
const [state, setState] = React.useState<string>(State.Closed);
|
||||||
|
const wsRef = React.useRef<WebSocket | undefined>(undefined);
|
||||||
|
|
||||||
|
const [messages, setMessages] = React.useState<TimestampedMessages[]>([]);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const ws = new WebSocket(WsApi.WsURL);
|
||||||
|
wsRef.current = ws;
|
||||||
|
|
||||||
|
ws.onopen = () => setState(State.Connected);
|
||||||
|
ws.onerror = (e) => {
|
||||||
|
console.error(`WS Debug error! ${e}`);
|
||||||
|
snackbar(`WebSocket error! ${e}`);
|
||||||
|
setState(State.Error);
|
||||||
|
};
|
||||||
|
ws.onclose = () => {
|
||||||
|
setState(State.Closed);
|
||||||
|
wsRef.current = undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onmessage = (msg) => {
|
||||||
|
const dec = JSON.parse(msg.data);
|
||||||
|
setMessages((l) => {
|
||||||
|
return [{ time: time(), ...dec }, ...l];
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return () => ws.close();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<MatrixGWRouteContainer label={"WebSocket Debug"}>
|
||||||
|
State: {state}
|
||||||
|
{JSON.stringify(messages)}
|
||||||
|
</MatrixGWRouteContainer>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user