我一直在Chrome 43中的webkitRTCPeerConnection
上进行一些本地化测试,并在node.js中运行本地TCP服务器以接收来自对等方的STUN数据包。我的客户端代码(用于本地测试)如下:
// Chrome only
var iceServers = {
iceServers: [{
url: "stun:localhost:8080"
}]
},
optional = {
optional: [{
DtlsSrtpKeyAgreement: true
}, {
RtpDataChannels: true
}]
},
peer1 = new webkitRTCPeerConnection(iceServers, optional),
peer2 = new webkitRTCPeerConnection(iceServers, optional);
peer1.createOffer(function(offer) {
peer1.setLocalDescription(offer);
peer2.setRemoteDescription(offer);
peer2.createAnswer(function(answer) {
peer1.setRemoteDescription(answer);
});
});
My Node.JS TCP套接字服务器仅用于接收STUN数据包并立即记录。它最终将成为RFC 5389中概述的TCP STUN服务器。现在这里是它的代码:
var net = require('net'),
options = {
allowHalfOpen: true
},
port = 8080,
server = net.createServer(options, connectionListener);
server.listen(port, listening);
function connectionListener(connection) {
var data = '';
console.log('Connection opened');
connection.setEncoding('utf8');
connection.on('data', function (buffer) {
console.log('data received');
data += buffer;
});
connection.on('end', function () {
console.log('data stream ended');
connection.end();
console.log('----\n%s\n----', data);
});
}
function listening() {
console.log('Listening on port %d', port);
}
当客户端代码完成运行时,peer1.iceConnectionState
为"checking"
,而peer1.iceGatheringState
,peer2.iceConnectionState
和peer2.iceGatheringState
都保留"new"
。除了Listening on port 8080
之外,TCP服务器上没有任何日志。任何人都可以告诉我为什么peer1.iceConnectionState
为"checking"
时没有打开任何关联?