๐Ÿ“Œ +) WebSocket ์œผ๋กœ ์ฑ„ํŒ…๊ธฐ๋Šฅ ๊ตฌํ˜„ ํ•ด๋ณด๊ธฐ

seul_velogยท2022๋…„ 10์›” 4์ผ
2

โœ๏ธ ํ”„๋กœ์ ํŠธ ์ฑ„ํŒ… ๊ธฐ๋Šฅ ๊ตฌํ˜„์„ ์œ„ํ•ด zoom ํด๋ก ์ฝ”๋”ฉ ์‹ค์Šต(๋ฐ”๋‹๋ผ JS, websocket)์„ ํ•ด๋ณด๋ฉด์„œ ๊ธฐ๋กํ•œ ๋‚ด์šฉ์ด๋‹ค. (1)

SETTING

  • ์„œ๋ฒ„๋‹จ ์„ธํŒ…ํ•˜๊ธฐ
    NodeJS ์†์„ฑ ์„œ๋ฒ„ ์„ค์ • ๋ฐ ์„ค์น˜
  • babel, Nodemon, Express๋ฅผ ํ™œ์šฉํ•ด์„œ NodeJS ํ”„๋กœ์ ํŠธ ์„ค์ •ํ•˜๊ธฐ
    npm init -y
    npm i nodemon -D
    npm i @babel/core @babel/cli @babel/node -D
    npm i @babel/preset-env -D
    npm i express
    npm i pug
  • pug๋กœ view engine ์„ค์ •
  • Express์— template๊ฐ€ ์–ด๋”” ์žˆ๋Š”์ง€ ์„ค์ •
  • public url์„ ์ƒ์„ฑํ•ด์„œ ์œ ์ €์—๊ฒŒ ํŒŒ์ผ์„ ๊ณต์œ 
  • home.pug๋ฅผ render ํ•ด์ฃผ๋Š” route handler ๋งŒ๋“ฆ

๐Ÿ“Œ nodemon
ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ดํŽด๋ณด๊ณ  ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์žˆ์„ ์‹œ ์„œ๋ฒ„๋ฅผ ์žฌ์‹œ์ž‘ํ•ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ

// nodemon.json

{
  "ignore": ["src/public/*"],
  "exec": "babel-node src/server.js"
}
  • ์„œ๋ฒ„๋ฅผ ์žฌ์‹œ์ž‘ ํ•˜๋Š” ๋Œ€์‹  babel node๋ฅผ ์‹คํ–‰ํ•˜๋Š”๋ฐ, ๋ฐ”๋ฒจ์€ ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ์ผ๋ฐ˜ nodeJS ์ฝ”๋“œ๋กœ ์ปดํŒŒ์ผ ํ•œ๋‹ค. ๊ทธ ์ž‘์—…์„ src/server.js ํŒŒ์ผ์— ํ•ด์ค€๋‹ค.

๐Ÿ“Œ server.js
BackEnd์—์„œ ๊ตฌ๋™๋จ

import express, { application } from 'express'; // express๋ฅผ import

const app = express(); // express ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์„ฑ

app.set('view engine', 'pug'); // ์—ฌ๊ธฐ์— view engine์„ Pug๋กœ ์„ค์ •,
app.set('views', __dirname + '/public/views'); // views ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์„ค์ •
app.use('/public', express.static(__dirname + '/public')); // 1)

app.get('/', (req, res) => res.render('home'));
app.get('/*', (req, res) => res.redirect('/')); // ์œ ์ €๊ฐ€ ์–ด๋Š url์„ ์ž…๋ ฅํ•ด๋„ ํ™ˆ์œผ๋กœ

const handleListen = () => console.log('hello');
app.listen(3000, handleListen);

const server = http.createServer(app); // http ์„œ๋ฒ„
const wss = new WebSocket.Server({server}) // WebSocket ์„œ๋ฒ„
  • 1) publicํŒŒ์ผ๋“ค์— ๋Œ€ํ•ด์„œ๋„ ๋˜‘๊ฐ™์€ ์ž‘์—…, 1) ์ค„์ด public ํด๋”๋ฅผ ์œ ์ €์—๊ฒŒ ๊ณต๊ฐœํ•ด ์ฃผ๋Š” ๊ฒƒ
  • ์„œ๋ฒ„๋Š” http, ws 2๊ฐœ์˜ protocol ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.
  • public : FrontEnd์—์„œ ๊ตฌ๋™๋˜๋Š” ์ฝ”๋“œ (public > js > FrontEnd์—์„œ ๊ตฌ๋™)





WEBSOCKETS

์„ค์น˜์ค€๋น„

  • npm i ws โ†’ ws ์„ค์น˜ํ•˜๊ธฐ

WebSocket Envents

connection

const handleConnection = (socket) => {
  console.log(socket); // *)
};
wss.on('connection', handleConnection);

// *) ์ด์ƒํƒœ์—์„œ๋Š” ์•„๋ฌด ์—ฐ๊ฒฐ์ด ์—†์œผ๋ฏ€๋กœ ์•„๋ฌด์ผ์ด ์ผ์–ด๋‚˜์ง€ ์•Š์Œ. backend์™€ ์—ฐ๊ฒฐ ํ•„์š”

1) on method์—์„œ๋Š” envent๊ฐ€ ๋ฐœ๋™ํ•˜๋Š” ๊ฒƒ์„ ๊ธฐ๋‹ค๋ฆฐ๋‹ค. (์œ„ ๊ฒฝ์šฐ connection ์ด๋ฒคํŠธ)
2) ๊ทธ๋ฆฌ๊ณ  function ์„ ๋ฐ›๋Š”๋ฐ connection์ด ์ด๋ฃจ์–ด์ง€๋ฉด ์ž‘๋™ํ•œ๋‹ค.
3) ๋˜ on method๋Š” backend์— ์—ฐ๊ฒฐ๋œ ์‚ฌ๋žŒ์˜ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด์ค€๋‹ค. (๊ทธ๊ฒŒ socket์—์„œ ์˜จ๋‹ค.)



on

  • on method์˜ ์„ค๋ช…์„ ๋ณด๋ฉด callback์œผ๋กœ socket์„ ๋ฐ›๋Š”๋‹ค.
  • socket โ†’ ์—ฐ๊ฒฐ๋œ ๋ธŒ๋ผ์šฐ์ €์™€์˜ contact(์—ฐ๋ฝ)๋ผ์ธ. socket์„ ์ด์šฉํ•˜๋ฉด ๋ฉ”์„ธ์ง€ ์ฃผ๊ณ ๋ฐ›๊ธฐ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๐Ÿ˜€


frontend ์—์„œ backend๋กœ ์—ฐ๊ฒฐํ•˜๊ธฐ
const ์†Œ์ผ“ = new WebSocket(url, protocols)



app.js ์—์„œ ์—ฐ๊ฒฐํ•ด๋ณด๊ธฐ

โœ๏ธ webSocket์€ ๋ธŒ๋ผ์šฐ์ €์™€ ์„œ๋ฒ„์‚ฌ์ด์˜ ์—ฐ๊ฒฐ์ด๋‹ค! ๐ŸŒŸ

const socket = new WebSocket(`ws://${window.location.host}`);



server.js์™€ app.js์˜ socket

1) ์—ฐ๊ฒฐ๋œ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ๋œปํ•œ๋‹ค.

// server.js
// ์ด socket์ด frontend์™€ real-time ์œผ๋กœ ์†Œํ†ตํ•  ์ˆ˜ ์žˆ๋‹ค.
const handleConnection = (socket) => {
  console.log(socket); 
};

2) ์„œ๋ฒ„๋กœ์˜ ์—ฐ๊ฒฐ์„ ๋œปํ•œ๋‹ค.

// app.js
// ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ frontend์—์„œ backend๋กœ ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
const socket = new WebSocket(`ws://${window.location.host}`);



messages ์ฃผ๊ณ ๋ฐ›๊ธฐ

1) ์—ฐ๊ฒฐ์„ ํ•ด์„œ socket์„ ์•Œ๊ธฐ
2) ๋ธŒ๋ผ์šฐ์ €์— ๋ฉ”์„ธ์ง€ ๋ณด๋‚ด๋ณด๊ธฐ
โ†’ socket์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ! (wss์„œ๋ฒ„์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹˜)

// server.js
wss.on('connection', (socket) => { // connection์ด ์ƒ๊ฒผ์„ ๋•Œ socket์œผ๋กœ ์ฆ‰์‹œ ๋ฉ”์„ธ์ง€('hi!!!')๋ฅผ ๋ณด๋‚ด๊ธฐ
  console.log('Connected to Browser ๐ŸŒŸ');
  socket.send('hi!!!');
});

3) ํ”„๋ก ํŠธ์—์„œ ๋ฉ”์„ธ์ง€ ๋ฐ›๊ธฐ & socket์— ์ด๋ฒคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด๊ธฐ

// app.js
const socket = new WebSocket(`ws://${window.location.host}`<);

socket.addEventListener('open', () => { // connection์ด open๋˜๋ฉด ์•„๋ž˜ ์ฝ˜์†” ์ถœ๋ ฅ
  console.log('Connected to Server ๐Ÿ’ซ');
});

socket.addEventListener('message', (message) => { // ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๋Š” message
  console.log('Just got this: ', message, 'from the server');
	// message.data -> hi!!!
});

socket.addEventListener('close', () => { // ์„œ๋ฒ„๊ฐ€ ์˜คํ”„๋ผ์ธ์ด ๋ ๋•Œ ์•„๋ž˜ ์ฝ˜์†” ์ถœ๋ ฅ
  console.log('Disconnect from Server ๐Ÿ”ฅ');
});

4) ํ”„๋ก ํŠธ์—์„œ ๋ฉ”์„ธ์ง€ ๋ณด๋‚ด๊ธฐ

// app.js
setTimeout(() => {
  socket.send('๋ธŒ๋ผ์šฐ์ €์—์„œ ๋ฉ”์„ธ์ง€ ๋ณด๋‚ด๊ธฐ!');
}, 5000);



Chat) ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  socket๋“ค์—๊ฒŒ ์ ‘๊ทผํ•˜๊ธฐ

UI

pug๋กœ UI ์ž‘์„ฑ

// pug ๋กœ UI ์ž‘์„ฑ
body 
  header
    h1 seul
    main 
    h2 Welcome to seul
      ul
      form
        input(type="text", placeholder="๋ฉ”์„ธ์ง€", required)
        button Send

    script(src="/public/js/app.js")
// app.js
const messageList = document.querySelector('ul');
const messageForm = document.querySelector('form');

function handleSubmit(e) {
  e.preventDefault();
  const input = messageForm.querySelector('input');
  socket.send(input.value); // ๋ฐฑ์—”๋“œ๋กœ ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด๋ƒ„
  input.value = '';
}
messageForm.addEventListener('submit', handleSubmit);

์—ฐ๊ฒฐ๋œ ๋ชจ๋“  socket๋“ค์—๊ฒŒ ์ ‘๊ทผํ•˜๊ธฐ

  • firefox๊ฐ€ ์—ฐ๊ฒฐ๋  ๋•Œ, firefox์˜ socket์„ ๊ฐ€์ ธ์™€์„œ sockets์˜ ๋ฐฐ์—ด์— ๋„ฃ์–ด์ค€๋‹ค.
  • ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ brave๊ฐ€ ์—ฐ๊ฒฐ๋  ๋•Œ๋Š” brave์˜ socket์„ array์— ๋„ฃ์–ด์ค€๋‹ค.
    โ†’ ex) brave์—์„œ ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐ›์œผ๋ฉด sockets์— ์žˆ๋Š” ๋ชจ๋“  ๊ณณ์— ์ „๋‹ฌํ•ด์ค„ ์ˆ˜ ์žˆ๊ฒŒ๋Œ
const sockets = [];

wss.on('connection', (socket) => {
  sockets.push(socket);
...



Chat) ๋‹‰๋„ค์ž„ ๋ฐ›๊ธฐ

ํƒ€์ž… ๋‚˜๋ˆ„๊ธฐ

  • ์„œ๋ฒ„๋‹จ ๋ฉ”์„ธ์ง€๋Š” ํ•ญ์ƒ ๋ชจ๋“  ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐ›์œผ๋ฏ€๋กœ ๋‹‰๋„ค์ž„๊ณผ ๋ฉ”์„ธ์ง€๋ฅผ type ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์ค„ ํ•„์š”๊ฐ€ ์žˆ๋‹ค.
// ์ด๋Ÿฐ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ์œ„ํ•ด!!
{
  type: 'message';
  payload: 'ํ—ฌ๋กœ';
}

{
  type: 'nickname';
  payload: 'seul';
}


function handleNickSubmit(e) {
  e.preventDefault();
  const input = nickForm.querySelector('input');
  socket.send({
    type: 'nickname',
    payload: input.value,
  });
  input.value = '';
}
  • ์œ„ obj๋ฅผ ๊ฐ€์ ธ์™€์„œ string์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ โ†’ string์œผ๋กœ ๋ฐ”๋€ obj๋Š” ๋ฐฑ์—”๋“œ๋กœ ์ „์†ก๋จ โ†’ ๋ฐฑ์—”๋“œ์—์„œ ๋‹ค์‹œ string์„ obj๋กœ ๋ฐ”๊ฟˆ

back-end๋กœ string ๋ณด๋‚ด์ฃผ๊ธฐ

  • obj โ†’ string ๋ฐ”๊ฟ”์ฃผ๊ธฐ : JS๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋„ ํ™œ์šฉ๊ฐ€๋Šฅํ•˜๋„๋ก string์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋„๋ก!
    โ†’ websocket์ด ๋ธŒ๋ผ์šฐ์ €์— ์žˆ๋Š” API์ด๋ฏ€๋กœ, ๋ฐฑ์—”๋“œ์—์„œ๋Š” ๋‹ค์–‘ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด API๋Š” ์–ด๋– ํ•œ ํŒ๋‹จ๋„ ํ•˜๋ฉด ์•ˆ๋œ๋‹ค. ์ฆ‰ API๊ฐ€ ์–ด๋–ค ๊ฒฐ์ •๋„ํ•˜๋ฉด ์•ˆ๋จ, ๋ฐฑ์—”๋“œ์—์„œ๋Š” string์„ ๊ฐ€์ง€๊ณ  ํ•˜๊ณ  ์‹ถ์€ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๊ธฐ.
const messageList = document.querySelector('ul');
const nickForm = document.querySelector('#nick');
const messageForm = document.querySelector('#message');
const socket = new WebSocket(`ws://${window.location.host}`);

function makeMessage(type, payload) {
  const msg = { type, payload };
  return JSON.stringify(msg);
}

...

function handleSubmit(e) {
  e.preventDefault();
  const input = messageForm.querySelector('input');
  socket.send(makeMessage('์ƒˆ๋กœ์šด ๋ฉ”์„ธ์ง€', input.value));
  input.value = '';
}

function handleNickSubmit(e) {
  e.preventDefault();
  const input = nickForm.querySelector('input');
  socket.send(makeMessage('๋‹‰๋„ค์ž„', input.value));
  input.value = '';
}

messageForm.addEventListener('submit', handleSubmit);
nickForm.addEventListener('submit', handleNickSubmit);


ํƒ€์ž… ๋ถ„๊ธฐ

  • ํƒ€์ž…์ด '์ƒˆ๋กœ์šด ๋ฉ”์„ธ์ง€' ๋ผ๋ฉด sockets ๋ฐฐ์—ด์˜ ์†Œ์ผ“๋“ค์„ ๋ณด๋‚ด์„œ ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด๋‚ด๊ธฐ
socket.on('message', (message) => {
    const parsed = JSON.parse(message.toString());
    if (parsed.type === '์ƒˆ๋กœ์šด ๋ฉ”์„ธ์ง€') {
      sockets.forEach((aSocket) => aSocket.send(parsed.payload));
    }
  });

swicth ๋ฌธ ํ™œ์šฉํ•˜์—ฌ ๋ถ„๊ธฐ ๋ฆฌํŒฉํ† ๋ง

wss.on('connection', (socket) => {
  sockets.push(socket);
  console.log('ํด๋ผ์ด์–ธํŠธ / ๋ธŒ๋ผ์šฐ์ €์™€ ์—ฐ๊ฒฐ ๐ŸŒŸ');
  socket.send('hi!!!');
  socket.on('close', () =>
    console.log('ํด๋ผ์ด์–ธํŠธ / ๋ธŒ๋ผ์šฐ์ €๋กœ๋ถ€ํ„ฐ ์—ฐ๊ฒฐ์ด ๋Š๊น€๐Ÿ”ฅ')
  );
  socket.on('message', (msg) => {
    const message = JSON.parse(msg.toString());
    switch (message.type) {
      case '์ƒˆ๋กœ์šด ๋ฉ”์„ธ์ง€': {
        sockets.forEach((aSocket) => aSocket.send(message.payload));
      }
      case '๋‹‰๋„ค์ž„': {
        console.log(message.payload);
      }
    }
  });
});

๋‹‰๋„ค์ž„์„ ์†Œ์ผ“ ์•ˆ์— ๋„ฃ๊ธฐ

  • ์†Œ์ผ“์ด ๋ˆ„๊ตฌ์ธ์ง€ ์•Œ์•„์•ผ ํ•˜๋ฏ€๋กœ !
  • ์†Œ์ผ“์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ์ฒด์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

๋งŒ์•ฝ type์ด ๋‹‰๋„ค์ž„์ธ ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐ›์œผ๋ฉด ๋‹‰๋„ค์ž„์„ socket์— ๋„ฃ์–ด์ค€๋‹ค. (๋‹‰๋„ค์ž„์ด๋“  ์ด๋ฉ”์ผ์ด๋“  ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.)

wss.on('connection', (socket) => {
  sockets.push(socket);
  socket['๋‹‰๋„ค์ž„'] = '์—†์Œ'
  console.log('ํด๋ผ์ด์–ธํŠธ / ๋ธŒ๋ผ์šฐ์ €์™€ ์—ฐ๊ฒฐ ๐ŸŒŸ');
  socket.send('hi!!!');
  socket.on('close', () =>
    console.log('ํด๋ผ์ด์–ธํŠธ / ๋ธŒ๋ผ์šฐ์ €๋กœ๋ถ€ํ„ฐ ์—ฐ๊ฒฐ์ด ๋Š๊น€๐Ÿ”ฅ')
  );
  socket.on('message', (msg) => {
    const message = JSON.parse(msg.toString());
    switch (message.type) {
      case '์ƒˆ๋กœ์šด ๋ฉ”์„ธ์ง€': {
        sockets.forEach((aSocket) => aSocket.send(message.payload));
      }
	  break;  // ๋ถ™์ด์ง€ ์•Š์œผ๋ฉด ์—ฌ๋Ÿฌ๋ฒˆ ๋ฉ”์„ธ์ง€๋ฅผ ์ž…๋ ฅํ•  ๋•Œ๋งˆ๋‹ค ๋‹‰๋„ค์ž„์ด ๋ฐ”๋€œ
      case '๋‹‰๋„ค์ž„': {
        console.log(message.payload);
        socket['๋‹‰๋„ค์ž„'] = message.payload; // ๋ฐ›์€ ๋‹‰๋„ค์ž„์„ ๋‹‰๋„ค์ž„์†Œ์ผ“์— ๋„ฃ์–ด์ฃผ๊ณ  ์žˆ๋Š”์ค‘
      }
	  break;
    }
  });
});

๐Ÿ“Œ ํŽธ์˜์„ฑ ๊ฐœ์„ ํ•˜๊ธฐ (๋ฌธ์ œ์ )

1) ๋‚˜๋ฅผ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ๋ชจ๋‘์—๊ฒŒ๋งŒ ๋ฉ”์„ธ์ง€๋ฅผ ์ „์†กํ•˜๊ธฐ
์ง€๊ธˆ์€ ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด๋‚ผ ๋•Œ ๋ฉ”์„ธ์ง€๊ฐ€ ์„œ๋ฒ„๋กœ ๊ฐ€๊ณ , ๋ฉ”์„ธ์ง€ ํƒ€์ž…์— ๋”ฐ๋ผ์„œ ์ž์‹ ๊ณผ ๋‹ค๋ฅธ ๋ธŒ๋ผ์šฐ์ €์— ์ „์†กํ•˜๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋งŒ์•ฝ brave๊ฐ€ ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด๋‚ด๋ฉด brave์—๋„ ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด๋‚ด์ฃผ๊ณ  ์žˆ๋‹ค. โ†’ ๊ฐ™์€ ๋ฉ”์„ธ์ง€๋ฅผ ๋Œ๋ ค์ฃผ๋Š” ์ค‘

2) ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๋ฉ”์„ธ์ง€ ์ปจํŠธ๋กค
๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋ฐฑ์—”๋“œ์—๊ฒŒ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๋ฉ”์„ธ์ง€๋ฅผ ์ฃผ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ (๋‹‰๋„ค์ž„, ์ƒˆ๋กœ์šด๋ฉ”์„ธ์ง€) ๋ฐฑ์—”๋“œ๋„ ํ”„๋ก ํŠธ์—๊ฒŒ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๋ฉ”์„ธ์ง€๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋‹ค. (์ฑ„ํŒ…๋ฐฉ ์ž…์žฅํ•œ ์‚ฌ๋žŒ, ์ฑ„ํŒ…๋ฐฉ ๋‚˜๊ฐ„์‚ฌ๋žŒ ๋“ฑ)

โ†’ ๋‹ค์‹œ๋งํ•ด์„œ, (ํ˜„์žฌ ๋ฐฉ์‹์˜ ๋ฐ˜๋Œ€์ฒ˜๋Ÿผ) ๋ฐฑ์—์„œ stringify๋ฅผ ํ•ด์ฃผ๊ณ , ๋„˜๊ธด ๋ฐ์ดํ„ฐ๋ฅผ ํ”„๋ก ํŠธ์—์„œ ๋‹ค์‹œ parse๋ฅผ ํ•ด์ค˜์•ผํ•  ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ๐Ÿง

โœ๏ธ ์ง€๊ธˆ์€ ๋ชจ๋“  ๊ฒƒ๋“ค์ด socket message๋กœ ๋“ค์–ด๊ฐ€์•ผ ๋ฉ”์„ธ์ง€ ํƒ€์ž…์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์†Œ์ผ“์•„์ด์˜ค ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์“ฐ๋ฉด socket.on(โ€™nicknameโ€™, fn) ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!!! ๐ŸŒŸ

์ด๋Ÿฌํ•œ ์ ๋“ค์„ Socket.IO ๋ฅผ ํ†ตํ•ด์„œ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.
wss์—๋Š” ์ œ์ผ ๊ธฐ์ดˆ์ ์ธ ๊ฒƒ๋“ค์ด ์žˆ๋‹ค. ์˜ค๋กœ์ง€ web socket specification์— ๋งž์ถฐ protocol์„ ์‹คํ–‰์‹œํ‚ฌ ๋ฟ์ด๋‹ค. ๋ถ€์ˆ˜์ ์ธ ๊ธฐ๋Šฅ๋„ ์—†๋‹ค๊ณ  ํ•œ๋‹ค.


ํ”„๋กœ์ ํŠธ ์ฑ„ํŒ… ๊ธฐ๋Šฅ ๊ตฌํ˜„์— ์•ž์„œ Websocket๊ณผ Socket.IO ๊ฐœ๋… ์•Œ๊ณ  ์ตํžˆ๊ธฐ. ์‹ค์Šต ํ”„๋กœ์ ํŠธ !
1. JavaScript + Websocket ๐Ÿ‘‰ WebSocket ์œผ๋กœ ์ฑ„ํŒ…๊ธฐ๋Šฅ ๊ตฌํ˜„ ํ•ด๋ณด๊ธฐ
2. JavaScript + Socket.IO ๐Ÿ‘‰ Socket.IO๋กœ ์ฑ„ํŒ…๊ธฐ๋Šฅ ๊ตฌํ˜„ ํ•ด๋ณด๊ธฐ
3. React + Socket.IO ๐Ÿ‘‰ Socket.IO๋กœ ์ฑ„ํŒ…๊ธฐ๋Šฅ ๊ตฌํ˜„ ํ•ด๋ณด๊ธฐ
โœจ with-dog ํ”„๋กœ์ ํŠธ ์ฑ„ํŒ… ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๐Ÿ‘‰ ์ฑ„ํŒ…๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

profile
๊ธฐ์–ต๋ณด๋‹จ ๊ธฐ๋ก์„ โœจ

0๊ฐœ์˜ ๋Œ“๊ธ€