Додана сторінка "Стенд"
Додане повідомлення про оновлення застосунку Оновлен Service Worker Перероблен WebSocket APІ
This commit is contained in:
@@ -3,9 +3,6 @@ let map_card;
|
||||
const Territory_card = {
|
||||
// Глобальні змінні стану
|
||||
id: null,
|
||||
socket: null,
|
||||
reconnectTimeout: null,
|
||||
reconnectAttempts: 0,
|
||||
listEntrances: [],
|
||||
listApartment: [],
|
||||
listBuilding: [],
|
||||
@@ -28,11 +25,6 @@ const Territory_card = {
|
||||
app.innerHTML = html;
|
||||
Territory_card.id = Id;
|
||||
|
||||
// Закриваємо старий WebSocket
|
||||
if (this.socket) this.socket.close(1000, "Перезапуск з'єднання");
|
||||
// this.cloud.start(makeid(6));
|
||||
this.cloud.start()
|
||||
|
||||
// Якщо це сторінка будинку, отримуємо під’їзди та стартуємо WebSocket
|
||||
if (type === "house") {
|
||||
const controls = document.getElementById('page-card-controls');
|
||||
@@ -45,6 +37,13 @@ const Territory_card = {
|
||||
this.getHomestead.map({});
|
||||
}
|
||||
|
||||
const ids = ['cloud_1', 'cloud_2', 'cloud_3'];
|
||||
ids.forEach((id, idx) => {
|
||||
const el = document.getElementById(id);
|
||||
if(!el) return;
|
||||
el.setAttribute('data-state', ['sync', 'ok', 'err'].indexOf(Cloud.status) === idx ? 'active' : '');
|
||||
});
|
||||
|
||||
// Додаємо обробник закриття попапу
|
||||
const popup = document.getElementById('card-new-date');
|
||||
if (!popup.dataset.listenerAdded) {
|
||||
@@ -59,71 +58,6 @@ const Territory_card = {
|
||||
|
||||
// Робота з WebSocket
|
||||
cloud: {
|
||||
start() {
|
||||
const uuid = localStorage.getItem("uuid");
|
||||
const ws = new WebSocket(CONFIG.wss, uuid);
|
||||
Territory_card.socket = ws;
|
||||
|
||||
ws.onopen = () => {
|
||||
console.log("[WebSocket] З'єднання встановлено");
|
||||
Territory_card.cloud.setStatus('ok');
|
||||
ws.send(JSON.stringify({
|
||||
event: 'connection',
|
||||
id: getTimeInSeconds(),
|
||||
date: getTimeInSeconds(),
|
||||
uuid,
|
||||
user: {
|
||||
name: USER.name,
|
||||
id: USER.id
|
||||
},
|
||||
data: {}
|
||||
}));
|
||||
Territory_card.reconnectAttempts = 0;
|
||||
clearTimeout(Territory_card.reconnectTimeout);
|
||||
};
|
||||
|
||||
ws.onmessage = (e) => {
|
||||
const data = JSON.parse(e.data);
|
||||
if (data.event === 'connection' && data.user.id !== USER.id) {
|
||||
console.log(`Новий користувач: ${data.user}`);
|
||||
}
|
||||
if (data.event === 'message') {
|
||||
Territory_card.cloud.update(data);
|
||||
}
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
console.warn("[WebSocket] З'єднання розірвано");
|
||||
Territory_card.cloud.setStatus('err');
|
||||
Territory_card.reconnectAttempts++;
|
||||
if (Territory_card.reconnectAttempts <= 5) {
|
||||
Territory_card.reconnectTimeout = setTimeout(() => {
|
||||
Territory_card.getEntrances({ update: true });
|
||||
Territory_card.cloud.start();
|
||||
}, 1000);
|
||||
} else {
|
||||
if (confirm("З'єднання розірвано! Перепідключитись?")) {
|
||||
Territory_card.reconnectAttempts = 0;
|
||||
Territory_card.getEntrances({ update: true });
|
||||
Territory_card.cloud.start();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ws.onerror = (err) => {
|
||||
console.error("[WebSocket] Помилка", err);
|
||||
Territory_card.cloud.setStatus('err');
|
||||
};
|
||||
},
|
||||
|
||||
setStatus(mode) {
|
||||
const ids = ['cloud_1', 'cloud_2', 'cloud_3'];
|
||||
ids.forEach((id, idx) => {
|
||||
const el = document.getElementById(id);
|
||||
el.setAttribute('data-state', ['sync', 'ok', 'err'].indexOf(mode) === idx ? 'active' : '');
|
||||
});
|
||||
},
|
||||
|
||||
update(msg) {
|
||||
if (msg.type !== "apartment" && msg.type !== "building") return;
|
||||
|
||||
@@ -189,25 +123,20 @@ const Territory_card = {
|
||||
|
||||
const message = {
|
||||
event: 'message',
|
||||
id: getTimeInSeconds(),
|
||||
date: getTimeInSeconds(),
|
||||
user: {
|
||||
name: USER.name,
|
||||
id: USER.id
|
||||
},
|
||||
type: "apartment",
|
||||
data: {
|
||||
...apt,
|
||||
sheep_id: USER.id
|
||||
}
|
||||
data: apt
|
||||
};
|
||||
|
||||
if (Territory_card.socket?.readyState === WebSocket.OPEN) {
|
||||
Territory_card.socket.send(JSON.stringify(message));
|
||||
if (Cloud.socket?.readyState === WebSocket.OPEN) {
|
||||
Cloud.socket.send(JSON.stringify(message));
|
||||
} else {
|
||||
if (confirm("З'єднання розірвано! Перепідключитись?")) {
|
||||
Territory_card.getEntrances({ update: true });
|
||||
Territory_card.cloud.start();
|
||||
Cloud.start();
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -241,21 +170,16 @@ const Territory_card = {
|
||||
|
||||
const message = {
|
||||
event: 'message',
|
||||
id: getTimeInSeconds(),
|
||||
date: getTimeInSeconds(),
|
||||
user: {
|
||||
name: USER.name,
|
||||
id: USER.id
|
||||
},
|
||||
type: "building",
|
||||
data: {
|
||||
...apt,
|
||||
sheep_id: USER.id
|
||||
}
|
||||
data: apt
|
||||
};
|
||||
|
||||
if (Territory_card.socket?.readyState === WebSocket.OPEN) {
|
||||
Territory_card.socket.send(JSON.stringify(message));
|
||||
if (Cloud.socket?.readyState === WebSocket.OPEN) {
|
||||
Cloud.socket.send(JSON.stringify(message));
|
||||
} else {
|
||||
if (confirm("З'єднання розірвано! Перепідключитись?")) {
|
||||
Territory_card.getEntrances({ update: true });
|
||||
@@ -434,9 +358,9 @@ const Territory_card = {
|
||||
let lng = data.geo?.lng ?? data.points?.[0]?.[0]?.[0]?.lng ?? 25.6145625;
|
||||
let zoom = 15;
|
||||
|
||||
if (map_card && map_Territory_card.remove) {
|
||||
map_Territory_card.stopLocate();
|
||||
map_Territory_card.remove();
|
||||
if (map_card && map_card.remove) {
|
||||
map_card.stopLocate();
|
||||
map_card.remove();
|
||||
}
|
||||
|
||||
const mapElement = document.getElementById('map_card');
|
||||
@@ -471,13 +395,13 @@ const Territory_card = {
|
||||
});
|
||||
|
||||
// слежение в реальном времени
|
||||
map_Territory_card.locate({ setView: false, watch: true, enableHighAccuracy: true });
|
||||
map_Territory_card.on('locationfound', (e) => {
|
||||
if (!map_Territory_card._userMarker) {
|
||||
map_Territory_card._userMarker = L.marker(e.latlng).addTo(map_card)
|
||||
map_card.locate({ setView: false, watch: true, enableHighAccuracy: true });
|
||||
map_card.on('locationfound', (e) => {
|
||||
if (!map_card._userMarker) {
|
||||
map_card._userMarker = L.marker(e.latlng).addTo(map_card)
|
||||
.bindPopup("Ви тут!");
|
||||
} else {
|
||||
map_Territory_card._userMarker.setLatLng(e.latlng);
|
||||
map_card._userMarker.setLatLng(e.latlng);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -489,7 +413,7 @@ const Territory_card = {
|
||||
|
||||
let layerControl = L.control.layers(baseMaps, [], { position: 'bottomright' }).addTo(map_card);
|
||||
|
||||
map_Territory_card.pm.setLang("ua");
|
||||
map_card.pm.setLang("ua");
|
||||
|
||||
const polygonOptions = {
|
||||
color: "#f2bd53",
|
||||
@@ -501,9 +425,9 @@ const Territory_card = {
|
||||
|
||||
L.polygon(data.points, polygonOptions).addTo(map_card);
|
||||
|
||||
map_Territory_card.setZoom(data.zoom);
|
||||
map_card.setZoom(data.zoom);
|
||||
|
||||
// map_Territory_card.getZoom()
|
||||
// map_card.getZoom()
|
||||
|
||||
// console.log(data.zoom);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user