import { useEffect, useState } from "react"; import { useFiltering } from "../../hooks/useFiltering"; function createSocketObject(data, validos, response) { //console.log("criando valuidos"); var socketObject = { name: data.name, socket: response, }; validos.push(socketObject); // console.log(validos); } function isRequestedValue(item, renderOptions) { if ( (item.name == "pluviometer" && renderOptions.citzen.pluviometer) || (item.name == "automaticPluviometer" && renderOptions.oficial.automaticPluviometer) || (item.name == "rain" && renderOptions.citzen.rain) || (item.name == "riverFlood" && renderOptions.citzen.riverFlood) || (item.name == "floodZones" && renderOptions.citzen.floodRisk) || (item.name == "susceptibilityAreas" && renderOptions.oficial.susceptibilityAreas) ) { return true; } } function verify_existingSockets(formType, renderOptions, validos) { if (formType.response == null && isRequestedValue(formType, renderOptions)) { //console.log("Criou socket: " + formType.name); const socket = new WebSocket( "wss://waterproofing.geog.uni-heidelberg.de/wss/hot/data?" + formType.endpoint ); formType.socketUrl = socket; createSocketObject(formType, validos, socket); return socket; } else { return null; } } export default function SocketClient(dataOptionObject, focusChanged) { const [data, setData] = useState(useFiltering().forms); var validos = []; const socket = verify_existingSockets(data[1], dataOptionObject, validos); const [dados, setDado] = useState(); useEffect(() => { console.log(dados); // data.map((e) => verify_existingSockets(e, dataOptionObject, validos)); if (socket && dados != 'undefined') { console.log("ESTÁ PASSANDO AQUI!!"); data[1].socketUrl.onmessage = ({ data }) => { // data[1].response = data setDado(data); // console.log("pegou espostas"); }; } else { setDado(null); } //console.log(dados); }, [dataOptionObject, focusChanged]); return dados; // //console.log("data socket client: "+ JSON.stringify(data)); // return data.response; }