diff --git a/app/active_calls/active_calls.php b/app/active_calls/active_calls.php index 9e80533678..ff0532cc51 100644 --- a/app/active_calls/active_calls.php +++ b/app/active_calls/active_calls.php @@ -339,21 +339,35 @@ echo "\n"; client.ws.addEventListener("open", async () => { try { console.log('Connected'); - console.log('Requesting authentication'); - await client.request('authentication'); reconnectAttempts = 0; const status = document.getElementById('calls_active_count'); status.style.backgroundColor = colors.INACTIVE; - bindEventHandlers(client); - console.log('Sent request for calls in progress'); - client.request('active.calls', 'in.progress'); - status.style.backgroundColor = colors.CONNECTED; } catch (err) { console.error("WS setup failed: ", err); return; } }); + // Handle incoming messages for authentication + client.ws.addEventListener("message", async (event) => { + try { + const message = JSON.parse(event.data); + // Check for authentication request from server + if (message.status_code === 407 && message.service_name === 'authentication') { + console.log('Authentication required - sending credentials'); + await client.request('authentication'); + console.log('Authentication sent'); + const status = document.getElementById('calls_active_count'); + bindEventHandlers(client); + console.log('Sent request for calls in progress'); + client.request('active.calls', 'in.progress'); + status.style.backgroundColor = colors.CONNECTED; + } + } catch (err) { + // Let the ws_client handle other messages + } + }); + // DISCONNECTED client.ws.addEventListener("close", async () => { const status = document.getElementById('calls_active_count'); @@ -552,7 +566,7 @@ echo "\n"; replace_arrow_icon(uuid, 'local'); row.dataset.forced_direction = 'local'; } - console.log('application', uuid, application_data); + //console.log('application', uuid, application_data); update_call_element(`application_${uuid}`, application_data); } @@ -782,7 +796,7 @@ echo "\n"; // add the row to the table tbody.appendChild(row); - console.log('NEW ROW ADDED', row.id); + //console.log('NEW ROW ADDED', row.id); // Hide/show domain column const domain = document.getElementById('th_domain'); @@ -988,7 +1002,12 @@ echo "\n"; //calculate already elapsed time const start = new Date(start_time / 1000); const now = new Date(); - const elapsed = Math.floor(now.getTime() - start.getTime()); + let elapsed = Math.floor(now.getTime() - start.getTime()); + + // Fix negative timer issue - if start time is in the future, set elapsed to 0 + if (elapsed < 0) { + elapsed = 0; + } //format time const hh = Math.floor(elapsed / (1000 * 3600)).toString(); diff --git a/app/active_calls/resources/dashboard/active_calls.php b/app/active_calls/resources/dashboard/active_calls.php index dad3af8da5..36145e68e4 100644 --- a/app/active_calls/resources/dashboard/active_calls.php +++ b/app/active_calls/resources/dashboard/active_calls.php @@ -267,31 +267,43 @@ if (!empty($_SESSION['user']['extension'])) { active_calls_widget_client.ws.addEventListener("open", async () => { try { console.log('Connected'); - console.log('Requesting authentication'); + reconnectAttempts = 0; //set the status as inactive while waiting const status = document.getElementById('calls_active_count'); status.style.backgroundColor = colors.INACTIVE; - - //wait to be authenticated - await active_calls_widget_client.request('authentication'); - reconnectAttempts = 0; - - //bind active call event to function - active_calls_widget_client.onEvent("CHANNEL_CALLSTATE", channel_callstate_event); - console.log('Sent request for calls in progress'); - - //get the in progress calls - active_calls_widget_client.request('active.calls', 'in.progress'); - - //display green circle for connected - status.style.backgroundColor = colors.CONNECTED; } catch (err) { console.error("WS setup failed: ", err); return; } }); + // Handle incoming messages for authentication + active_calls_widget_client.ws.addEventListener("message", async (event) => { + try { + const message = JSON.parse(event.data); + // Check for authentication request from server + if (message.status_code === 407 && message.service_name === 'authentication') { + console.log('Authentication required - sending credentials'); + await active_calls_widget_client.request('authentication'); + console.log('Authentication sent'); + + //bind active call event to function + active_calls_widget_client.onEvent("CHANNEL_CALLSTATE", channel_callstate_event); + console.log('Sent request for calls in progress'); + + //get the in progress calls + active_calls_widget_client.request('active.calls', 'in.progress'); + + //display green circle for connected + const status = document.getElementById('calls_active_count'); + status.style.backgroundColor = colors.CONNECTED; + } + } catch (err) { + // Let the ws_client handle other messages + } + }); + // DISCONNECTED active_calls_widget_client.ws.addEventListener("close", async () => { const status = document.getElementById('calls_active_count'); @@ -320,8 +332,6 @@ if (!empty($_SESSION['user']['extension'])) { const other_leg_unique_id = call.other_leg_unique_id ?? ''; switch (state) { case 'ringing': - //calls that are already in progress should be answered status - if (call.caller_channel_created_time > Date.now()) call.answer_state = 'answered'; //update the data update_call(call); replace_arrow_color(uuid, colors.RINGING); @@ -461,15 +471,14 @@ echo '