@@ -45,7 +45,7 @@ class wsport:
45
45
46
46
__slots__ = [ 'aw' , '_loop' , '_thread' , '_write_queue' , 'default_host' , 'host' , 'port' , 'path' , 'machineID' , 'lastReadResult' , 'channels' , 'readings' , 'tx' ,
47
47
'channel_requests' , 'channel_nodes' , 'channel_modes' , 'connect_timeout' , 'request_timeout' , 'compression' ,
48
- 'reconnect_interval' , 'ping_interval ' , 'ping_timeout ' , 'id_node' , 'machine_node' ,
48
+ 'reconnect_interval' , '_ping_interval ' , '_ping_timeout ' , 'id_node' , 'machine_node' ,
49
49
'command_node' , 'data_node' , 'pushMessage_node' , 'request_data_command' , 'charge_message' , 'drop_message' , 'addEvent_message' , 'event_node' ,
50
50
'DRY_node' , 'FCs_node' , 'FCe_node' , 'SCs_node' , 'SCe_node' , 'STARTonCHARGE' , 'OFFonDROP' , 'open_event' , 'pending_events' ,
51
51
'ws' , 'wst' ]
@@ -79,12 +79,12 @@ def __init__(self, aw:'ApplicationWindow') -> None:
79
79
self .channel_modes :List [int ] = [0 ]* self .channels # temp mode is an int here, 0:__,1:C,2:F
80
80
81
81
# configurable via the UI:
82
- self .connect_timeout :float = 4 # in seconds
83
- self .request_timeout :float = 0.5 # in seconds
84
- self .reconnect_interval :float = 2 # in seconds # not used for now
82
+ self .connect_timeout :float = 4 # in seconds (websockets default is 10)
83
+ self .request_timeout :float = 0.5 # in seconds
84
+ self .reconnect_interval :float = 0. 2 # in seconds # not used for now (reconnect delay)
85
85
# not configurable via the UI:
86
- self .ping_interval : float = 0 # in seconds; if 0 pings are not send automatically
87
- self .ping_timeout :Optional [float ] = None # in seconds
86
+ self ._ping_interval : Optional [ float ] = 20 # in seconds; None disables keepalive (default is 20)
87
+ self ._ping_timeout :Optional [float ] = 20 # in seconds; None disables timeouts (default is 20)
88
88
89
89
# JSON nodes
90
90
self .id_node :str = 'id'
@@ -256,6 +256,7 @@ async def producer_handler(self, websocket:'ClientConnection') -> None:
256
256
message = await self .producer ()
257
257
if message is not None :
258
258
await websocket .send (message )
259
+ await asyncio .sleep (0.1 ) # yield control to the event loop
259
260
260
261
261
262
# if serial settings are given, host/port are ignore and communication is handled by the given serial port
@@ -273,8 +274,11 @@ async def connect(self) -> None:
273
274
hostport = f'{ self .host } :{ self .port } '
274
275
async for websocket in websockets .connect (
275
276
f'ws://{ hostport } /{ self .path } ' ,
276
- compression = ('deflate' if self .compression else None ),
277
- origin = websockets .Origin (f'http://{ socket .gethostname ()} ' ),
277
+ open_timeout = self .connect_timeout ,
278
+ ping_interval = self ._ping_interval ,
279
+ ping_timeout = self ._ping_timeout ,
280
+ compression = ('deflate' if self .compression else None ),
281
+ origin = websockets .Origin (f'http://{ socket .gethostname ()} ' ),
278
282
user_agent_header = f'Artisan/{ __version__ } websockets' ):
279
283
done : Set [asyncio .Task [Any ]] = set ()
280
284
pending : Set [asyncio .Task [Any ]] = set ()
@@ -288,15 +292,28 @@ async def connect(self) -> None:
288
292
[consumer_task , producer_task ],
289
293
return_when = asyncio .FIRST_COMPLETED ,
290
294
)
295
+ _log .debug ('disconnected' )
296
+ for task in pending :
297
+ task .cancel ()
298
+ for task in done :
299
+ exception = task .exception ()
300
+ if isinstance (exception , Exception ):
301
+ raise exception
291
302
except websockets .ConnectionClosed :
303
+ _log .debug ('ConnectionClosed exception' )
292
304
continue
305
+ except Exception as e : # pylint: disable=broad-except
306
+ _log .exception (e )
293
307
finally :
294
308
for task in pending :
295
309
task .cancel ()
296
310
for task in done :
297
311
exception = task .exception ()
298
312
if isinstance (exception , Exception ):
299
313
raise exception
314
+ _log .debug ('reconnecting' )
315
+ self .aw .sendmessageSignal .emit (QApplication .translate ('Message' , '{} disconnected' ).format ('WebSocket' ),True ,None )
316
+ await asyncio .sleep (0.1 )
300
317
except asyncio .TimeoutError :
301
318
_log .info ('connection timeout' )
302
319
except Exception as e : # pylint: disable=broad-except
0 commit comments