Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mortzaCFT authored Nov 9, 2024
1 parent 6e4a7e7 commit 98d7e73
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
72 changes: 72 additions & 0 deletions lib/unit/api/HttpService.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'dart:convert';
import 'package:http/http.dart' as http;

class HttpService {
String? sessionId;

Future<void> login(String username, String password) async {
String url = 'http://127.0.0.1:8081/login';
try {
final response = await http.post(
Uri.parse(url),
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
'username': username,
'password': password,
}),
);

if (response.statusCode == 200) {
final jsonData = jsonDecode(response.body);
if (jsonData['login_status'] == 'pending') {
sessionId = jsonData['id'];
print("OTP sent, session id: $sessionId");
} else {
print("Unexpected login status: ${jsonData['login_status']}");
}
} else {
print("Login failed with status code: ${response.statusCode}");
}
} catch (e) {
print("Error during login request: $e");
}
}

Future<bool> verifyOtp(int otp) async {
String url = 'http://127.0.0.1:8081/verify_otp';
if (sessionId == null) {
print("ID not found");
return false;
}

try {
final response = await http.post(
Uri.parse(url),
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
'session_id': sessionId!,
'otp': otp,
}),
);

if (response.statusCode == 200) {
final jsonData = jsonDecode(response.body);
if (jsonData['login_status'] == 'success') {
print("login success");
return true;
} else {
print("Unexpected response: ${jsonData['login_status']}");
}
} else {
print("OTP verification failed with status code: ${response.statusCode}");
}
} catch (e) {
print("Error during OTP verification: $e");
}
return false;
}
}
50 changes: 50 additions & 0 deletions lib/unit/api/WebSocketService.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:web_socket_channel/web_socket_channel.dart';

class WebSocketService {
WebSocketChannel? channel;
bool _isConnected = false;

bool get isConnected => _isConnected;

Future<bool> wsConnect(String uri, Function(String) onMessageReceived) async {
try {
channel = WebSocketChannel.connect(Uri.parse(uri));
channel!.stream.listen(
(message) {
print("Received data: $message");
onMessageReceived(message);
},
onError: (error) {
print("WebSocket connection error: $error");
_isConnected = false;
channel?.sink.close();
},
onDone: () {
print("WebSocket connection closed.");
_isConnected = false;
},
);

_isConnected = true;
return true;
} catch (e) {
print("Failed to connect: $e");
_isConnected = false;
return false;
}
}

void sendMessage(String message) {
if (_isConnected && channel != null) {
channel!.sink.add(message);
print("Sent message: $message");
} else {
print('Cannot send message: WebSocket is not connected.');
}
}

void closeConnection() {
channel?.sink.close();
_isConnected = false;
}
}

0 comments on commit 98d7e73

Please sign in to comment.