You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.9 KiB
Dart
61 lines
1.9 KiB
Dart
import 'package:mc_common_app/classes/app_state.dart';
|
|
import 'package:mc_common_app/classes/consts.dart';
|
|
import 'package:mc_common_app/main.dart';
|
|
import 'package:mc_common_app/utils/utils.dart';
|
|
import 'package:signalr_netcore/http_connection_options.dart';
|
|
import 'package:signalr_netcore/hub_connection.dart';
|
|
import 'package:signalr_netcore/hub_connection_builder.dart';
|
|
import 'package:logging/logging.dart';
|
|
|
|
abstract class ChatRepo {
|
|
Future<HubConnection?> buildChatHubConnection();
|
|
}
|
|
|
|
class ChatRepoImp implements ChatRepo {
|
|
@override
|
|
Future<HubConnection?> buildChatHubConnection() async {
|
|
final userId = AppState().getUser.data!.userInfo!.userId ?? "";
|
|
HttpConnectionOptions httpOptions = HttpConnectionOptions(skipNegotiation: false, logMessageContent: true);
|
|
HubConnection hubConnection = HubConnectionBuilder()
|
|
.withUrl(
|
|
"${ApiConsts.chatHubUrl}?userID=$userId",
|
|
options: httpOptions,
|
|
)
|
|
.withAutomaticReconnect(
|
|
retryDelays: <int>[2000, 5000, 10000, 20000],
|
|
)
|
|
.configureLogging(
|
|
Logger("configureLogging"),
|
|
)
|
|
.build();
|
|
hubConnection.onclose(
|
|
({Exception? error}) {
|
|
logger.i("onClose");
|
|
},
|
|
);
|
|
hubConnection.onreconnecting(
|
|
({Exception? error}) {
|
|
logger.i("onReconnecting");
|
|
},
|
|
);
|
|
hubConnection.onreconnected(
|
|
({String? connectionId}) {
|
|
logger.i("onReconnected");
|
|
},
|
|
);
|
|
if (hubConnection.state != HubConnectionState.Connected) {
|
|
await hubConnection.start();
|
|
logger.i("Started HubConnection");
|
|
|
|
try {
|
|
hubConnection.on("ReceiveMessageRequestOffer", (List<Object?>? arguments) {
|
|
Utils.showToast("I received ping : ${arguments.toString()}");
|
|
});
|
|
} catch (e) {
|
|
logger.i("Error in OnSendQuestionToParticipant");
|
|
}
|
|
}
|
|
return hubConnection;
|
|
}
|
|
}
|