socket.io 예제 따라하기

socket.io 예제 따라하기 socket 통신을 nodejs로 구현한 내용이다. 한번 정리해보기로 한다. 1. 채팅 소스 공식 사이트에 있는 예제 소스이다. https://socket.io/get-started/chat 간단한 채팅 소스이다. git을 통해 예제를 받아 볼수 있다. git clone https://github.com/socketio/chat-example.git 딱 채팅만 동작하는 소스이다. 실행 방법은 1. git으로 당기고 git clone https://github.com/socketio/chat-example.git 2. 해당 폴더로 이동하고 cd chat-example/ 3. 초기화 하고 yarn 4. 실행한다. yarn start 5. 브라우저 2개를 열어서 위의 이미지와 같이 메시지를 입력하면 끝이다. 소스를 확인해 보겠다. 보아야 할 소스는 2개 밖에 없다. index.js 와 index.html 2개이다. 먼저 index.js 를 보면 const app = require ( ' express ' )() ; const http = require ( ' http ' ) . Server ( app ) ; const io = require ( ' socket.io ' )( http ) ; const port = process . env . PORT || 3000 ; console . log ( process . env . PORT ) app . get ( ' / ' , ( req , res ) => { res . sendFile ( __dirname + ' /index.html ' ) ; } ) ; io ...