I have some code in React for websocket conexion in components, I tried to port the code to solidjs, but no success yet.
// Websockets
onMount(() => {
const initWebSocket = () => {
let hostname = window.location.hostname;
console.log(hostname);
if (hostname === "localhost") ws = new WebSocket("ws://localhost:5000/");
else ws = new WebSocket("ws://" + hostname + "/api/events/ws");
console.log(ws);
ws.onclose = onClose;
ws.onmessage = onMessage;
ws.onerror = onError;
};
const onClose = (e) => {
setTimeout(initWebSocket, 2000);
};
const onError = (event) => {
console.error("Error:" + event);
};
// Listener websocket message
//////////////////////////////////////////////////
const onMessage = (e) => {
console.log(e.data);
};
initWebSocket();
});
Any idea of how would look this to work correctly? the idea of the websockets is to update an http resource (I think I'll use the mutate of the createResource API)