2016-11-22 209 views
0

你好我正在我的node.js服務器上使用websockets來建立WebRTC連接。但是,當我結束連接的節點服務器控制檯給了我以下錯誤:關閉websocket連接錯誤

 conn.otherName = null; 
        ^

TypeError: Cannot set property 'otherName' of undefined 

其他名稱是其他同行和I'ts在這樣的連接設置好的了名字: 情況下,「優惠」: // for ex。 UserA想要調用UserB console.log(「發送offer:」,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

破裂停止它與一些防禦性的代碼很簡單,改變這種

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

這個

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" 
      }); 
     } 
    } 

爲什麼用戶[connection.otherName]被返回undefined它不涉及(這是對你的鍛鍊),但它會阻止它崩潰

+0

即使這樣,仍然無法正常工作:/ –

+0

你的意思是不工作? – Mikkel

+0

無論如何,它會崩潰,問題是,當我點擊兩次發送「關閉」消息的按鈕。由於它已經關閉,它返回undefined,所以它崩潰顯示相同的錯誤日誌。 –