2016-11-22 8 views
0

こんにちは私はWebRTC接続を確立するために、node.jsサーバーでwebsocketを使用しています。私は、接続を終了する場合でも、ノードサーバコンソールには、私に次のエラーを与える:WebSocket接続エラーを閉じる

 conn.otherName = null; 
        ^

TypeError: Cannot set property 'otherName' of undefined 

その他の名称は、他のピアの名前であり、I'tsは、このような接続で、最大setted: ケース「のオファーを」: //例: UserAがUserBに電話をかけたいとします。 console.log( "送信先を:"、data.name);

 //if UserB exists then send him offer details 
     var conn = users[data.name]; 

     if(conn != null) { 
      //setting that UserA connected with UserB 
      connection.otherName = data.name; 

      sendTo(conn, { 
       type: "offer", 
       offer: data.offer, 
       name: connection.name 
      }); 
     } 

     break; 

    case "answer": 
     console.log("Sending answer to: ", data.name); 
     //for ex. UserB answers UserA 
     var conn = users[data.name]; 

     if(conn != null) { 
      connection.otherName = data.name; 
      sendTo(conn, { 
       type: "answer", 
       answer: data.answer 
      }); 
     } 

その後私はこのような接続を閉じています:

connection.on("close", function() { 

    if(connection.name) { 
    delete users[connection.name]; 

    if(connection.otherName) { 
     console.log("Disconnecting from ", connection.otherName); 
     var conn = users[connection.otherName]; 
     conn.otherName = null; 

     if(conn != null) { 
      sendTo(conn, { 
       type: "leave" 
      }); 
     } 
    } 
    } 
}); 

は、どのように私は私のノードサーバをクラッシュすることなく、接続を閉じることができるように変更できますか?破壊からそれを停止

答えて

0

は、いくつかの防御的なコードと非常に簡単です、ユーザーが[connection.otherName](undefinedを返している理由は対応していないこの

if (connection.otherName) { 
    var conn = users[connection.otherName]; 
    if (conn) 
     conn.otherName = null; 

    if(conn != null) { 
// The connection is closed, trying to send at this point isn't a good idea 
      sendTo(conn, { 
       type: "leave" 
      }); 
     } 
    } 

にこの

var conn = users[connection.otherName]; 
conn.otherName = null; 

を変更それはあなたのための運動です)、それはクラッシュを停止します

+0

まだそれでも動作しません:/ –

+0

あなたは働いていないという意味ですか? – Mikkel

+0

とにかくクラッシュします。問題は、「閉じる」メッセージを送信するボタンを2回クリックしたときです。既に閉じているので、undefinedを返して、同じエラーログを表示してクラッシュします。 –

関連する問題