@@ -16,6 +16,7 @@ let currentCAT=null;
1616var WServer ;
1717let wsServer ;
1818let wsClients = new Set ( ) ;
19+ let isShuttingDown = false ;
1920
2021const 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>' ;
2122
@@ -112,7 +113,8 @@ ipcMain.on("setCAT", async (event,arg) => {
112113} ) ;
113114
114115ipcMain . on ( "quit" , async ( event , arg ) => {
115- app . isQuitting = true ;
116+ console . log ( 'Quit requested from renderer' ) ;
117+ shutdownApplication ( ) ;
116118 app . quit ( ) ;
117119 event . returnValue = true ;
118120} ) ;
@@ -123,6 +125,41 @@ ipcMain.on("radio_status_update", async (event,arg) => {
123125 event . returnValue = true ;
124126} ) ;
125127
128+ function shutdownApplication ( ) {
129+ if ( isShuttingDown ) {
130+ console . log ( 'Shutdown already in progress, ignoring duplicate request' ) ;
131+ return ;
132+ }
133+
134+ isShuttingDown = true ;
135+ console . log ( 'Initiating application shutdown...' ) ;
136+
137+ try {
138+ // Close all servers
139+ if ( WServer ) {
140+ console . log ( 'Closing UDP server...' ) ;
141+ WServer . close ( ) ;
142+ }
143+ if ( httpServer ) {
144+ console . log ( 'Closing HTTP server...' ) ;
145+ httpServer . close ( ) ;
146+ }
147+ if ( wsServer ) {
148+ console . log ( 'Closing WebSocket server and clients...' ) ;
149+ // Close all WebSocket client connections
150+ wsClients . forEach ( client => {
151+ if ( client . readyState === WebSocket . OPEN ) {
152+ client . close ( ) ;
153+ }
154+ } ) ;
155+ wsClients . clear ( ) ;
156+ wsServer . close ( ) ;
157+ }
158+ } catch ( error ) {
159+ console . error ( 'Error during server shutdown:' , error ) ;
160+ }
161+ }
162+
126163function show_noti ( arg ) {
127164 if ( Notification . isSupported ( ) ) {
128165 try {
@@ -161,30 +198,13 @@ ipcMain.on("test", async (event,arg) => {
161198} ) ;
162199
163200app . on ( 'before-quit' , ( ) => {
164- console . log ( 'Shutting down servers...' ) ;
165- if ( WServer ) {
166- WServer . close ( ) ;
167- }
168- if ( httpServer ) {
169- httpServer . close ( ) ;
170- }
171- if ( wsServer ) {
172- // Close all WebSocket client connections
173- wsClients . forEach ( client => {
174- if ( client . readyState === WebSocket . OPEN ) {
175- client . close ( ) ;
176- }
177- } ) ;
178- wsClients . clear ( ) ;
179- // Close the WebSocket server
180- wsServer . close ( ) ;
181- }
201+ console . log ( 'before-quit event triggered' ) ;
202+ shutdownApplication ( ) ;
182203} ) ;
183204
184205process . on ( 'SIGINT' , ( ) => {
185- console . log ( 'SIGINT received, closing servers...' ) ;
186- if ( WServer ) WServer . close ( ) ;
187- if ( httpServer ) httpServer . close ( ) ;
206+ console . log ( 'SIGINT received, initiating shutdown...' ) ;
207+ shutdownApplication ( ) ;
188208 process . exit ( 0 ) ;
189209} ) ;
190210
@@ -216,8 +236,12 @@ if (!gotTheLock) {
216236}
217237
218238app . on ( 'window-all-closed' , function ( ) {
239+ console . log ( 'All windows closed, initiating shutdown...' ) ;
240+ if ( ! isShuttingDown ) {
241+ shutdownApplication ( ) ;
242+ }
219243 if ( process . platform !== 'darwin' ) app . quit ( ) ;
220- app . quit ( ) ;
244+ else app . quit ( ) ;
221245} )
222246
223247function normalizeTxPwr ( adifdata ) {
0 commit comments