2017년 5월 31일 수요일

[nodeJS]노드JS 웹소켓 클라이언트 구현.

어제는 자바로 웹소켓 클라이언트를 구현해 보았다.

오늘은 nodeJS 에서 웹소켓 서버를 구현해보겟다.

일단 nodejs websocket 모듈을 설치해야한다.

윈도우의 경우 모듈을 다운 받아서 node_modules 폴더에 넣으면 되고

리눅스의 경우 node_modules 디렉토리에서 npm 으로 다운 받으면된다.


var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();



client.on('connect', function(clientConnection) {
    //연결확인
    console.log('WebSocket Client Connected');
   
    //에러확인
    clientConnection.on('error', function(error) {

        console.log("clientConnection Error: " + error.toString());

    });

    //연결종료확인
    clientConnection.on('close', function() {

        console.log('echo-protocol clientConnection Closed');

    });

    //메세지 수신
    clientConnection.on('message', function(message) {

        if (message.type === 'utf8') {
            console.log("Received: '" + message.utf8Data + "'");
        }
    });
   
    //데이터 전송 함수.
    function send(data) {

        if (clientConnection.connected) {

            clientConnection.sendUTF(JSON.stringify(data));
        }
    }

    var sendDataObj = {
       "value" : 1
    }
    //연결되면 메세지 보내기
    send(sendDataObj);
});

//프로토콜 설정은 알아서 찾아보길.
client.connect('ws://웹소켓도메인','프로토콜 설정');

더이상의 자세한 설명은 생략한다.


댓글 없음:

댓글 쓰기

[lunux]리눅스 폴더별 용량 확인

리눅스 폴더별 용량 확인 조회 하고자 하는 디렉토리 리스트가있는 경로로 이동후 du -h --max-depth=1