feat: implement ws

This commit is contained in:
Thibaut Valentin 2025-11-21 14:03:06 +01:00
parent a83088387b
commit b78b3f005b
2 changed files with 89 additions and 47 deletions

View File

@ -45,6 +45,9 @@ siren-api.key=siren-ap
quarkus.rest-client."fr.titionfire.ffsaf.rest.client.SirenService".url=https://data.siren-api.fr/
quarkus.rest-client."fr.titionfire.ffsaf.rest.client.StateIdService".url=https://www.data-asso.fr/api/
quarkus.websockets-next.server.auto-ping-interval=PT10s
quarkus.websockets-next.client.connection-idle-timeout=PT30s
#Login
quarkus.oidc.token-state-manager.split-tokens=true
quarkus.oidc.token.refresh-expired=true

View File

@ -1,4 +1,4 @@
import {createContext, useContext, useEffect, useReducer, useRef, useState} from "react";
import {createContext, useContext, useEffect, useId, useReducer, useRef, useState} from "react";
function uuidv4() {
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c =>
@ -36,8 +36,10 @@ function reducer(state, action) {
}
}
const mountCounter = {};
export function WSProvider({url, onmessage, children}) {
const id = useId();
const [isReady, setIsReady] = useState(false)
const [state, dispatch] = useReducer(reducer, {listener: []})
const ws = useRef(null)
@ -49,11 +51,33 @@ export function WSProvider({url, onmessage, children}) {
}, [state.listener])
useEffect(() => {
if (!mountCounter[id])
mountCounter[id] = 0
mountCounter[id] += 1
console.log(`WSProvider ${id} mounted ${mountCounter[id]} time(s)`);
if (mountCounter[id] === 1 && (ws.current === null || ws.current.readyState >= WebSocket.CLOSING)){
console.log("WSProvider: connecting to", url);
const socket = new WebSocket(url)
socket.onopen = () => setIsReady(true)
socket.onclose = () => setIsReady(false)
socket.onclose = () => {
setIsReady(false)
if (mountCounter[id] > 0) {
console.log("WSProvider: reconnecting to", url);
setTimeout(() => {
try {
const newSocket = new WebSocket(url)
ws.current = newSocket
newSocket.onopen = socket.onopen
newSocket.onclose = socket.onclose
newSocket.onmessage = socket.onmessage
}catch (e) {
}
}, 5000)
}
}
socket.onmessage = (event) => {
const msg = JSON.parse(event.data)
@ -86,20 +110,45 @@ export function WSProvider({url, onmessage, children}) {
}
ws.current = socket
}
return () => {
socket.close()
mountCounter[id] -= 1
console.log(`WSProvider ${id} unmounted, ${mountCounter[id]} instance(s) remain`);
setTimeout(() => {
console.log(`WSProvider ${id} checking for close, ${mountCounter[id]} instance(s) remain`);
if (mountCounter[id] === 0) {
console.log("WSProvider: closing connection to", url);
ws.current.close()
}
}, 250)
}
}, [])
const send = (uuid, code, type, data, onReply = () => {
}, onError = () => {
const send = (uuid, code, type, data, resolve = () => {
}, reject = () => {
}) => {
if (!isReady) {
onError("WebSocket is not connected");
reject("WebSocket is not connected");
return;
}
if (type === "REQUEST") {
callbackRef.current[uuid] = {onReply: onReply, onError: onError}
const timeout = setTimeout(() => {
reject("timeout");
delete callbackRef.current[uuid];
}, 30000);
callbackRef.current[uuid] = {
onReply: (data) => {
clearTimeout(timeout);
resolve(data);
},
onError: (data) => {
clearTimeout(timeout);
reject(data);
}
}
}
ws.current?.send(JSON.stringify({
uuid: uuid,
@ -124,17 +173,7 @@ export function useWS() {
wait_length,
sendRequest: (code, data) => {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject("timeout");
}, 1000);
send(uuidv4(), code, "REQUEST", data, (data) => {
clearTimeout(timeout);
resolve(data);
}, (data) => {
clearTimeout(timeout);
reject(data);
});
send(uuidv4(), code, "REQUEST", data, resolve, reject);
})
},
sendNotify: (code, data) => {