关闭websocket连接错误
Closing websocket connection error
您好,我在 node.js 服务器上使用 websockets 来建立 WebRTC 连接。但是当我结束连接时,节点服务器控制台会给我以下错误:
conn.otherName = null;
^
TypeError: Cannot set property 'otherName' of undefined
Other name 是另一个 Peer 的名称,我在连接中设置如下:
案例"offer":
//例如。 UserA 想打电话给 UserB
console.log("Sending offer to: ", 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"
});
}
}
}
});
如何更改它才能关闭连接而不会使我的节点服务器崩溃?
通过一些防御代码阻止它崩溃非常简单,改变这个
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] 返回未定义的原因(这是你的练习),但它会阻止它崩溃
您好,我在 node.js 服务器上使用 websockets 来建立 WebRTC 连接。但是当我结束连接时,节点服务器控制台会给我以下错误:
conn.otherName = null;
^
TypeError: Cannot set property 'otherName' of undefined
Other name 是另一个 Peer 的名称,我在连接中设置如下: 案例"offer": //例如。 UserA 想打电话给 UserB console.log("Sending offer to: ", 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"
});
}
}
}
});
如何更改它才能关闭连接而不会使我的节点服务器崩溃?
通过一些防御代码阻止它崩溃非常简单,改变这个
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] 返回未定义的原因(这是你的练习),但它会阻止它崩溃