-
Notifications
You must be signed in to change notification settings - Fork 4
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
서버가 클라이언트에게 Ping/Pong 요청 #776
Changes from 5 commits
74a10d8
4fa1eae
ed17a87
48fcc60
33f4148
9b879eb
edd8e7f
e9323a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,20 +16,27 @@ | |
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.socket.PingMessage; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class ChatWebSocketHandleTextMessageProvider implements WebSocketHandleTextMessageProvider { | ||
|
||
private static final String PING_STATUS_KEY = "pingStatus"; | ||
private final WebSocketChatSessions sessions; | ||
private final ObjectMapper objectMapper; | ||
private final MessageService messageService; | ||
|
@@ -136,6 +143,39 @@ private void updateReadMessageLog( | |
|
||
@Override | ||
public void remove(final WebSocketSession session) { | ||
log.info("{} 연결 종료", session); | ||
sessions.remove(session); | ||
} | ||
|
||
@Scheduled(fixedDelay = 60000) | ||
public void sendPingSessions() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 칭찬스케줄러를 잘 사용해주셨네요 👍👍🏻 |
||
final Set<WebSocketSession> webSocketSessions = getWebSocketSessions(); | ||
|
||
webSocketSessions.parallelStream() | ||
.forEach(this::sendPingMessage); | ||
} | ||
|
||
private Set<WebSocketSession> getWebSocketSessions() { | ||
return sessions.getChatRoomSessions() | ||
.values() | ||
.stream() | ||
.flatMap(webSocketSessions -> webSocketSessions.getSessions().stream()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 칭찬오! flatMap을 사용해주셨네요 👍🏻 |
||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private void sendPingMessage(final WebSocketSession session) { | ||
final Map<String, Object> attributes = session.getAttributes(); | ||
final boolean pingStatus = (boolean) attributes.get(PING_STATUS_KEY); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 선택pingStatus 대신 connected는 어떤가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. connected 좋습니다! |
||
if (!pingStatus) { | ||
sessions.remove(session); | ||
return; | ||
} | ||
|
||
attributes.put(PING_STATUS_KEY, false); | ||
try { | ||
session.sendMessage(new PingMessage()); | ||
} catch (IOException e) { | ||
log.error("ping 보내기 실패 : {} ", session); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 질문ping 전송에 실패하면 로그만 찍고 다시 ping을 전송하지 않아도 괜찮을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 ping이 실패한 경우 다시 ping 요청을 수행하더라도 실패할 것이라 생각했습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 실패한 로직을 재전송하는 부분이 필요할 것 같다는 생각이 들었어요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다음 회의에서 이에 대해 한번 논의해 보기로 해요! |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ public boolean beforeHandshake( | |
) throws Exception { | ||
attributes.put("userId", findUserId(request)); | ||
attributes.put("baseUrl", ImageRelativeUrl.USER.calculateAbsoluteUrl()); | ||
attributes.put("pingStatus", true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 선택리마인드 차원에서 코멘트 남깁니다 |
||
|
||
return super.beforeHandshake(request, response, wsHandler, attributes); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,17 +7,20 @@ | |
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.socket.CloseStatus; | ||
import org.springframework.web.socket.PongMessage; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
import org.springframework.web.socket.handler.TextWebSocketHandler; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class WebSocketHandler extends TextWebSocketHandler { | ||
|
||
private static final String TYPE_KEY = "type"; | ||
private static final String PING_STATUS_KEY = "pingStatus"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 선택 & 질문여기도 함께 변경해주세요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 |
||
|
||
private final WebSocketHandleTextMessageProviderComposite providerComposite; | ||
private final ObjectMapper objectMapper; | ||
|
@@ -43,4 +46,10 @@ public void afterConnectionClosed(final WebSocketSession session, final CloseSta | |
final WebSocketHandleTextMessageProvider provider = providerComposite.findProvider(textMessageType); | ||
provider.remove(session); | ||
} | ||
|
||
@Override | ||
public void handlePongMessage(WebSocketSession session, PongMessage message) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 필수제 눈에는 왜 Final이 이렇게 잘 보이는 걸까요..? |
||
final Map<String, Object> attributes = session.getAttributes(); | ||
attributes.put(PING_STATUS_KEY, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 선택PingStatus를 Enum으로 관리하고, value로 true/false를 관리하는 것은 어떤가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PingStatus가 위 리뷰에 따라 conected로 변경되었습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋습니다! 반영해주신거 확인했습니다 |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
필수
static 필드 밑에 한 줄 개행해주세요!