docs(wss): more ws stuff
This commit is contained in:
@@ -1,35 +1,41 @@
|
|||||||
import { useState } from 'react'
|
import { useState } from "react";
|
||||||
import reactLogo from './assets/react.svg'
|
import reactLogo from "./assets/react.svg";
|
||||||
import viteLogo from '/vite.svg'
|
import viteLogo from "/vite.svg";
|
||||||
import './App.css'
|
import "./App.css";
|
||||||
|
import WebSocketViewer from "./WebSocketTest";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [count, setCount] = useState(0)
|
const [count, setCount] = useState(0);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div>
|
<div>
|
||||||
<a href="https://vite.dev" target="_blank">
|
<a href="https://vite.dev" target="_blank">
|
||||||
<img src={viteLogo} className="logo" alt="Vite logo" />
|
<img src={viteLogo} className="logo" alt="Vite logo" />
|
||||||
</a>
|
</a>
|
||||||
<a href="https://react.dev" target="_blank">
|
<a href="https://react.dev" target="_blank">
|
||||||
<img src={reactLogo} className="logo react" alt="React logo" />
|
<img
|
||||||
</a>
|
src={reactLogo}
|
||||||
</div>
|
className="logo react"
|
||||||
<h1>Vite + React</h1>
|
alt="React logo"
|
||||||
<div className="card">
|
/>
|
||||||
<button onClick={() => setCount((count) => count + 1)}>
|
</a>
|
||||||
count is {count}
|
</div>
|
||||||
</button>
|
<h1>Vite + React</h1>
|
||||||
<p>
|
<div className="card">
|
||||||
Edit <code>src/App.tsx</code> and save to test HMR
|
<button onClick={() => setCount((count) => count + 1)}>
|
||||||
</p>
|
count is {count}
|
||||||
</div>
|
</button>
|
||||||
<p className="read-the-docs">
|
<p>
|
||||||
Click on the Vite and React logos to learn more
|
Edit <code>src/App.tsx</code> and save to test HMR
|
||||||
</p>
|
</p>
|
||||||
</>
|
</div>
|
||||||
)
|
<p className="read-the-docs">
|
||||||
|
Click on the Vite and React logos to learn more
|
||||||
|
</p>
|
||||||
|
<WebSocketViewer />
|
||||||
|
</>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App
|
export default App;
|
||||||
|
|||||||
41
frontend/src/WebSocketTest.tsx
Normal file
41
frontend/src/WebSocketTest.tsx
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { useEffect, useRef } from "react";
|
||||||
|
|
||||||
|
const WebSocketViewer = () => {
|
||||||
|
const ws = useRef<any>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Connect to your Go backend WebSocket endpoint
|
||||||
|
ws.current = new WebSocket(
|
||||||
|
(window.location.protocol === "https:" ? "wss://" : "ws://") +
|
||||||
|
window.location.host +
|
||||||
|
"/lst/api/logger/logs"
|
||||||
|
);
|
||||||
|
|
||||||
|
ws.current.onopen = () => {
|
||||||
|
console.log("[WebSocket] Connected");
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.current.onmessage = (event: any) => {
|
||||||
|
console.log("[WebSocket] Message received:", event.data);
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.current.onerror = (error: any) => {
|
||||||
|
console.error("[WebSocket] Error:", error);
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.current.onclose = () => {
|
||||||
|
console.log("[WebSocket] Disconnected");
|
||||||
|
};
|
||||||
|
|
||||||
|
// Cleanup on unmount
|
||||||
|
return () => {
|
||||||
|
if (ws.current) {
|
||||||
|
ws.current.close();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return <div>Check the console for WebSocket messages</div>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default WebSocketViewer;
|
||||||
Reference in New Issue
Block a user