WebSocket 2

[JavaScript] WebSocket vs Socket IO

Socket IO ์‹ค์‹œ๊ฐ„, ์–‘๋ฐฉํ–ฅ, event ๊ธฐ๋ฐ˜์˜ ํ†ต์‹ ์„ ํ•˜๋Š” framework webSocket์€ ์œ„์˜ ํ†ต์‹ ์„ ์ œ๊ณตํ•˜๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜ Socket IO๋Š” webSocket์˜ ๋ถ€๊ฐ€๊ธฐ๋Šฅ (X) Socket IO๋Š” ๊ฐ€๋” webSocket์„ ์ด์šฉ (O) ๋งŒ์•ฝ webSocket ์ด์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด, Socket IO๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•ด์„œ ๊ณ„์† ์ž‘๋™ ์ž๋™ ์žฌ์—ฐ๊ฒฐ ์ง€์›, ์—ฐ๊ฒฐ ๋Š๊น€ ํ™•์ธ, ๋ฐ”์ด๋„ˆ๋ฆฌ ์ง€์› ํƒ„๋ ฅ์„ฑ, ์‹ ๋ขฐ์„ฑ 1. get socket server WebSocket //server.js const httpServer = http.createServer(app); const wsServer = new ws. WebSocketServer({httpServer}); Socket IO ์œ ์ €์—๊ฒŒ URL ์ œ๊ณต..

JavaScript 2024.03.29

[JavaScript] WebSocket | ์„œ๋ฒ„์™€ ๋ธŒ๋ผ์šฐ์ € ์—ฐ๊ฒฐ

1. express ๋กœ ์„œ๋ฒ„ ๊ตฌ์ถ• express ? node.js์˜ ํ”„๋ ˆ์ž„์›Œํฌ ์„œ๋ฒ„ ์‚ฌ์ด๋“œ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ Node.js์˜ API๋ฅผ ๋‹จ์ˆœํ™”ํ•˜๊ณ , ์‰ฝ๊ฒŒ ์„œ๋ฒ„ ๊ตฌ์ถ• ๊ฐ€๋Šฅ express application์œผ๋กœ๋ถ€ํ„ฐ http server ์ƒ์„ฑ import express from 'express'; import http from 'http'; const app = express(); const server = http.createServer(app); 2. http ์„œ๋ฒ„ ์œ„์—, webSocket ์„œ๋ฒ„ ๊ตฌ์ถ• -> ๊ฐ™์€ ํฌํŠธ ์ด์šฉ import ws from 'ws'; //http ์„œ๋ฒ„ ์ „๋‹ฌ const wss = new we.WebSocketServer({ server }); 3. WebSocket Connection :..

JavaScript 2024.03.28