<textareaid="dataChannelSend"disabledplaceholder="Press Start, enter some text, then press Send."></textarea><textareaid="dataChannelReceive"disabled></textarea><divid="buttons"> <buttonid="startButton">Start</button> <buttonid="sendButton">Send</button> <buttonid="closeButton">Stop</button></div>
一个textarea将用于输入文本,另一个textarea将显示在对等体之间流式传输的文本。
index.html现在应该是这样的:
<!DOCTYPE html>
<html>
<head>
<title>Realtime communication with WebRTC</title>
<link rel="stylesheet" href="css/main.css" />
</head>
<body>
<h1>Realtime communication with WebRTC</h1>
<textarea id="dataChannelSend" disabled
placeholder="Press Start, enter some text, then press Send."></textarea>
<textarea id="dataChannelReceive" disabled></textarea>
<div id="buttons">
<button id="startButton">Start</button>
<button id="sendButton">Send</button>
<button id="closeButton">Stop</button>
</div>
<script src="js/lib/adapter.js"></script>
<script src="js/main.js"></script>
</body>
</html>
functioncreateConnection() {dataChannelSend.placeholder ='';var servers =null; pcConstraint =null; dataConstraint =null;trace('Using SCTP based data channels');// For SCTP, reliable and ordered delivery is true by default.// Add localConnection to global scope to make it visible// from the browser console.window.localConnection = localConnection =newRTCPeerConnection(servers, pcConstraint);trace('Created local peer connection object localConnection'); sendChannel =localConnection.createDataChannel('sendDataChannel', dataConstraint);trace('Created send data channel');localConnection.onicecandidate = iceCallback1;sendChannel.onopen = onSendChannelStateChange;sendChannel.onclose = onSendChannelStateChange;// Add remoteConnection to global scope to make it visible// from the browser console.window.remoteConnection = remoteConnection =newRTCPeerConnection(servers, pcConstraint);trace('Created remote peer connection object remoteConnection');remoteConnection.onicecandidate = iceCallback2;remoteConnection.ondatachannel = receiveChannelCallback;localConnection.createOffer().then( gotDescription1, onCreateSessionDescriptionError );startButton.disabled =true;closeButton.disabled =false;}functionsendData() {var data =dataChannelSend.value;sendChannel.send(data);trace('Sent Data: '+ data);}