소켓 3

socket 튜토리얼 - emit 메소드와 emitWithAck 둘 중에 뭘 써야할까?

socket.io로 공부를 하다보면, emit과 emitWithAck 메소드를 보게된다.이 두 메소드의 기능은 서버 클라이언트 간의 데이터를 방출하는 기능으로 동일하다.다른 점이라면, emitWithAck는 emit과 다르게 Promise객체를 반환시킨다는 점이다  1. 비동기 작업 지원 간단한 socket통신을 구성할 땐 상관없지만,로직이 복잡해질 수록 비동기 처리를 효율적으로 관리할 수 있어야 한다.그런 점에서 반환된 Promise를 통해 작업을 순차적으로 혹은 병렬적으로 실행하는등 관리를 할 수 있게 되는 것이다.  2. 에러처리의 용이성 emit메소드로 에러처리 할 경우// 클라이언트socket.timeout(5000).emit("request", {foo, "bar"}, (err, respon..

소켓 2024.05.02

socket 튜토리얼 - Emitting events

메소드 참고 링크https://socket.io/docs/v4/emitting-events/ Emitting events | Socket.IOThere are several ways to send events between the server and the client.socket.io1. io객체와 socket객체를 혼동하지 말자클라이언트const socket = io();socket.on("이벤트이름", (msg) => { 로직 } 서버const io = new Server(server)...중략...io.on("connection", (socket) => { socket.on("이벤트이름", (msg) => { io.emit("chatMsg", msg) })}) io객체는 n..

소켓 2024.05.02

socket 튜토리얼 - STEP1 부터 STEP5

소켓통신 공부를 위해 사이드 프로젝트를 하기 전문서에 나와있는 간단한 socket 튜토리얼을 진행했다.https://socket.io/docs/v4/tutorial/introduction Tutorial - Introduction | Socket.IOWelcome to the Socket.IO tutorial!socket.io Step2 : Serving Htmlstep1에서 서버객체 생성 후, localhost:3000에 접속 시 "Hello Wolrd" 워딩을 띄울 수 있게끔 res.send 안에 html코드를 string 값으로 전송했다.그러나 route와 html태그가 index.js 파일에 함께 있는 것은 혼동을 줄 수 있기 때문에 step2에선 화면을 그리는 파일과 route파일을 분리하는 작..

소켓 2024.05.01