Skip to content

Commit

Permalink
Update WsConnection.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
mortzaCFT authored Nov 9, 2024
1 parent e6005d6 commit c7e32d8
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/controllers/WsConnection.dart
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
import 'dart:convert';
import 'package:get/get.dart';
import 'package:msf/unit/api/WebSocketService.dart';
import 'package:msf/unit/com.dart';

import '../unit/api/HttpService.dart';
import 'ResourceUsageController.dart';

class WsConnection extends GetxController {
final Com _comService = Com();
final Com api = Com(
httpService: HttpService(),
webSocketService: WebSocketService(),
);

var isLoading = true.obs;
var isConnected = false.obs;

@override
void onInit() {
super.onInit();
connectWebSocket();
connectWebSocket();
}

Future<void> connectWebSocket() async {
isLoading.value = true;
bool connected = await _comService.wsConnect();
bool connected = await api.wsConnect(processIncomingData);
isConnected.value = connected;
isLoading.value = false;

if (connected) {
sendMessage("start_system_info");
api.sendMessageOverSocket("start_system_info");
} else {
print("Failed to connect to WebSocket.");
}
}


void processIncomingData(String message) {
print("WebSocket received data: $message");
try {
Map<String, dynamic> data = jsonDecode(message);
print("WebSocket received data: $message");

if (data.containsKey('cpu_usage') ||
data.containsKey('memory_usage_percentage') ||
data.containsKey('cloud_usage_percentage') ||
data.containsKey('traffic_usage')) {

_updateResourceUsage(data);
print("WebSocket Updated messages");
print("WebSocket updated resource usage data.");
}
} catch (e) {
print("Failed to process incoming data: $e");
Expand All @@ -53,26 +59,24 @@ class WsConnection extends GetxController {

void sendMessage(String message) async {
if (isConnected.value) {
_comService.sendMessage(message);
api.sendMessageOverSocket(message);
print("Sent message: $message");
} else {
print('E.WebSocket is not connected. Retrying in 1 second...');
print('WebSocket is not connected. Retrying in 1 second...');
await Future.delayed(Duration(seconds: 1));

if (isConnected.value) {
_comService.sendMessage(message);
api.sendMessageOverSocket(message);
} else {
print("E,Message was not sent.");
print("Message was not sent.");
}
}
}

@override
void onClose() {
super.onClose();
if (_comService.channel != null) {
_comService.channel!.sink.close();
print("WebSocket connection closed on controller dispose.");
}
api.closeWebSocketConnection();
print("WebSocket connection closed on controller dispose.");
}
}

0 comments on commit c7e32d8

Please sign in to comment.