fixed double ws bug

This commit is contained in:
anovazzi1 2023-05-05 12:42:59 -03:00
commit 8758dd85d5

View file

@ -26,6 +26,7 @@ export default function ChatModal({
const [chatHistory, setChatHistory] = useState<ChatMessageType[]>([]); const [chatHistory, setChatHistory] = useState<ChatMessageType[]>([]);
const { reactFlowInstance } = useContext(typesContext); const { reactFlowInstance } = useContext(typesContext);
const { setErrorData, setNoticeData } = useContext(alertContext); const { setErrorData, setNoticeData } = useContext(alertContext);
const [isStreaming,setIsStreaming] = useState()
const [ws, setWs] = useState<WebSocket | null>(null); const [ws, setWs] = useState<WebSocket | null>(null);
const [lockChat, setLockChat] = useState(false); const [lockChat, setLockChat] = useState(false);
const addChatHistory = ( const addChatHistory = (
@ -47,6 +48,10 @@ export default function ChatModal({
}); });
}; };
function checkOpenFunction(){
return open
}
function connectWS() { function connectWS() {
try { try {
const urlWs = const urlWs =
@ -60,6 +65,7 @@ export default function ChatModal({
newWs.onopen = () => { newWs.onopen = () => {
console.log("WebSocket connection established!"); console.log("WebSocket connection established!");
}; };
console.log(flow.id)
newWs.onmessage = (event) => { newWs.onmessage = (event) => {
setLockChat(false); setLockChat(false);
const data = JSON.parse(event.data); const data = JSON.parse(event.data);
@ -116,10 +122,12 @@ export default function ChatModal({
} }
}; };
newWs.onclose = (_) => { newWs.onclose = (_) => {
setLockChat(false); if(checkOpenFunction){
setTimeout(() => { setLockChat(false);
connectWS(); setTimeout(() => {
}, 1000); connectWS();
}, 1000);
}
}; };
newWs.onerror = (ev) => { newWs.onerror = (ev) => {
console.log(ev, "error"); console.log(ev, "error");
@ -142,7 +150,6 @@ export default function ChatModal({
useEffect(() => { useEffect(() => {
let newWs = connectWS(); let newWs = connectWS();
return () => { return () => {
console.log("trigger");
newWs.close(); newWs.close();
}; };
}, []); }, []);
@ -151,7 +158,6 @@ export default function ChatModal({
if (ws && (ws.readyState === ws.CLOSED || ws.readyState === ws.CLOSING)) { if (ws && (ws.readyState === ws.CLOSED || ws.readyState === ws.CLOSING)) {
let newWs = connectWS(); let newWs = connectWS();
return () => { return () => {
console.log("trigger");
newWs.close(); newWs.close();
}; };
} }