Hub Connection Fix

merge-requests/86/head
Aamir Muhammad 3 years ago
parent 2d6ffff8be
commit e94a8d9a5e

@ -51,10 +51,8 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
} }
Future<void> buildHubConnection() async { Future<void> buildHubConnection() async {
hubConnection = await getHubConnection(); chatHubConnection = await getHubConnection();
await hubConnection.start()!.then((value) { await chatHubConnection.start();
print('-----------------------Hub Started For Chat------------------');
});
} }
Future<HubConnection> getHubConnection() async { Future<HubConnection> getHubConnection() async {
@ -75,18 +73,18 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
} }
void registerEvents() { void registerEvents() {
hubConnection.on("OnUpdateUserStatusAsync", changeStatus); chatHubConnection.on("OnUpdateUserStatusAsync", changeStatus);
hubConnection.on("OnDeliveredChatUserAsync", onMsgReceived); chatHubConnection.on("OnDeliveredChatUserAsync", onMsgReceived);
// hubConnection.on("OnSeenChatUserAsync", onChatSeen); // hubConnection.on("OnSeenChatUserAsync", onChatSeen);
//hubConnection.on("OnUserTypingAsync", onUserTyping); //hubConnection.on("OnUserTypingAsync", onUserTyping);
hubConnection.on("OnUserCountAsync", userCountAsync); chatHubConnection.on("OnUserCountAsync", userCountAsync);
// hubConnection.on("OnUpdateUserChatHistoryWindowsAsync", updateChatHistoryWindow); // hubConnection.on("OnUpdateUserChatHistoryWindowsAsync", updateChatHistoryWindow);
hubConnection.on("OnGetUserChatHistoryNotDeliveredAsync", chatNotDelivered); chatHubConnection.on("OnGetUserChatHistoryNotDeliveredAsync", chatNotDelivered);
hubConnection.on("OnUpdateUserChatHistoryStatusAsync", updateUserChatStatus); chatHubConnection.on("OnUpdateUserChatHistoryStatusAsync", updateUserChatStatus);
} }
void getUserRecentChats() async { void getUserRecentChats() async {
if (hubConnection.state != HubConnectionState.Connected) { if (chatHubConnection.state != HubConnectionState.Connected) {
getUserAutoLoginToken().whenComplete(() async { getUserAutoLoginToken().whenComplete(() async {
await buildHubConnection(); await buildHubConnection();
getUserRecentChats(); getUserRecentChats();
@ -127,7 +125,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
} }
Future invokeUserChatHistoryNotDeliveredAsync({required int userId}) async { Future invokeUserChatHistoryNotDeliveredAsync({required int userId}) async {
await hubConnection.invoke("GetUserChatHistoryNotDeliveredAsync", args: [userId]); await chatHubConnection.invoke("GetUserChatHistoryNotDeliveredAsync", args: [userId]);
return ""; return "";
} }
@ -194,7 +192,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
void updateUserChatHistoryStatusAsync(List data) { void updateUserChatHistoryStatusAsync(List data) {
try { try {
hubConnection.invoke("UpdateUserChatHistoryStatusAsync", args: [data]); chatHubConnection.invoke("UpdateUserChatHistoryStatusAsync", args: [data]);
} catch (e) { } catch (e) {
throw e; throw e;
} }
@ -202,7 +200,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
void updateUserChatHistoryOnMsg(List data) { void updateUserChatHistoryOnMsg(List data) {
try { try {
hubConnection.invoke("UpdateUserChatHistoryStatusAsync", args: [data]); chatHubConnection.invoke("UpdateUserChatHistoryStatusAsync", args: [data]);
} catch (e) { } catch (e) {
throw e; throw e;
} }
@ -486,7 +484,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
String chatData = String chatData =
'{"contant":"$msg","contantNo":"$contentNo","chatEventId":$chatEventId,"fileTypeId": $fileTypeId,"currentUserId":${AppState().chatDetails!.response!.id},"chatSource":1,"userChatHistoryLineRequestList":[{"isSeen":false,"isDelivered":false,"targetUserId":$targetUserId,"targetUserStatus":1}],"chatReplyId":$chatReplyId,"conversationId":"$chatCID"}'; '{"contant":"$msg","contantNo":"$contentNo","chatEventId":$chatEventId,"fileTypeId": $fileTypeId,"currentUserId":${AppState().chatDetails!.response!.id},"chatSource":1,"userChatHistoryLineRequestList":[{"isSeen":false,"isDelivered":false,"targetUserId":$targetUserId,"targetUserStatus":1}],"chatReplyId":$chatReplyId,"conversationId":"$chatCID"}';
await hubConnection.invoke("AddChatUserAsync", args: <Object>[json.decode(chatData)]); await chatHubConnection.invoke("AddChatUserAsync", args: <Object>[json.decode(chatData)]);
} }
void sendChatMessage(int targetUserId, String targetUserName, BuildContext context) async { void sendChatMessage(int targetUserId, String targetUserName, BuildContext context) async {

@ -30,7 +30,7 @@ class _ChatHomeState extends State<ChatHome> {
super.initState(); super.initState();
data = Provider.of<ChatProviderModel>(context, listen: false); data = Provider.of<ChatProviderModel>(context, listen: false);
data.registerEvents(); data.registerEvents();
if (hubConnection.state != HubConnectionState.Connected) { if (chatHubConnection.state != HubConnectionState.Connected) {
data.getUserAutoLoginToken().whenComplete(() async { data.getUserAutoLoginToken().whenComplete(() async {
await data.buildHubConnection(); await data.buildHubConnection();
data.getUserRecentChats(); data.getUserRecentChats();

@ -31,7 +31,7 @@ import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:signalr_netcore/signalr_client.dart'; import 'package:signalr_netcore/signalr_client.dart';
late HubConnection hubConnection; late HubConnection chatHubConnection;
class DashboardScreen extends StatefulWidget { class DashboardScreen extends StatefulWidget {
DashboardScreen({Key? key}) : super(key: key); DashboardScreen({Key? key}) : super(key: key);
@ -67,7 +67,7 @@ class _DashboardScreenState extends State<DashboardScreen> {
@override @override
void dispose() { void dispose() {
super.dispose(); super.dispose();
hubConnection.stop(); chatHubConnection.stop();
} }
void _bHubCon() { void _bHubCon() {

Loading…
Cancel
Save