bug fix für Nachrichtenversand vom mobilen Client
This commit is contained in:
1 parent
c793e8b16d
commit
c477a05f03
8 files changed
+133
-32
No files matched your search
@@ -127,10 +127,8 @@
|
||||
ws.addEventListener('open', () => {
|
||||
ws.send(JSON.stringify({
|
||||
code: 0,
|
||||
msg: {
|
||||
token: data.token,
|
||||
session_id: window.CMBNET_CLIENT_SESSION_ID || ''
|
||||
}
|
||||
msg: data.token,
|
||||
session_id: window.CMBNET_CLIENT_SESSION_ID || ''
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* damit er sowohl die mobilen Views als auch die statischen Assets
|
||||
* kontrolliert. Der Cache-Name wird pro App-Wurzel gebildet.
|
||||
*/
|
||||
const VERSION = 'cmbnet-pwa-v7';
|
||||
const VERSION = 'cmbnet-pwa-v8';
|
||||
const BASE = new URL('./', self.location).href;
|
||||
const SHELL_CACHE = `${VERSION}-shell`;
|
||||
const TILE_CACHE = `${VERSION}-tiles`;
|
||||
|
||||
@@ -2703,10 +2703,8 @@ async function connectWebSocket() {
|
||||
ws.addEventListener('open', () => {
|
||||
ws.send(JSON.stringify({
|
||||
code: 0,
|
||||
msg: {
|
||||
token,
|
||||
session_id: window.CMBNET_CLIENT_SESSION_ID || ''
|
||||
}
|
||||
msg: token,
|
||||
session_id: window.CMBNET_CLIENT_SESSION_ID || ''
|
||||
}));
|
||||
dispatcherState.authenticated = true;
|
||||
setConnectionStatus('Verbunden', 'connected');
|
||||
|
||||
@@ -21,6 +21,7 @@ const DEFAULT_SETTINGS = {
|
||||
const AUTO_POSITION_INTERVAL_MS = 30000;
|
||||
const NAVIGATION_MAX_AGE_MS = 30 * 60 * 1000;
|
||||
const NAVIGATION_REFRESH_MS = 15000;
|
||||
const WS_OUTBOX_LIMIT = 200;
|
||||
|
||||
const STREET_STATES = ['unbearbeitet', 'zugewiesen', 'erledigt'];
|
||||
const STREET_COLORS = {
|
||||
@@ -45,6 +46,7 @@ const state = {
|
||||
currentStreetId: null,
|
||||
ws: null,
|
||||
authenticated: false,
|
||||
outbox: [],
|
||||
reconnectTimer: null,
|
||||
chatMessages: [],
|
||||
chatKeys: new Set(),
|
||||
@@ -309,13 +311,54 @@ function updatePositionSharingUi() {
|
||||
button.textContent = 'Position senden';
|
||||
}
|
||||
|
||||
function canSendWs() {
|
||||
return Boolean(state.ws
|
||||
&& state.ws.readyState === WebSocket.OPEN
|
||||
&& state.authenticated);
|
||||
}
|
||||
|
||||
function sendWsMessage(message, queueWhenOffline = true) {
|
||||
const data = JSON.stringify(message);
|
||||
|
||||
if (canSendWs()) {
|
||||
try {
|
||||
state.ws.send(data);
|
||||
return true;
|
||||
} catch (error) {
|
||||
// Nach einem Sendefehler wird die Nachricht unten gepuffert.
|
||||
}
|
||||
}
|
||||
|
||||
if (queueWhenOffline) {
|
||||
state.outbox.push(data);
|
||||
if (state.outbox.length > WS_OUTBOX_LIMIT) {
|
||||
state.outbox.splice(0, state.outbox.length - WS_OUTBOX_LIMIT);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function flushWsOutbox() {
|
||||
if (!canSendWs() || !state.outbox.length) return;
|
||||
|
||||
const pending = state.outbox;
|
||||
state.outbox = [];
|
||||
for (let index = 0; index < pending.length; index += 1) {
|
||||
try {
|
||||
state.ws.send(pending[index]);
|
||||
} catch (error) {
|
||||
state.outbox = pending.slice(index).concat(state.outbox);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sendOwnPosition(lat, lon, automatic = false) {
|
||||
if (viewMode !== 'mobile_group' || currentRole !== 'Gruppe') return false;
|
||||
if (!state.currentGroupId) return false;
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return false;
|
||||
if (!state.ws || state.ws.readyState !== WebSocket.OPEN || !state.authenticated) return false;
|
||||
|
||||
state.ws.send(JSON.stringify({
|
||||
const sent = sendWsMessage({
|
||||
code: 300,
|
||||
msg: JSON.stringify({
|
||||
group_id: state.currentGroupId,
|
||||
@@ -325,7 +368,9 @@ function sendOwnPosition(lat, lon, automatic = false) {
|
||||
actor: currentAccountId,
|
||||
automatic: automatic === true
|
||||
})
|
||||
}));
|
||||
}, !automatic);
|
||||
|
||||
if (!sent && automatic) return false;
|
||||
state.lastPositionSentAt = Date.now();
|
||||
return true;
|
||||
}
|
||||
@@ -869,12 +914,10 @@ function saveMobileStreetState() {
|
||||
} else if (newState === 'erledigt' && street.group_id) {
|
||||
groupId = Number(street.group_id);
|
||||
}
|
||||
if (state.ws && state.ws.readyState === WebSocket.OPEN && state.authenticated) {
|
||||
state.ws.send(JSON.stringify({
|
||||
code: 100,
|
||||
msg: JSON.stringify({name: street.name, state: newState, actor: currentAccountId, street_id: street.id, group_id: groupId})
|
||||
}));
|
||||
}
|
||||
sendWsMessage({
|
||||
code: 100,
|
||||
msg: JSON.stringify({name: street.name, state: newState, actor: currentAccountId, street_id: street.id, group_id: groupId})
|
||||
});
|
||||
closeMobileStreetState();
|
||||
}
|
||||
|
||||
@@ -1105,14 +1148,26 @@ function connectWebSocket() {
|
||||
const ws = new WebSocket(wsUrl);
|
||||
state.ws = ws;
|
||||
ws.addEventListener('open', () => {
|
||||
// Der Server bestätigt den Handshake mit Code 0. Bis dahin werden
|
||||
// fachliche Nachrichten im Outbox-Puffer gesammelt.
|
||||
state.authenticated = false;
|
||||
ws.send(JSON.stringify({
|
||||
code: 0,
|
||||
msg: {
|
||||
token,
|
||||
session_id: window.CMBNET_CLIENT_SESSION_ID || ''
|
||||
}
|
||||
msg: token,
|
||||
session_id: window.CMBNET_CLIENT_SESSION_ID || ''
|
||||
}));
|
||||
state.authenticated = true;
|
||||
|
||||
// Fallback für ältere Server, die noch keine Code-0-Bestätigung
|
||||
// senden. Die Code-0-Nachricht wurde bereits als erstes gesendet,
|
||||
// daher ist die Reihenfolge auf dem Socket weiterhin korrekt.
|
||||
setTimeout(() => {
|
||||
if (state.ws === ws
|
||||
&& ws.readyState === WebSocket.OPEN
|
||||
&& !state.authenticated) {
|
||||
state.authenticated = true;
|
||||
flushWsOutbox();
|
||||
}
|
||||
}, 1500);
|
||||
});
|
||||
ws.addEventListener('message', handleWs);
|
||||
ws.addEventListener('close', () => {
|
||||
@@ -1129,13 +1184,12 @@ function sendChat() {
|
||||
if (!input) return;
|
||||
const text = input.value.trim();
|
||||
if (!text) return;
|
||||
if (!state.ws || state.ws.readyState !== WebSocket.OPEN || !state.authenticated) return;
|
||||
|
||||
|
||||
if (viewMode === 'mobile_group' && state.currentGroupId) {
|
||||
state.ws.send(JSON.stringify({code: 2000, msg: JSON.stringify({group_id: state.currentGroupId, text})}));
|
||||
sendWsMessage({code: 2000, msg: JSON.stringify({group_id: state.currentGroupId, text})});
|
||||
} else if (viewMode === 'mobile_dispatcher') {
|
||||
// For dispatcher mobile, could send 1000 broadcast or 1100 - follow general behavior
|
||||
state.ws.send(JSON.stringify({code: 1000, msg: JSON.stringify({text})}));
|
||||
sendWsMessage({code: 1000, msg: JSON.stringify({text})});
|
||||
}
|
||||
input.value = '';
|
||||
}
|
||||
@@ -1275,6 +1329,7 @@ function handleWs(event) {
|
||||
|
||||
if (message.code === 0) {
|
||||
state.authenticated = true;
|
||||
flushWsOutbox();
|
||||
} else if (message.code === 1) {
|
||||
if (state.ws && state.ws.readyState === WebSocket.OPEN) {
|
||||
state.ws.send(JSON.stringify({code: 2, msg: 'pong'}));
|
||||
|
||||
Reference in new issue
Block a user