@@ -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,71 @@ 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 , exclusive : true } ) ;
501+
502+ wsServer . on ( 'connection' , ( ws ) => {
503+ wsClients . add ( ws ) ;
504+ console . log ( 'WebSocket client connected' ) ;
505+
506+ ws . on ( 'close' , ( ) => {
507+ wsClients . delete ( ws ) ;
508+ } ) ;
509+
510+ ws . on ( 'error' , ( error ) => {
511+ console . error ( 'WebSocket error:' , error ) ;
512+ wsClients . delete ( ws ) ;
513+ } ) ;
514+
515+ // Send current radio status on connection
516+ ws . send ( JSON . stringify ( {
517+ type : 'welcome' ,
518+ message : 'Connected to WaveLogGate WebSocket server'
519+ } ) ) ;
520+ broadcastRadioStatus ( currentCAT ) ;
521+ } ) ;
522+
523+ wsServer . on ( 'error' , ( error ) => {
524+ console . error ( 'WebSocket server error:' , error ) ;
525+ } ) ;
526+
527+ } catch ( e ) {
528+ console . error ( 'WebSocket server startup error:' , e ) ;
529+ }
530+ }
531+
532+ function broadcastRadioStatus ( radioData ) {
533+ currentCAT = radioData ;
534+ let message = {
535+ type : 'radio_status' ,
536+ frequency : radioData . frequency ? parseInt ( radioData . frequency ) : null ,
537+ mode : radioData . mode || null ,
538+ power : radioData . power || null ,
539+ radio : radioData . radio || 'wlstream' ,
540+ timestamp : Date . now ( )
541+ } ;
542+ // Only include frequency_rx if it's not null
543+ if ( radioData . frequency_rx ) {
544+ message . frequency_rx = parseInt ( radioData . frequency_rx ) ;
545+ }
546+
547+ const messageStr = JSON . stringify ( message ) ;
548+ wsClients . forEach ( ( client ) => {
549+ if ( client . readyState === WebSocket . OPEN ) {
550+ client . send ( messageStr ) ;
551+ }
552+ } ) ;
553+ }
554+
485555
486556async function get_modes ( ) {
487557 return new Promise ( ( resolve ) => {
@@ -574,6 +644,8 @@ async function settrx(qrg, mode = '') {
574644 client . on ( "close" , ( ) => { } ) ;
575645 }
576646
647+ // Broadcast frequency/mode change to WebSocket clients
648+
577649 return true ;
578650}
579651
0 commit comments