Skip to content

[Feat] 웹소켓 연결 #414#427

Merged
edv-Shin merged 13 commits intofeat/websocketfrom
feat/websocket-414
Jan 7, 2026
Merged

[Feat] 웹소켓 연결 #414#427
edv-Shin merged 13 commits intofeat/websocketfrom
feat/websocket-414

Conversation

@edv-Shin
Copy link
Copy Markdown
Contributor

#️⃣연관된 이슈

ex) #이슈번호, #이슈번호

#414

📝작업 내용

  • 폴링 방식에서 웹소켓으로 채팅 기능을 리팩토링했습니다.
    • 웹소켓 연결 및 실시간 메시징 처리
    • 네트워크 상태 변화를 감지하는 NetworkMonitor 구현
      • 네트워크가 끊겼을 경우 재연결을 위한 Exponential Backoff 로직 적용
  • memberId와 senderId를 비교하여 채팅 메세지를 표시하도록 추가 구현이 필요합니다

이번 PR에서 작업한 내용을 간략히 설명해주세요(이미지 첨부 가능)

스크린샷 (선택)

💬리뷰 요구사항(선택)

리뷰어가 특별히 봐주었으면 하는 부분이 있다면 작성해주세요

ex) 메서드 XXX의 이름을 더 잘 짓고 싶은데 혹시 좋은 명칭이 있을까요?

@edv-Shin edv-Shin requested a review from yunjaena December 29, 2025 10:45
@edv-Shin edv-Shin self-assigned this Dec 29, 2025
@@ -0,0 +1,55 @@
package com.project200.common.utils
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core network module로 빼는건 어떤가요

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 생각인 것 같습니다!
추후 모듈로 분리하는 pr 따로 올리겠습니다

CoroutineScope(Dispatchers.IO).launch {
networkMonitor.networkState.collect { isConnected ->
if (isConnected && isUserInChatRoom) {
retryCount = 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retry count가 thread safe한가요?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지적해주신대로 retryCount가 현재 서로 다른 스레드에서 동시에 접근하고 있습니다.

  1. NetworkMonitor의 Flow 콜백
  2. WebSocketListener의 이벤트
  3. handleConnectionFailure 코루틴
    동시 접근 시에 경쟁 상태가 되는 문제가 발생하는 것을 확인했습니다.

AtomicInteger를 사용하여 retryCount가 일관적으로 유지되도록 수정했습니다.
5a9560f


// 소켓 연결
val wsUrl =
if (BuildConfig.DEBUG) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constant로 바꿔주세요

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다! d47824d

* 소켓 연결 처리
*/
private fun connectSocketInternal(chatRoomId: Long) {
if (webSocket != null) return
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

백그라운드 스레드에서 웹소켓이 연결되서 타이밍 이슈가 발생하면 어떻게 되나요

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

타이밍 이슈가 발생하면 webSocket이 생성되기 전에 트리거 되면서, 여러 개의 웹소켓이 생성될 수 있을 것 같습니다.

connectionMutex.withLock을 사용하여 하나의 활성 웹소켓 인스턴스만 존재하도록 보장했습니다.
1e4787d

private fun handleConnectionFailure(t: Throwable) {
if (!isUserInChatRoom) return
if (!networkMonitor.isCurrentlyConnected()) return

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CouroutineScope cancle처리는 어떻게 되나요


Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존 코드에서 관리되지 않는 CoroutineScope를 사용하면 채팅방을 나간 뒤에도 작업이 취소되지 않는 문제가 있었습니다.

클래스 전용 repositoryScope를 정의하고, 재연결(retryJob) 및 하트비트(heartbeatJob) 작업을 별도의 Job 변수로 관리하여 cancel 할 수 있도록 수정했습니다.
4fb3d61

@edv-Shin edv-Shin requested a review from yunjaena December 30, 2025 18:04
@edv-Shin edv-Shin changed the base branch from dev to feat/websocket January 7, 2026 03:23
@edv-Shin edv-Shin merged commit 1296e6a into feat/websocket Jan 7, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants