Skip to content

Commit

Permalink
Fix #9
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenx committed May 14, 2022
1 parent 05eb1ec commit 9c5ecfb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 13 additions & 1 deletion lib/handlers/discord_websocket/discord_websocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,23 @@ class DiscordWebSoket {

// Handle a message
webSocketMessagesHandler(webSocketMessage);
}, onDone: () {
}, onDone: () async {
logger.error('WebSocket connection was closed.', name: 'DWS: Main');
logger.error(
'WebSocket close code: ${webSocketChannel?.closeCode}',
name: 'DWS: Main',
);

// If disconnection is not the user's will
if (webSocketChannel?.closeCode != null &&
webSocketChannel?.closeCode == 1005) {
if (listeners['onDisconnect'] != null) {
heartbeatTimer?.cancel();
isReconnecting = true;
await Future.delayed(const Duration(seconds: 1));
init();
}
}
// Stop WebSocketManager death process if WebSocket trying reconnect
if (isReconnecting) {
// Stop updates from Manager (if started)
Expand Down Expand Up @@ -98,6 +109,7 @@ class DiscordWebSoket {
if (listeners['onClose'] != null) {
listeners['onClose']!();
}
heartbeatTimer?.cancel();
}, onError: (error) async {
logger.error('WebSocket error $error.', name: 'DWS: Main');

Expand Down
8 changes: 5 additions & 3 deletions lib/handlers/discord_websocket/websocket_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ class DiscordWebSocketManager {
final DiscordWebSoket ws = DiscordWebSoket();

/// Init websockets and setup listeners
/// `test` flag should be used on Discord token or Spotify credentials checking
void init({bool? test}) {
String isTestInstance = test != null && test ? 'Test Instance' : '';
String isTestInstance = test != null && test ? ' Test Instance' : '';

logger.debug('Triggered init', name: 'DWS $isTestInstance: Manager');
logger.debug('Triggered init', name: 'DWS$isTestInstance: Manager');
if (initialized) {
return;
}
logger.info('Start init', name: 'DWS $isTestInstance: Manager');
logger.info('Start init', name: 'DWS$isTestInstance: Manager');

if (test == null || !test) {
ws.addListener(name: 'onDisconnect', listener: () {});
ws.addListener(
name: 'onReconnect_Manager',
listener: () {
Expand Down

0 comments on commit 9c5ecfb

Please sign in to comment.