Skip to content

Commit

Permalink
Seamless reconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
RblSb committed Aug 15, 2024
1 parent 724a224 commit a8ffd53
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 76 deletions.
31 changes: 26 additions & 5 deletions lib/models/app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/status.dart' as status;
import 'package:youtube_explode_dart/youtube_explode_dart.dart' as youtube;
Expand Down Expand Up @@ -35,6 +36,7 @@ class AppModel extends ChangeNotifier {

Timer? _getTimeTimer;
Timer? _reconnectionTimer;
Timer? _disconnectNotificationTimer;

int synchThreshold = 2;
int _prefferedOrientation = 0;
Expand Down Expand Up @@ -89,11 +91,18 @@ class AppModel extends ChangeNotifier {
if (Settings.isTV) isChatVisible = false;
}

void connect() {
_channel = IOWebSocketChannel.connect(wsUrl);
void connect() async {
final prefs = await SharedPreferences.getInstance();
final uuid = prefs.getString('uuid');
var url = wsUrl;
if (uuid != null) url += '?uuid=$uuid';
_channel = IOWebSocketChannel.connect(url);
_wsSubscription = _channel.stream.listen(onMessage, onDone: () {
chatPanel.isConnected = false;
player.pause();
_disconnectNotificationTimer ??= Timer(const Duration(seconds: 5), () {
if (chatPanel.isConnected) return;
player.pause();
});
reconnect();
}, onError: (error) {
print(error);
Expand All @@ -103,7 +112,7 @@ class AppModel extends ChangeNotifier {
void reconnect() {
if (chatPanel.isConnected) return;
_wsSubscription.cancel();
_reconnectionTimer = Timer(const Duration(seconds: 3), () {
_reconnectionTimer = Timer(const Duration(seconds: 1), () {
print('Try to reconnect...');
connect();
});
Expand Down Expand Up @@ -136,14 +145,18 @@ class AppModel extends ChangeNotifier {
switch (data.type) {
case 'Connected':
chatPanel.isConnected = true;
_disconnectNotificationTimer?.cancel();
_disconnectNotificationTimer = null;
final type = data.connected!;
saveUUID(type.uuid);
config = type.config;
_getTimeTimer?.cancel();
_getTimeTimer =
Timer.periodic(Duration(seconds: synchThreshold), (Timer timer) {
if (playlist.isEmpty()) return;
send(WsData(type: 'GetTime'));
});
final prevActiveUrl = playlist.getItem(playlist.pos)?.url ?? '';
playlist.setPos(type.itemPos);
playlist.update(type.videoList);
clients = type.clients;
Expand All @@ -154,7 +167,10 @@ class AppModel extends ChangeNotifier {
type.history.map((e) => ChatItem(e.name, e.text, e.time)).toList(),
);
playlist.setPlaylistLock(type.isPlaylistOpen);
player.loadVideo(playlist.pos);
final activeUrl = playlist.getItem(playlist.pos)?.url ?? '';
if (prevActiveUrl != activeUrl) {
player.loadVideo(playlist.pos);
}
chat.setEmotes(type.config.emotes, getChannelLink());
chatPanel.notifyListeners();
if (chat.isUnknownClient) tryAutologin();
Expand Down Expand Up @@ -529,4 +545,9 @@ class AppModel extends ChangeNotifier {
_channel.sink.close(status.goingAway);
super.dispose();
}

Future<void> saveUUID(String uuid) async {
final prefs = await SharedPreferences.getInstance();
prefs.setString('uuid', uuid);
}
}
6 changes: 5 additions & 1 deletion lib/wsdata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class WsData {
}

class Connected {
late String uuid;
late Config config;
late List<History> history;
late List<Client> clients;
Expand All @@ -201,7 +202,8 @@ class Connected {
late String globalIp;

Connected(
{required this.config,
{required this.uuid,
required this.config,
required this.history,
required this.clients,
required this.isUnknownClient,
Expand All @@ -212,6 +214,7 @@ class Connected {
required this.globalIp});

Connected.fromJson(Map<String, dynamic> json) {
uuid = json['uuid'];
config = new Config.fromJson(json['config']);
if (json['history'] != null) {
history = [];
Expand Down Expand Up @@ -240,6 +243,7 @@ class Connected {

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['uuid'] = this.uuid;
data['config'] = this.config.toJson();
data['history'] = this.history.map((v) => v.toJson()).toList();
data['clients'] = this.clients.map((v) => v.toJson()).toList();
Expand Down
Loading

0 comments on commit a8ffd53

Please sign in to comment.