Websocket
(Svelte)
import { writable } from "svelte/store";
export const GlobalStates = {};
export const WS = new WebSocket(`ws://${window.location.hostname}:8090/ws`);
export const Send = (obj) => {
WS.send(
JSON.stringify({
guid: guid,
msg: obj,
})
);
};
let guid = 0;
WS.onopen = (e) => {
guid = Date.now();
Send({
id: guid,
state: "connected",
});
GlobalStates[guid] = {};
};
WS.onmessage = (message) => {
const data = JSON.parse(message.data);
GlobalStates[data.msg.id] = data.msg;
console.log(GlobalStates);
};
// WS.onerror = (error) => alert(`[error] ${error.message}`);
WS.onclose = (event) => alert("Соединение прервано");Last updated