@@ -4,14 +4,18 @@ const {ipcMain} = require('electron')
44const http = require ( 'http' ) ;
55const xml = require ( "xml2js" ) ;
66const net = require ( 'net' ) ;
7+ const WebSocket = require ( 'ws' ) ;
78
89const gotTheLock = app . requestSingleInstanceLock ( ) ;
910
1011let powerSaveBlockerId ;
1112let s_mainWindow ;
1213let msgbacklog = [ ] ;
1314let httpServer ;
15+ let currentCAT = null ;
1416var WServer ;
17+ let wsServer ;
18+ let wsClients = new Set ( ) ;
1519
1620const DemoAdif = '<call:5>DJ7NT <gridsquare:4>JO30 <mode:3>FT8 <rst_sent:3>-15 <rst_rcvd:2>33 <qso_date:8>20240110 <time_on:6>051855 <qso_date_off:8>20240110 <time_off:6>051855 <band:3>40m <freq:8>7.155783 <station_callsign:5>TE1ST <my_gridsquare:6>JO30OO <eor>' ;
1721
@@ -143,6 +147,12 @@ ipcMain.on("quit", async (event,arg) => {
143147 event . returnValue = true ;
144148} ) ;
145149
150+ ipcMain . on ( "radio_status_update" , async ( event , arg ) => {
151+ // Broadcast radio status updates from renderer to WebSocket clients
152+ broadcastRadioStatus ( arg ) ;
153+ event . returnValue = true ;
154+ } ) ;
155+
146156function show_noti ( arg ) {
147157 if ( Notification . isSupported ( ) ) {
148158 try {
@@ -477,11 +487,73 @@ function startserver() {
477487 settrx ( qrg , mode ) ;
478488 }
479489 } ) . listen ( 54321 ) ;
490+
491+ // Start WebSocket server
492+ startWebSocketServer ( ) ;
480493 } catch ( e ) {
481494 tomsg ( 'Some other Tool blocks Port 2333 or 54321. Stop it, and restart this' ) ;
482495 }
483496}
484497
498+ function startWebSocketServer ( ) {
499+ try {
500+ wsServer = new WebSocket . Server ( { port : 54322 } ) ;
501+ tomsg ( 'WebSocket server started on port 54322' ) ;
502+
503+ wsServer . on ( 'connection' , ( ws ) => {
504+ wsClients . add ( ws ) ;
505+ tomsg ( 'WebSocket client connected' ) ;
506+
507+ ws . on ( 'close' , ( ) => {
508+ wsClients . delete ( ws ) ;
509+ tomsg ( 'WebSocket client disconnected' ) ;
510+ } ) ;
511+
512+ ws . on ( 'error' , ( error ) => {
513+ console . error ( 'WebSocket error:' , error ) ;
514+ wsClients . delete ( ws ) ;
515+ } ) ;
516+
517+ // Send current radio status on connection
518+ ws . send ( JSON . stringify ( {
519+ type : 'welcome' ,
520+ message : 'Connected to WaveLogGate WebSocket server'
521+ } ) ) ;
522+ broadcastRadioStatus ( currentCAT ) ;
523+ } ) ;
524+
525+ wsServer . on ( 'error' , ( error ) => {
526+ console . error ( 'WebSocket server error:' , error ) ;
527+ } ) ;
528+
529+ } catch ( e ) {
530+ tomsg ( 'Failed to start WebSocket server on port 54322: ' + e . message ) ;
531+ }
532+ }
533+
534+ function broadcastRadioStatus ( radioData ) {
535+ currentCAT = radioData ;
536+ let message = {
537+ type : 'radio_status' ,
538+ frequency : radioData . frequency ? parseInt ( radioData . frequency ) : null ,
539+ mode : radioData . mode || null ,
540+ power : radioData . power || null ,
541+ radio : radioData . radio || 'wlstream' ,
542+ timestamp : Date . now ( )
543+ } ;
544+ // Only include frequency_rx if it's not null
545+ if ( radioData . frequency_rx ) {
546+ message . frequency_rx = parseInt ( radioData . frequency_rx ) ;
547+ }
548+
549+ const messageStr = JSON . stringify ( message ) ;
550+ wsClients . forEach ( ( client ) => {
551+ if ( client . readyState === WebSocket . OPEN ) {
552+ client . send ( messageStr ) ;
553+ }
554+ } ) ;
555+ }
556+
485557
486558async function get_modes ( ) {
487559 return new Promise ( ( resolve ) => {
@@ -574,6 +646,8 @@ async function settrx(qrg, mode = '') {
574646 client . on ( "close" , ( ) => { } ) ;
575647 }
576648
649+ // Broadcast frequency/mode change to WebSocket clients
650+
577651 return true ;
578652}
579653
0 commit comments