๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป ํ”„๋ก ํŠธ์—”๋“œ

React Native ์›น์†Œ์ผ“ ์ฑ„ํŒ… ๊ตฌํ˜„

by megan07 2024. 1. 10.

์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋ฉด์„œ RN์œผ๋กœ ์ฑ„ํŒ…์„ ๊ตฌํ˜„ ์ค‘์ด๋‹ค

 

 

์ผ๋‹จ ํด๋ผ์ด์–ธํŠธ์—์„œ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ธ ์›น์†Œ์ผ“ ์—ฐ๊ฒฐ์„ ๊ตฌํ˜„ํ•ด๋ดค๋‹ค

 

์šฐ์„  ์›น์†Œ์ผ“ ์—ฐ๊ฒฐ์„ ์œ„ํ•œ WebSocketProvider๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ฑ„ํŒ…๋ฐฉ์„ ๊ฐ์‹ธ์คฌ์Šต๋‹ˆ๋‹ค.

์›น์†Œ์ผ“๊ณผ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ๋“ค์„ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค๊นŒ ํ•˜๋‹ค๊ฐ€ ์–ด์ฐจํ”ผ ํ•˜๋‚˜์˜ ์›น์†Œ์ผ“ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

useMemo๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ† ํฐ์ด ์—…๋ฐ์ดํŠธ ๋˜์—ˆ์„ ๋•Œ ์ƒˆ๋กœ์šด ์›น์†Œ์ผ“์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

WebSocketContext๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฑ„ํŒ…๋ฐฉ์—์„œ ์›น์†Œ์ผ“์„ ํ†ตํ•ด ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ  ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import {tokenState} from '@/store/loginAtom';
import React, {createContext, useMemo} from 'react';
import {useRecoilValue} from 'recoil';

type Props = {
  children: React.ReactNode;
};

export const WebSocketContext = createContext<WebSocketClient | null>(null);

export const WebSocketProvider = ({children}: Props) => {
  const token = useRecoilValue(tokenState);

  const webSocket = useMemo(() => {
    if (!token) return null;

    return new WebSocketClient(token.accessToken);
  }, [token]);

  return (
    <WebSocketContext.Provider value={webSocket}>
      {children}
    </WebSocketContext.Provider>
  );
};

export default WebSocketProvider;

 

 

 

WebSocketClient ํด๋ž˜์Šค๋Š” ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

class WebSocketClient {
  socket: WebSocket | null = null;
  token = '';
  pingInterval: NodeJS.Timeout | null = null;

//์ƒ์„ฑ์ž์—์„œ ํ† ํฐ์„ ์ €์žฅํ•˜๊ณ  ์›น์†Œ์ผ“ ์—ฐ๊ฒฐ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค
  constructor(token: string) {
    this.token = token;
    this.socket = this.connectWebSocket();
  }


//์›น์†Œ์ผ“ ์—ฐ๊ฒฐ ๋ฉ”์„œ๋“œ
  connectWebSocket = () => {
    const socket = new WebSocket(
      `์š”์ฒญ uri?authorization=${this.token}`,
    );

    //์›น์†Œ์ผ“์ด ์—ฐ๊ฒฐ๋˜๋ฉด ํ•‘์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค
    socket.onopen = () => {
      console.log('WebSocket connected');
      this.startPing();
    };

    //์›น์†Œ์ผ“์ด ๋Š๊ฒผ์„ ๊ฒฝ์šฐ, ์žฌ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค
    socket.onclose = () => {
      console.log('WebSocket connection closed');
      this.reconnect();
    };

    //๋ฐ›์€ ๋ฉ”์‹œ์ง€๋ฅผ handleMessage๋ฉ”์„œ๋“œ๋กœ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค
    socket.onmessage = e => {
      this.handleMessage(e);
    };

    //์—๋Ÿฌ๋กœ ์—ฐ๊ฒฐ์ด ๋Š๊ฒผ์„ ๊ฒฝ์šฐ, ์žฌ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค
    socket.onerror = err => {
      console.error('WebSocket error:', err);
      this.reconnect();
    };
    
    return socket;
  };

//ํ•‘ ๋ฉ”์„œ๋“œ, ๋งˆ์ง€๋ง‰ ์š”์ฒญ์œผ๋กœ๋ถ€ํ„ฐ 30์ดˆ๊ฐ„ ์—ฐ๊ฒฐ์ด ์ง€์†๋˜๊ธฐ ๋•Œ๋ฌธ์— 25์ดˆ ๊ฐ„๊ฒฉ์œผ๋กœ ํ•‘
  startPing = () => {
    this.pingInterval = setInterval(() => {
      if (this.socket) {
        this.socket.send(
          JSON.stringify(ํ•‘๋ฉ”์‹œ์ง€๊ฐ€ ๋“ค์–ด๊ฐˆ ๊ณณ),
        );
      }
    }, 25000);
  };

//ํ•‘ ์ธํ„ฐ๋ฒŒ ์ด๋ฒคํŠธ๋ฅผ ํด๋ฆฌ์–ด ํ•˜๋Š” ๋ฉ”์„œ๋“œ
  stopPing = () => {
    if (this.pingInterval) {
      clearInterval(this.pingInterval);
      this.pingInterval = null;
    }
  };

//์žฌ์—ฐ๊ฒฐ ๋ฉ”์„œ๋“œ
  reconnect = () => {
    console.log('Reconnecting...');
    this.stopPing();
    setTimeout(() => {
      this.connectWebSocket();
    }, 300);
  };

//์ˆ˜์‹  ๋ฉ”์‹œ์ง€ ํ•ธ๋“ค ๋ฉ”์„œ๋“œ
  handleMessage = (event: WebSocketMessageEvent) => {
    const parse = JSON.parse(event.data);
    console.log(parse);
    //๋ฉ”์‹œ์ง€ ์•Œ์•„์„œ ํ•ธ๋“ค
  };

//๋ฉ”์‹œ์ง€ ์ „์†ก ๋ฉ”์„œ๋“œ
//์›น์†Œ์ผ“์ด ์—ฐ๊ฒฐ๋œ ์ƒํƒœ์—์„œ๋งŒ ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค
  sendMessage = (roomId: number, content: string) => {
    if (this.socket && this.socket.readyState === WebSocket.OPEN) {
      const message = JSON.stringify({action: 'TEXT', roomId, content});
      this.socket.send(message);
    } else {
      console.error('WebSocket is not connected');
    }
  };
}

 

 

์‚ฌ์‹ค ์›น์†Œ์ผ“ ํ†ต์‹ ์ด ์ž˜ ๋˜์ง€๋Š”์ง€๋งŒ ํ™•์ธํ•œ๊ฑฐ๋ผ์„œ ์•„์ง ๊ฐˆ ๊ธธ์ด ๋ฉ€๋‹ค...

ํŒ€์›์—๊ฒŒ ์ฝ”๋“œ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›๊ธฐ ์ „์ด๋ผ์„œ ์•„๋งˆ ์ˆ˜์ •๋˜์ง€ ์•Š์„๊นŒ ์‹ถ๋‹ค..

 

 

์ฑ„ํŒ… ํผ๋ธ”๋ฆฌ์‹ฑ์„ ๋‹ค ํ•˜๊ณ  ๋‚˜๋ฉด ๋จธ๋ฆฌ๊ฐ€ ์•„ํ”Œ ๊ฒƒ ๊ฐ™๋‹ค...

์ฑ„ํŒ… ๋ฉ”์‹œ์ง€๋“ค์„ ๋กœ์ปฌ์— ์ €์žฅํ•˜๊ณ  FCM ์ฒ˜๋ฆฌ๋„ ํ•ด์•ผ ํ•˜๊ณ ,,

 

์–ธ์ œ ๋‹คํ•˜์ง€ ใ…Žใ…Žใ…Ž