Skip to content

๐Ÿ› ๏ธ [ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ…] socket.io ๊ฒฝ๋กœ๋กœ ์ง€์†์ ์œผ๋กœ HTTP ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ๋ฌธ์ œ

๊น€๊ทผ์šฐ edited this page Dec 1, 2024 · 1 revision

๐Ÿšจย ๋ฌธ์ œ ์ƒํ™ฉ

๊ณต๋ถ€๋ฐฉ ํŽ˜์ด์ง€์— ์ ‘์† ํ›„ /socket.io ๊ฒฝ๋กœ๋กœ ์ง€์†์ ์œผ๋กœ HTTP ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•œ๋‹ค.

Nov-30-2024_12-23-50

๊ณต๋ถ€๋ฐฉ ๋ฆฌ์ŠคํŠธ ํŽ˜์ด์ง€๋กœ ๋‚˜๊ฐ„ ํ›„์—๋„ /socket.io ๊ฒฝ๋กœ๋กœ ์ง€์†์ ์œผ๋กœ HTTP ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ์žˆ๋‹ค.

Nov-30-2024_12-28-54

HTTP ์š”์ฒญ ํ—ค๋”

์š”์ฒญ ํ˜•์‹: XMLHttpRequest

GET /socket.io/?EIO=4&transport=polling&t=lxrjhd23 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: ko,en;q=0.9
Connection: keep-alive
Host: gomz.kr
Referer: https://gomz.kr/study-room/114
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
sec-ch-ua: "Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "macOS"

%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA_2024-11-30_12 25 47

์ฟผ๋ฆฌ ๋ฌธ์ž์—ด ๋งค๊ฐœ๋ณ€์ˆ˜

  • EIO: 4
  • transport: polling
  • t: lxrjhd23

%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA_2024-11-30_12 25 54

HTTP ์‘๋‹ต ํ—ค๋”

HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Sat, 30 Nov 2024 08:05:04 GMT
Content-Type: text/html
Last-Modified: Sat, 30 Nov 2024 04:03:55 GMT
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"674a8eab-3d7"
Content-Encoding: gzip

HTTP ์‘๋‹ต

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>GOMZ - ์‹ค์‹œ๊ฐ„ ์˜จ๋ผ์ธ ํ•™์Šต ํ”Œ๋žซํผ</title>
    <link rel="icon" href="/images/gomz-logo.png" />
    <meta
      name="description"
      content="์บ ์„ ์ผœ๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๊ณผ ํ•จ๊ป˜ ๊ณต๋ถ€ํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ธฐ๋กํ•  ์ˆ˜ ์žˆ๋Š” ์˜จ๋ผ์ธ ํ•™์Šต ํ”Œ๋žซํผ์ž…๋‹ˆ๋‹ค."
    />
    <meta name="keywords" content="๋น„๋Œ€๋ฉด, ๊ณต๋ถ€๋ฐฉ, ํ•™์Šต, ํƒ€์ด๋จธ, ํ™”์ƒ ํšŒ์˜, ํ•™์Šต ์‹œ๊ฐ„ ๊ธฐ๋ก" />
    <link
      rel="stylesheet"
      as="style"
      crossorigin
      href="https://cdn.jsdelivr.net/gh/orioncactus/[email protected]/dist/web/variable/pretendardvariable-dynamic-subset.min.css"
    />
    <script type="module" crossorigin src="/assets/index-wKdhJbca.js"></script>
    <link rel="stylesheet" crossorigin href="/assets/index-C6ivelCl.css">
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA_2024-11-30_12 26 06

โš ๏ธย ๋ฌธ์ œ ์›์ธ

useWebRTC์—์„œ signaling ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐ๋˜๋Š” socket์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด useRef๋กœ ๊ด€๋ฆฌํ•˜๊ณ , ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ io()๋กœ ์ƒ์„ฑํ•ด์„œ ๋„ฃ์–ด์ฃผ๊ณ  ์žˆ์—ˆ๋‹ค.

// useWebRTC.ts
const useWebRTC = (): [WebRTCState, WebRTCControls] => {
  const socket = useRef(io());
  
  ...
  
  const joinRoom = async (roomId: string) => {
    ...

    socket.current = signalingClient(configuration, localStreamRef.current, webRTCMap.current);
    socket.current.emit('joinRoom', { roomId });

    return socket.current;
  };
  
  ...

๊ณต๋ถ€๋ฐฉ ํŽ˜์ด์ง€์— ์ž…์žฅํ•˜๋ฉด joinRoom ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ณ  signalingClient ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ socket์„ ๊ฐ€์ ธ์™€์„œ ์ €์žฅํ•œ๋‹ค.

signalingClient์—์„œ๋Š” ์†Œ์ผ“ URL์„ https://gomz.kr:3000๋กœ ์„ค์ •ํ•ด์„œ ์ƒ์„ฑํ•˜๊ณ  ์žˆ๋‹ค.

// signalingClient.ts
const signalingClient = (...[configuration, localStream, webRTCMap]: SignalingClientParams) => {
  const socket = io(import.meta.env.VITE_SIGNALING_SERVER_URL);
  // ์—ฌ๊ธฐ์„œ import.meta.env.VITE_SIGNALING_SERVER_URL์€ https://gomz.kr:3000์ด๋‹ค.
  
  ...

signalingClient๊ฐ€ ์‹คํ–‰๋˜๋ฉด socket.current์˜ ์ฐธ์กฐ๊ฐ’์ด ์ƒˆ๋กœ์šด ์†Œ์ผ“์œผ๋กœ ๋ณ€๊ฒฝ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด ์†Œ์ผ“ ๊ฐ์ฒด๊ฐ€ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜ ๋Œ€์ƒ์ด ๋˜์–ด ์‚ฌ๋ผ์งˆ ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ–ˆ๋‹ค. โ†’ ๊ฒฐ๋ก : ์‚ฌ๋ผ์ง€์ง€ ์•Š๊ณ  ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์ฃผํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ์‚ฌ๋ผ์ง€์ง€ ์•Š๋Š” ์ด์œ ๋Š” ์ด์–ด์„œ ์„ค๋ช….

Socket.IO - Client API๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด io()๋กœ ์ƒ์„ฑํ•  ๋•Œ URL์„ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ์ƒ์„ฑํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋„๋ฉ”์ธ์˜ ๋ฃจํŠธ(/) ๊ฒฝ๋กœ๋กœ ์—ฐ๊ฒฐ์„ ์‹œ๋„ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

Socket.IO - How it works๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด WebSocket ์—ฐ๊ฒฐ์ด ๋„คํŠธ์›Œํฌ ํ™˜๊ฒฝ, ๋ฐฉํ™”๋ฒฝ ๋˜๋Š” ํ”„๋ก์‹œ ์„ค์ • ๋•Œ๋ฌธ์— ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ Socket.IO๋Š” ์ž๋™์œผ๋กœ HTTP Long-polling ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  Socket.IO๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ HTTP Long-Polling ๋ฐฉ์‹์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„ ์—ฐ๊ฒฐ์„ ์‹œ์ž‘ํ•œ ํ›„, WebSocket์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ๋ฅผ ์‹œ๋„ํ•œ๋‹ค.

๋ธŒ๋ผ์šฐ์ €์˜ ๋„คํŠธ์›Œํฌ ๋ชจ๋‹ˆํ„ฐ์—์„œ ํ™•์ธํ•œ Socket.IO ์—…๊ทธ๋ ˆ์ด๋“œ ๋ฉ”์ปค๋‹ˆ์ฆ˜

%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA_2024-11-30_18 05 42

3000๋ฒˆ ํฌํŠธ๋กœ๋Š” ๊ณต์‹๋ฌธ์„œ์— ๋‚˜์˜จ๋ฐ๋กœ HTTP Long-polling ๋ฐฉ์‹์œผ๋กœ ์‹œ์ž‘ํ•ด์„œ WebSocket์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ๋˜๊ณ  ์žˆ์—ˆ๋‹ค.

์ดํ›„์— ๊ณ„์†๋˜๋Š” ์š”์ฒญ์˜ ๊ฒฝ๋กœ๋ฅผ ๋ณด๋ฉด 3000๋ฒˆ ํฌํŠธ์˜ ๋ฃจํŠธ(https://gomz.kr:3000/)๊ฐ€ ์•„๋‹Œ ๋„๋ฉ”์ธ์˜ ๋ฃจํŠธ(https://gomz.kr/) ๊ฒฝ๋กœ๋กœ GET ์š”์ฒญ์„ ๊ณ„์† ๋ณด๋‚ด๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA_2024-11-30_18 10 01

์ฆ‰, ๋งจ ์ฒ˜์Œ์— ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์ƒ์„ฑํ•œ ์†Œ์ผ“์ด ์—ฐ๊ฒฐ์„ ๊ณ„์† ์‹œ๋„ํ•˜๊ณ  ์žˆ๋˜ ๊ฒƒ์ด์˜€๋‹ค.

์ฒซ ๋ฒˆ์งธ GET ์š”์ฒญ(ํ•ธ๋“œ์‰์ดํฌ)์ด ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์•„ ๊ณ„์†ํ•ด์„œ ์žฌ์‹œ๋„ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ถ”์ •ํ•œ๋‹ค.

socket.current์˜ ์ฐธ์กฐ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ์ „์— ๊ธฐ์กด ์†Œ์ผ“์— ๋Œ€ํ•ด disconnect(), close() ๋“ฑ์˜ ์กฐ์น˜๋ฅผ ์ทจํ•ด๋„ ์—ฐ๊ฒฐ ์‹œ๋„๋ฅผ ๋ฉˆ์ถœ ์ˆ˜ ์—†์—ˆ๋‹ค.

๊ฒฐ๋ก :

  • socket.current์˜ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์ƒ์„ฑํ•œ ์†Œ์ผ“์ด ์ž˜๋ชป๋œ ๊ฒฝ๋กœ๋กœ ๊ณ„์†ํ•ด์„œ ์—ฐ๊ฒฐ ์‹œ๋„๋ฅผ ํ•˜๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜ ๋Œ€์ƒ์ด ๋˜์ง€ ์•Š์•„ ๋ฉ”๋ชจ๋ฆฌ์— ๊ณ„์† ์ƒ์ฃผํ•˜๊ณ  ์žˆ์—ˆ๋‹ค.
  • ๊ณต๋ถ€๋ฐฉ ๋ฆฌ์ŠคํŠธ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•ด๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์ฃผํ•˜๋ฉฐ ๊ณ„์†ํ•ด์„œ ์—ฐ๊ฒฐ ์‹œ๋„๋ฅผ ํ•˜๊ณ  ์žˆ์—ˆ๋‹ค.
  • ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์–ด๋–ค ์†Œ์ผ“๋„ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ๋‚˜์„ ๊ฒƒ ๊ฐ™๋‹ค.

โœ…ย ๋ฌธ์ œ ํ•ด๊ฒฐ

์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์†Œ์ผ“์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค.

// ์ˆ˜์ • ์ „
const useWebRTC = (): [WebRTCState, WebRTCControls] => {
  const socket = useRef(io());
  
  ...
// ์ˆ˜์ • ํ›„
const useWebRTC = (): [WebRTCState, WebRTCControls] => {
  const socket = useRef<Socket>();
  
  ...

๐Ÿš€ ํ”„๋กœ์ ํŠธ ๊ทœ์น™

๐Ÿš€ ํ”„๋กœ์ ํŠธ ๊ธฐํš

๐Ÿ“ฝ ๋ฐ์ผ๋ฆฌ ์Šคํฌ๋Ÿผ

๐Ÿ‘ฅ ๊ทธ๋ฃน ํšŒ๊ณ 

๐Ÿ““ ๋ฉ˜ํ† ๋ง ์ผ์ง€

๐Ÿ“† ํšŒ์˜๋ก

๐Ÿง ๊ฐœ๋ฐœ์ผ์ง€

Clone this wiki locally