코드 6- 7 Distributor 접속 기능

    예제 파일 : server.js

    ......
        onClose(socket) {
            console.log("onClose", socket.remoteAddress, socket.remotePort);
        }
            
    connectToDistributor(host, port, onNoti) {    // ➊ Distributor 접속 함수
        var packet = {                            // ➋ Distributor에 전달할 패킷 정의
            uri: "/distributes",
            method: "POST",
            key: 0,
            params: this.context
        };
        var isConnectedDistributor = false;       // ➌ Distributor 접속 상태
            
        this.clientDistributor = new tcpClient(   // ➍ Client 클래스 인스턴스 생성
            host
            , port
            , (options) => {                      // ➎ 접속 이벤트
                isConnectedDistributor = true;
                this.clientDistributor.write(packet);
            }
            , (options, data) => { onNoti(data); }             // ➏ 데이터 수신 이벤트
            , (options) => { isConnectedDistributor = false; } // ➐ 접속 종료 이벤트
            , (options) => { isConnectedDistributor = false; } // ➑ 에러 이벤트
            );
    
            setInterval(() => {                       // ➒ 주기적인 Distributor 접속 시도
                if (isConnectedDistributor /= true) {
                    this.clientDistributor.connect();
                }
            }, 3000);
        }
    }
    
    module.exports = tcpServer;
    
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.