Javascript Webrtc数据通道始终处于连接状态且未打开
我正在创建一个小的WebRTC应用程序,目前用于交换短信。我已使WebRTC连接正常工作,但数据通道始终处于“连接”状态,并且从未进入“打开”状态 请告诉我这里缺少什么 以下是JSJavascript Webrtc数据通道始终处于连接状态且未打开,javascript,webrtc,Javascript,Webrtc,我正在创建一个小的WebRTC应用程序,目前用于交换短信。我已使WebRTC连接正常工作,但数据通道始终处于“连接”状态,并且从未进入“打开”状态 请告诉我这里缺少什么 以下是JS socket.onmessage = function(e){ console.log("Message from signaling server"); writeToScreen('<span class="server">Server: <
socket.onmessage = function(e){
console.log("Message from signaling server");
writeToScreen('<span class="server">Server: </span>'+e.data);
var data = JSON.parse(e.data);
switch(data.type) {
case "login":
onLogin(data.success);
break;
case "offer":
onOffer(data.offer, data.name);
break;
case "answer":
onAnswer(data.answer);
break;
case "candidate":
onCandidate(data.candidate);
break;
default:
break;
}
}
// Enable send and close button
$('#send').prop('disabled', false);
$('#close').prop('disabled', false);
$('#connect').prop('disabled', true);
}
function close(){
socket.close();
}
function writeToScreen(msg){
var screen = $('#screen');
screen.append('<p>'+msg+'</p>');
screen.animate({scrollTop: screen.height()}, 10);
}
function clearScreen(){
$('#screen').html('');
}
function sendMessage(){
if(!socket || socket == undefined) return false;
var mess = $.trim($('#message').val());
if(mess == '') return;
writeToScreen('<span class="client">Client: </span>'+mess);
socket.send(mess);
// Clear input
$('#message').val('');
}
$(document).ready(function(){
$('#message').focus();
$('#frmInput').submit(function(){
sendMessage();
});
$('#connect').click(function(){
connect();
});
$('#close').click(function(){
close();
});
$('#clear').click(function(){
clearScreen();
});
});
if (!window.RTCPeerConnection) {
window.RTCPeerConnection = window.webkitRTCPeerConnection;
}
var configuration = {
"iceServers": [
{
"urls": "stun:mmt-stun.verkstad.net"
},
{
"urls": "turn:mmt-turn.verkstad.net",
"username": "webrtc",
"credential": "secret"
}
]
};
myConnection = new RTCPeerConnection(configuration,{optional:[{RtpDataChannels: true},{DtlsSrtpKeyAgreement: true}]});
console.log("RTCPeerConnection object was created");
console.log(myConnection);
openDataChannel();
//when the browser finds an ice candidate we send it to another peer
myConnection.onicecandidate = function (event) {
console.log(event.candidate);
if (event.candidate) {
send({
type: "candidate",
candidate: event.candidate
});
}
};
// Datachannel
var mediaConstraints = {
'offerToReceiveAudio': 1,
'offerToReceiveVideo': 1
};
var connectToOtherUsernameBtn = document.getElementById("connectToOtherUsernameBtn");
console.log(connectToOtherUsernameBtn);
connectToOtherUsernameBtn.addEventListener("click", function () {
console.log("ice state : "+myConnection.iceGatheringState);
var otherUsername = connectToOtherUsernameBtn.value;
connectedUser = otherUsername;
if (otherUsername.length > 0) {
//make an offer
myConnection.createOffer(function (offer) {
send({
type: "offer",
offer: offer
});
console.log(offer);
console.log(typeof(offer));
myConnection.setLocalDescription(offer);
console.log("localDescription");
console.log(myConnection.localDescription);
}, function (error) {
alert("An error has occurred.");
console.log(error);
});
}
});
function send(message) {
if (connectedUser) {
message.name = connectedUser;
}
socket.send(JSON.stringify(message));
};
//when somebody wants to call us
function onOffer(offer, name) {
console.log("offer recieved");
connectedUser = name;
myConnection.setRemoteDescription(new RTCSessionDescription(offer));
myConnection.createAnswer(function (answer) {
myConnection.setLocalDescription(answer);
send({
type: "answer",
answer: answer
});
}, function (error) {
alert("oops...error");
console.log(error);
});
console.log("Answer sent");
}
//when another user answers to our offer
function onAnswer(answer) {
console.log("answer recieved");
myConnection.setRemoteDescription(new RTCSessionDescription(answer));
console.log(myConnection.iceConnectionState );
}
//when we got ice candidate from another user
function onCandidate(candidate) {
myConnection.addIceCandidate(new RTCIceCandidate(candidate));
}
});
//data channel
//creating data channel
function openDataChannel() {
console.log("opening Data Channel");
var dataChannelOptions = {
reliable:true,
};
dataChannel = myConnection.createDataChannel("myDataChannel",dataChannelOptions);
dataChannel.onerror = function (error) {
console.log("Error:", error);
};
dataChannel.onmessage = function (event) {
console.log("Got message:", event.data);
};
}
function sendmsg() {
console.log("send message");
var msgInput=document.getElementById("msgInput");
var val = msgInput.value;
console.log(val);
dataChannel.send(val);
}
function checkstatus(){
console.log("Checking Status");
console.log("signalingState: "+myConnection.signalingState);
console.log("iceConnectionState: "+myConnection.iceConnectionState);
console.log("iceGatheringState: "+myConnection.iceGatheringState);
console.log("localDescription: ");
console.log(myConnection.localDescription);
console.log("remoteDescription:");
console.log(myConnection.remoteDescription);
console.log("Connestion id");
console.log(dataChannel.id);
console.log("Connestion readyState");
console.log(dataChannel.readyState);
}
socket.onmessage=函数(e){
日志(“来自信令服务器的消息”);
writeToScreen('服务器:'+e.data);
var data=JSON.parse(e.data);
开关(数据类型){
案例“登录”:
onLogin(data.success);
打破
案例“要约”:
onOffer(data.offer,data.name);
打破
案例“答案”:
onAnswer(data.answer);
打破
案例“候选人”:
onCandidate(data.candidate);
打破
违约:
打破
}
}
//启用发送和关闭按钮
$('#send').prop('disabled',false);
$('#close').prop('disabled',false);
$('#connect').prop('disabled',true);
}
函数关闭(){
socket.close();
}
函数writeToScreen(msg){
变量屏幕=$(“#屏幕”);
screen.append(''+msg+'');
动画({scrollTop:screen.height()},10);
}
函数clearScreen(){
$('#screen').html('');
}
函数sendMessage(){
如果(!socket | | socket==未定义)返回false;
var mess=$.trim($('#message').val());
如果(mess='')返回;
writeToScreen('Client:'+mess);
socket.send(mess);
//清晰输入
$('#message').val('');
}
$(文档).ready(函数(){
$(“#消息”).focus();
$('#frmInput')。提交(函数(){
sendMessage();
});
$(“#连接”)。单击(函数(){
connect();
});
$(“#关闭”)。单击(函数(){
close();
});
$(“#清除”)。单击(函数(){
清除屏幕();
});
});
如果(!window.rtpeerconnection){
window.rtpeerconnection=window.webkittrtpeerconnection;
}
变量配置={
“ICEServer”:[
{
“URL:“stun:mmt stun.verkstad.net”
},
{
“URL”:“turn:mmt turn.verkstad.net”,
“用户名”:“webrtc”,
“凭证”:“机密”
}
]
};
myConnection=new rtpeerconnection(配置,{可选:[{RtpDataChannels:true},{DtlsSrtpKeyAgreement:true}]});
log(“创建了RTPeerConnection对象”);
console.log(myConnection);
openDataChannel();
//当浏览器找到ice候选对象时,我们将其发送给另一个对等方
myConnection.onicecandidate=函数(事件){
console.log(event.candidate);
if(事件候选){
发送({
键入:“候选人”,
候选人:event.candidate
});
}
};
//数据通道
变量mediaConstraints={
“offerToReceiveAudio”:1,
“offerToReceiveVideo”:1
};
var connecttoutherusernamebtn=document.getElementById(“connecttoutherusernamebtn”);
console.log(connectToOtherUsernameBtn);
ConnectToutorUserNameBtn.addEventListener(“单击”,函数)(){
console.log(“ice状态:+myConnection.iceGatheringState”);
var otherUsername=connecttoutherusernamebtn.value;
connectedUser=otherUsername;
如果(otherUsername.length>0){
//出价
myConnection.createOffer(函数(offer){
发送({
键入:“报价”,
报价:报价
});
控制台日志(报价);
控制台日志(报价类型);
myConnection.setLocalDescription(提供);
log(“localDescription”);
log(myConnection.localDescription);
},函数(错误){
警报(“发生错误”);
console.log(错误);
});
}
});
函数发送(消息){
如果(连接用户){
message.name=connectedUser;
}
send(JSON.stringify(message));
};
//当有人想打电话给我们时
功能onOffer(报价、名称){
控制台日志(“收到报价”);
connectedUser=name;
myConnection.setRemoteDescription(新的RTCSessionDescription(提供));
myConnection.createAnswer(函数(应答){
myConnection.setLocalDescription(答案);
发送({
键入:“回答”,
答复:答复:
});
},函数(错误){
警报(“oops…错误”);
console.log(错误);
});
控制台日志(“已发送应答”);
}
//当其他用户响应我们的报价时
回答功能(答案){
console.log(“收到的答复”);
myConnection.setRemoteDescription(新的RTCSessionDescription(应答));
console.log(myConnection.iceConnectionState);
}
//当我们从另一个用户那里得到ice候选人时
函数onCandidate(候选){
myConnection.addIceCandidate(新的RTICE候选者(候选者));
}
});
//数据通道
//创建数据通道
函数openDataChannel(){
console.log(“打开数据通道”);
var数据通道选项={
可靠:对,,
};
dataChannel=myConnection.createDataChannel(“myDataChannel”,dataChannelOptions);
dataChannel.onerror=函数(错误){
日志(“错误:”,错误);
};
dataChannel.onmessage=函数(事件){
log(“获取消息:”,event.data);
};
}
函数sendmsg(){
控制台日志(“发送消息”);
var msgInput=document.getElementById(“msgInput”);
var val=msgInput.value;
控制台日志(val);
数据通道发送(val);
}
函数checkstatus(){
控制台日志(“检查状态”);
日志(“信号状态:+myConnection.signalingState”);
log(“iceConnectionState:+myConnection.iceConnectionState”);
log(“iceGatheringState:+myConnection.iceGatheringState”);
log(“localDescription:”);
log(myConnection.localDescription);
日志(“remoteDescription:”);
骗局