【发布时间】:2016-11-30 03:23:05
【问题描述】:
您好,我在我的 node.js 服务器上使用 websockets 来建立 WebRTC 连接。但是当我结束连接时,节点服务器控制台给了我以下错误:
conn.otherName = null;
^
TypeError: Cannot set property 'otherName' of undefined
Other name 是另一个 Peer 的名称,我在连接中设置如下: 案例“报价”: //例如。用户A想呼叫用户B 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"
});
}
}
}
});
如何更改它以关闭连接而不会导致我的节点服务器崩溃?
【问题讨论】: