generated from lucien/api-template
fix: improved websocket usage
This commit is contained in:
parent
fbf7821320
commit
132b1f3c06
7 changed files with 135 additions and 79 deletions
|
@ -34,10 +34,15 @@ export default function ChannelsPage() {
|
|||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
const socket = new WebSocket("/api/channels");
|
||||
const socket = new WebSocket("/api/ws");
|
||||
|
||||
const id = setInterval(() => {
|
||||
socket.send(JSON.stringify("ping"));
|
||||
}, 10000);
|
||||
|
||||
socket.addEventListener('message', function (event) {
|
||||
if (event.data === "new_channel") {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === "new_channel") {
|
||||
axios
|
||||
.get("/api/channels")
|
||||
.then((res) => {
|
||||
|
@ -48,6 +53,11 @@ export default function ChannelsPage() {
|
|||
})
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
clearInterval(id);
|
||||
socket.close();
|
||||
}
|
||||
}, [])
|
||||
|
||||
if (!channels) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue