117 lines
3.5 KiB
Dart
117 lines
3.5 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import '../models/chat_message.dart';
|
|
|
|
class ChatService {
|
|
// 서버 URL
|
|
static const String serverUrl = 'http://49.238.167.71:8000/chat/';
|
|
|
|
// 타임아웃 설정 (초)
|
|
static const int timeoutSeconds = 30;
|
|
|
|
/// 사용자 메시지를 서버로 전송하고 응답을 받는 메서드
|
|
static Future<String> sendMessage(String userMessage) async {
|
|
try {
|
|
// 요청 생성
|
|
final requestBody = {
|
|
'device_id': 1,
|
|
'locale': 'ko-KR',
|
|
'message': userMessage,
|
|
'session_id': 'session-001',
|
|
'user_id': 'user-123',
|
|
};
|
|
|
|
// JSON 형식 확인
|
|
final jsonBody = jsonEncode(requestBody);
|
|
print('서버로 요청 전송 (Map): $requestBody');
|
|
print('서버로 요청 전송 (JSON): $jsonBody');
|
|
|
|
final uri = Uri.parse(serverUrl);
|
|
print('요청 URI: $uri');
|
|
print('요청 헤더: {Content-Type: application/json, accept: application/json}');
|
|
print('요청 바디: ${jsonEncode(requestBody)}');
|
|
|
|
// POST 요청 전송
|
|
final response = await http.post(
|
|
Uri.parse(serverUrl),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'accept': 'application/json',
|
|
},
|
|
body: jsonEncode(requestBody),
|
|
).timeout(
|
|
const Duration(seconds: timeoutSeconds),
|
|
onTimeout: () => throw TimeoutException('서버 응답 시간 초과'),
|
|
);
|
|
|
|
print('서버 응답 상태코드: ${response.statusCode}');
|
|
print('서버 응답 본문: ${response.body}');
|
|
|
|
// 상태 코드 확인
|
|
if (response.statusCode == 200) {
|
|
final jsonResponse = jsonDecode(response.body);
|
|
|
|
print('전체 응답: $jsonResponse');
|
|
|
|
// 'message' 필드 추출
|
|
final responseMessage = jsonResponse['message'] as String?;
|
|
|
|
if (responseMessage != null && responseMessage.isNotEmpty) {
|
|
print('응답 메시지: $responseMessage');
|
|
return responseMessage;
|
|
} else {
|
|
print('message 필드가 없습니다');
|
|
// 혹시 다른 필드에 응답이 있는지 확인
|
|
print('응답 전체: ${jsonResponse.toString()}');
|
|
return '응답을 받지 못했습니다';
|
|
}
|
|
} else {
|
|
// 실패
|
|
print('서버 오류: ${response.statusCode}');
|
|
return '서버 오류 (${response.statusCode})';
|
|
}
|
|
} on TimeoutException catch (e) {
|
|
print('타임아웃: $e');
|
|
return '서버 응답이 없습니다. 시간 초과';
|
|
} on FormatException catch (e) {
|
|
print('JSON 파싱 오류: $e');
|
|
return 'JSON 형식 오류';
|
|
} catch (e) {
|
|
print('오류 발생: $e');
|
|
return '오류가 발생했습니다: $e';
|
|
}
|
|
}
|
|
|
|
/// 서버 연결 테스트
|
|
static Future<bool> testConnection() async {
|
|
try {
|
|
print('서버 연결 테스트 중...');
|
|
|
|
final response = await http.get(
|
|
Uri.parse(serverUrl),
|
|
).timeout(
|
|
const Duration(seconds: timeoutSeconds),
|
|
);
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 405) {
|
|
print('서버 연결 성공');
|
|
return true;
|
|
} else {
|
|
print('서버 연결 실패: ${response.statusCode}');
|
|
return false;
|
|
}
|
|
} catch (e) {
|
|
print('서버 연결 오류: $e');
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 타임아웃 예외
|
|
class TimeoutException implements Exception {
|
|
final String message;
|
|
TimeoutException(this.message);
|
|
|
|
@override
|
|
String toString() => message;
|
|
} |