Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] 버그수정 #90

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/kutaverse/game/chat/domain/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
import lombok.experimental.WithBy;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
Expand All @@ -11,6 +13,8 @@
@Document(collation = "chat")
@AllArgsConstructor
@Builder
@Getter
@ToString
public class Chat {

@Id
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/kutaverse/game/chat/service/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ public class ChatService {
public Mono<Chat> save(ChatRequestDto chatRequestDto){
Chat chat=Chat.builder()
.senderUserId(chatRequestDto.getUserId())
.nickname(chatRequestDto.getNickname())
.content(chatRequestDto.getContent())
.createdAt(LocalDateTime.now())
.build();
return chatRepository.insert(chat);
return chatRepository.insert(chat)
.doOnNext(savedChat -> {
System.out.println("Saved chat: " + savedChat);
});

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kutaverse.game.websocket.chat;

import com.fasterxml.jackson.databind.ObjectMapper;
import kutaverse.game.chat.domain.Chat;
import kutaverse.game.chat.service.ChatService;
import kutaverse.game.websocket.chat.dto.request.ChatRequestDto;
import kutaverse.game.websocket.chat.dto.response.ChatResponseDto;
Expand All @@ -9,6 +10,7 @@
import org.springframework.web.reactive.socket.WebSocketSession;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -26,6 +28,7 @@ public ChatWebSocketHandler(ChatService chatService){
public Mono<Void> handle(WebSocketSession session) {
sessions.put(session.getId(), session);
ObjectMapper objectMapper=new ObjectMapper();

session.receive()
.map(WebSocketMessage::getPayloadAsText)
.map(data-> {
Expand All @@ -36,19 +39,33 @@ public Mono<Void> handle(WebSocketSession session) {
}
})
.doOnNext(System.out::println)
.map(chatRequest -> {
chatService.save(chatRequest).subscribe();
ChatResponseDto response = new ChatResponseDto(chatRequest.getUserId(), chatRequest.getNickname(), chatRequest.getContent());
try {
return objectMapper.writeValueAsString(response);
} catch (Exception e) {
throw new RuntimeException(e);
}
.flatMap(chatRequest -> {
//chatService.save(chatRequest).subscribe();
return chatService.save(chatRequest).map(savedChat -> {
// 저장된 결과를 기반으로 응답 생성
System.out.println(savedChat.getId());
ChatResponseDto response = new ChatResponseDto(savedChat.getSenderUserId(), savedChat.getNickname(), savedChat.getContent());
try {
return objectMapper.writeValueAsString(response);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
})
.flatMap(data->{
Flux.fromIterable(sessions.values())
.flatMap(wsSession -> wsSession.send(Mono.just(wsSession.textMessage(data)))).subscribe();
return Mono.never();
return Flux.fromIterable(sessions.entrySet())
.flatMap(entry -> {
WebSocketSession wsSession = entry.getValue();
String key = entry.getKey();
if(!wsSession.isOpen()) {
wsSession.close().subscribe();
sessions.remove(key);
return Mono.never();
}
else
return wsSession.send(Mono.just(wsSession.textMessage(data)));
}).then();
//return Mono.never();
})
.subscribe();

Expand Down
Loading