#2.0 SocketIO vs WebSockets
SocketIO : ์ค์๊ฐ, ์๋ฐฉํฅ, event ๊ธฐ๋ฐ์ ํต์ ์ ๊ฐ๋ฅ์ผ ํ๋ framework
WebSocket๊ณผ์ ์ฐจ์ด์ ์, webSocket์ด socketIO๊ฐ ์์ ํต์ ์ ๊ฐ๋ฅํ๊ฒ ํ๋๋ก ๋ง๋๋ ๋ฐฉ๋ฒ ์ค ํ๋์ ๋ถ๊ณผํ๋ฉฐ, ๋ง์ฝ WebSocket์ด ์ฌ์ฉ๋ชปํ๊ฒ ๋๋๋ผ๋ socketIO๋ http long-polling ๋ฑ์ ๋์ฒดํด ์ฌ์ฉํ ์ ์๋ค๊ณ ๋ฌธ์์ ๋์์๋ค. ๋น์ฅ webSocket์ protocol, socketIO๋ framework์ด๋ผ๋ ์ ์์ ๋ค๋ฅด๋ค..
https://www.npmjs.com/package/socket.io
#2.1 Installing SocketIO
SocketIO ์ค์น
npm i socket.io
๊ธฐ์กด webSocket ๋ฐฉ์ : ๋จผ์ http ์๋ฒ๋ฅผ ๋ง๋ค๊ณ , ์๋ก์ด webSocket ์๋ฒ ๋ง๋ค ๋ HTTP๋ฅผ ์์ ์์ ์ฌ๋ฆฌ๋ฉฐ ๋ง๋ค์์
= socketIO๋ ๋ง์ฐฌ๊ฐ์ง.
server.js
import { Server } from "socket.io";
// --- ์๋ต
const httpServer = http.createServer(app);
const wsServer = new Server(httpServer);
wsServer.on("connection", (socket)=>{
console.log(socket);
});
const handleListen = () => console.log(`Listening on http://localhost:3000`);
httpServer.listen(3000, handleListen);
์ฐธ๊ณ ๋ก ์ง๊ธ๊น์ง ํ๋ ๋ด์ฉ๋ค์ ์ด์ socket.io๋ก ํ๊ธฐ ๋๋ฌธ์ websocket ๊ธฐ๋ฐ์ ์ฝ๋๋ค์ ์น ์ด๊ธฐํ๋๋ค...
๊ฐ์์์๋ user๊ฐ chat์ ์ฐธ๊ฐํ๊ณ ์ถ์ ๋, room์ ๋จผ์ ๋ง๋ค๋๋ก ํ๋ ์ฝ๋๋ฅผ ์์ฑํ ๊ฒ์ด๋ผ home.pug ํ์ผ๋ ์์ ๋๋ค.
#2.2 SocketIO is Amazing
๋จผ์ ๋ฐฉ์ ๋ง๋๋ ์นํ์ด์ง๋ฅผ ๋ง๋ค์ด์ค๋ค.
home.pug
doctype html
html(lang="en")
head
meta(charset="UTF-8")
meta(http-equiv="X-UA-Compatible", content="IE=edge")
meta(name="viewport", content="width=device-width, initial-scale=1.0")
title Noom
//- mvp.css ์ฌ์ฉ
link(rel="stylesheet", href="https://unpkg.com/mvp.css")
body
header
h1 Noom
main
div#welcome
form
input(placeholder="room name", required, type="text")
button Enter Room
//- front-end์๋ socket.io ์ค์น
script(src="/socket.io/socket.io.js")
script(src="/public/js/app.js")
app.js์์๋ io() ๋ฉ์๋๋ฅผ ์ด์ฉํ๋๋ฐ, ์ด๋ ๊ธฐ์กด์ ๋ณต์กํ ์์ ๋ค์ ํ๋ฒ์ ํด๊ฒฐํด์ฃผ๋ฉฐ, ์๋์ ์ผ๋ก client์ back-end socket.io๋ฅผ ์ฐ๊ฒฐํด์ฃผ๋ ํจ์๋ค.
webSocket์์๋ send()๋ฅผ ์ผ๋๋ฐ, ์ด๋ฒ์๋ emit()์ ์ฌ์ฉํ๋ค. emit์ ์ด๋ค ์ด๋ฒคํธ๋ช ๋ ๋งจ ์ ๋งค๊ฐ๋ณ์๋ก ์ ์ด์ฃผ๋ฉด ๋๊ณ , string์ด ์๋ ๋ค๋ฅธ ์์(object)๋ค๋ ์ ์ก์ด ๊ฐ๋ฅํ๋ฉฐ ๋ฐฑ์๋์์๋ ํจ์ ์คํ์ด ๊ฐ๋ฅํ๋ค๋ ์ ์ด ์๋ค. ์ค์ ๋ก ์๋ ์ฝ๋์์๋ 'enter_room'์ด๋ผ๋ ํจ์๋ฅผ ์ด๋ฒคํธ๋ฅผ back-end์์ ์คํํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
server.js
import http from "http";
import { Server } from "socket.io";
import express from "express";
const app = express();
app.set("view engine", "pug");
app.set("views", __dirname + "/views");
app.use("/public", express.static(__dirname + "/public"));
app.get("/", (_, res) => res.render("home"));
app.get("/*", (_, res) => res.redirect("/"));
// back-end์ socket.io์ค์น
const httpServer = http.createServer(app);
const wsServer = new Server(httpServer);
wsServer.on("connection", (socket) => {
socket.on("enter_room", (msg, done) => {
console.log(msg);
setTimeout(()=>{
done("from the backend");
}, 10000);
});
});
const handleListen = () => console.log(`Listening on http://localhost:3000`);
httpServer.listen(3000, handleListen);
app.js
const socket = io();
const welcome = document.getElementById("welcome");
const form = welcome.querySelector("form");
function handleRoomSubmit(event) {
event.preventDefault();
const input = form.querySelector("input");
socket.emit("enter_room", { payload: input.value }, () =>{
console.log("server is done.");
});
input.value = "";
}
form.addEventListener("submit", handleRoomSubmit);
๋ด๊ฐ ๋นก๋๊ฐ๋ฆฌ๋ผ์ ์ ๋ฆฌํ๋๋ฐ https://ssocoit.tistory.com/192 ์ ๋์์ ์ ๋ง์ ๋ง๋ง์ด๋ฐ์๋ค....๊ฑฐ์๋ณต๋ถ์์ค์ด๋ผํ ์ ๋๋ก,,,
'javascript > ์ค ํด๋ก ์ฝ๋ฉ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DAY 2 #1.3 ~ 1.9 (0) | 2023.02.22 |
---|---|
DAY 1 #0.2 ~ #1.2 (0) | 2023.02.21 |