-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |