본문 바로가기
C.W.K.
Stream
Lesson 05 of 07 · published

자동 재연결과 ACK

~11 min · library, reconnect, ack

Level 0Poller
0 XP0/60 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

내장 재연결

Socket.IO 에는 지터를 더한 지수 백오프 재연결이 들어 있어. 트랙 2 에서 직접 만든 기능을 여기서는 선택지 5개만 설정하면 쓸 수 있지. 대부분의 앱에 알맞은 기본값은 최대 시도 횟수 무한, 시작 지연 1초, 상한 5~30초, 지터 0.5 야.

재연결 이벤트

socket.io.on('reconnect') 로 재연결 성공을 듣고, reconnect_attempt 로 재시도를, reconnect_failed 로 영구 실패를 알 수 있어. 이 이벤트로 UI 의 ‘연결하는 중…’ 덮개와 ‘오프라인’ 배지를 움직여.

응답 확인용 ACK

서버 이벤트 처리기가 값이나 여러 인자를 위한 목록을 반환하면 클라이언트의 ACK 페이로드가 돼. 트랙 5 에서 만든 상관관계 ID 처리 전체가 코드 한 줄로 줄어드는 셈이야.

Code

재연결 선택지·javascript
const socket = io('https://api.example.com', {
  auth: { token },
  reconnection: true,
  reconnectionAttempts: Infinity,
  reconnectionDelay: 1_000,        // start delay
  reconnectionDelayMax: 30_000,    // cap
  randomizationFactor: 0.5,        // ±50% jitter
});

socket.io.on('reconnect_attempt', (n) => {
  console.log('reconnect attempt', n);
  setBanner('Reconnecting...');
});

socket.io.on('reconnect', (n) => {
  console.log('reconnected after', n, 'attempts');
  setBanner(null);
});

socket.io.on('reconnect_failed', () => {
  console.log('reconnect failed permanently');
  setBanner('Offline. Refresh to retry.');
});
서버가 ACK 데이터 반환하기·python
@sio.on('chat:message')
async def chat(sid, data):
    saved = await db.save_message(data)
    return {'status': 'ok', 'id': saved.id, 'ts': saved.created_at}

External links

Exercise

재연결 선택지와 세 reconnect_* 이벤트를 ‘상태: 온라인 / 재연결 중 / 오프라인’ 배지에 연결해. 서버를 30초 동안 내리고 배지가 상태 사이를 바꾸는지, 서버를 다시 시작하면 온라인으로 돌아오는지 확인해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.