아하
검색 이미지
생활꿀팁 이미지
생활꿀팁생활
생활꿀팁 이미지
생활꿀팁생활
부유한할미새273
부유한할미새27321.04.04

파이썬 웹소켓 궁금한 내용이 있습니다

웹소켓 구현에 대해서 궁금한게 있습니다

var socket = 0; function connect() { if (socket != 0 && socket.readyState != 1) return; socket = new WebSocket('ws://localhost:9999'); socket.onopen = function() { socketOpened(); }; socket.onerror = function(error) { console.log('socket error'); console.log(error); printMessage('connection error'); }; socket.onmessage = function(event) { handleMessage(event.data); }; socket.onclose = function() { socketClosed(); }; }

아래 코드가 어떤기능을 하는지 궁금합니다

55글자 더 채워주세요.
답변의 개수1개의 답변이 있어요!
  • 안녕하세요

    코드에 인라인으로 주석을 달았습니다.

    var socket = 0; function connect() { if (socket != 0 && socket.readyState != 1) return; //소켓이 이미 연결되어 있으면 리턴 socket = new WebSocket('ws://localhost:9999'); //자신의 PC(localhost)에 실행중인 web server의 port 9999번에 연결을 시도 //아래의 것들은 해당 event가 발생하는 경우의 handler입니다. socket.onopen = function() { socketOpened(); }; //소켓이 연결된 경우 socketOepnde 호출 socket.onerror = function(error) { console.log('socket error'); console.log(error); printMessage('connection error'); }; //소켓 에러 발생 시 에러 출력 socket.onmessage = function(event) { handleMessage(event.data); }; //서버에서 소켓을 통해서 메시지 도착시에 해당 메시지를 처리, 도착한 내용은 event.data를 통해서 전달됨 socket.onclose = function() { socketClosed(); }; //소켓이 닫힌 경우에 호출 }