nomomo / Twip-Toonation-Afreehp-Parser-Example

Nodejs example for parsing donation notifications from Afreeca helper, Twip, and Toonation
MIT License
6 stars 4 forks source link

connection이 끊겨도 죽은 connection에 계속 ping보냄. #2

Closed jamesleegit closed 2 years ago

jamesleegit commented 2 years ago
toonAtClient.on('connect', function(connection) {
      console.log('Toonat Connected');

      setInterval(function(){
          connection.ping("#ping");
      }, 12000);

이런소스는 좋지않습니다. connection이 끊어졌을 때도 계속 타이머 돌면서 연결끊킨 connection에 ping 을 보내기 때문입니다. connection이 정상상태가 아니라면 저 루프에서 빠져나오는 release 로직까지 짜두어야합니다.

jamesleegit commented 2 years ago
client.on('connect', function(connection) {
    let isClose = false;
    const heartbeat = () => {
        if (isClose) {
            return;
        }
        connection.ping("#ping");
        setTimeout(()=>heartbeat(), 3000);
    }
    heartbeat();

    connection.on('error', function(error) {
        isClose = true;
    });
    connection.on('close', function() {
        isClose = true;
        setTimeout(function(){
            createConnection();
        }, 3000);
    });
jamesleegit commented 2 years ago

솔루션

nomomo commented 2 years ago

네 감사합니다. 해당 부분은 수정해두도록 하겠습니다.