Skip to content

Commit

Permalink
#66 메세지 실시간 스트리밍(실시간으로 읽기 확인)
Browse files Browse the repository at this point in the history
  • Loading branch information
opjoobe committed Aug 3, 2022
1 parent 4b3cce0 commit e74005f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
46 changes: 30 additions & 16 deletions lib/screens/message/chat_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ class ChatScreen extends StatefulWidget {
int friendID;
String friendName;
String friendImage;
int notReadCnt;

ChatScreen(
{Key? key,
required this.chatroomID,
required this.loginID,
required this.friendID,
required this.friendName,
required this.friendImage,
required this.notReadCnt})
required this.friendImage})
: super(key: key);

@override
Expand All @@ -37,6 +35,30 @@ class _ChatScreenState extends State<ChatScreen> {
int? myConnId;
int? friendConnId;
StreamController _streamController = StreamController();
int? notReadCnt;

getChatReadCnts() async {
try {
var dio = Dio();
Response response = await dio.get(
'http://34.64.217.3:3000/api/chatroom/readcnts?id_1=${widget.loginID}&id_2=${widget.friendID}');
if (response.statusCode == 200) {
final jsonData = response.data;
int tempCnt = int.parse(jsonData['notreadcnt']);
setState(() {
notReadCnt = tempCnt;
});
return await getChatMessages(tempCnt);
} else {
print('error');
return false;
}
} on DioError catch (e) {
final errorjson = jsonDecode(e.response.toString());
print(errorjson);
return false;
}
}

getChatConns() async {
try {
Expand All @@ -45,9 +67,6 @@ class _ChatScreenState extends State<ChatScreen> {
'http://34.64.217.3:3000/api/chatroom/conns?id_1=${widget.loginID}&id_2=${widget.friendID}');
if (response.statusCode == 200) {
final jsonData = response.data;
print(jsonData[0].runtimeType);
print(jsonData[1].runtimeType);
print(jsonData.runtimeType);
setState(() {
myConnId = jsonData[0];
friendConnId = jsonData[1];
Expand All @@ -69,11 +88,11 @@ class _ChatScreenState extends State<ChatScreen> {
}
}

getChatMessages(int chatroomid) async {
getChatMessages(int notReadCnt) async {
try {
var dio = Dio();
Response response = await dio.get(
'http://34.64.217.3:3000/api/chatroom/message?room_id=$chatroomid');
'http://34.64.217.3:3000/api/chatroom/message?room_id=${widget.chatroomID}');
if (response.statusCode == 200) {
final jsonData = response.data;
List<ChatModel> temp = [];
Expand All @@ -82,16 +101,11 @@ class _ChatScreenState extends State<ChatScreen> {
int i = totalSends;
jsonData.forEach((e) {
var nowtime = DateTime.parse(e['sendat']);
print(nowtime.runtimeType);
var realnow = DateTime.now();
print(realnow.runtimeType);
Duration diff = realnow.difference(nowtime);
print(diff.inMinutes);
print(diff.inHours);
print(diff.inDays);
bool nowisread = true;
if (e['sender'] == widget.loginID) {
if (i <= widget.notReadCnt) {
if (i <= notReadCnt) {
nowisread = false;
}
i -= 1;
Expand All @@ -109,7 +123,7 @@ class _ChatScreenState extends State<ChatScreen> {
_messages = temp;
_streamController.add(_messages);
});
print('접속 성공!');
return true;
} else {
print('error');
return false;
Expand Down Expand Up @@ -219,8 +233,8 @@ class _ChatScreenState extends State<ChatScreen> {

@override
void initState() {
getChatReadCnts();
getChatConns();
getChatMessages(widget.chatroomID);
super.initState();
// enterChatRoom();
initializeSocket();
Expand Down
1 change: 0 additions & 1 deletion lib/screens/mypage/profile_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ class _ProfilePageState extends State<ProfilePage> {
friendID: widget.friendId!, // not isMe
friendName: user.nickname,
friendImage: user.imagePath,
notReadCnt: notReadCnt,
);
}));
} else {
Expand Down
1 change: 0 additions & 1 deletion lib/widgets/message/conversation_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class _ConversationListState extends State<ConversationList> {
friendID: widget.friendID,
friendName: widget.friendName,
friendImage: widget.friendImage,
notReadCnt: widget.notReadCnt,
);
}));
},
Expand Down

0 comments on commit e74005f

Please sign in to comment.