JSFiddle - React, Tailwind, and code Playground
JavaScript
/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/
'use strict';
var localConnection;
var remoteConnection;
var sendChannel;
var receiveChannel;
var rttSum = 0;
var count = 0;
var useHost = false;
setTimeout(createConnection, 0);
function trace(x) {
console.log(x);
}
function createConnection() {
var config = { iceServers: [ { url: "stun:stun.l.google.com:19302"}]};
trace('Using SCTP based data channels');
// SCTP is supported from Chrome 31 and is supported in FF.
// No need to pass DTLS constraint as it is on by default in Chrome 31.
// For SCTP, reliable and ordered is true by default.
// Add localConnection to global scope to make it visible from the browser console.
localConnection =
new RTCPeerConnection(config);
trace('Created local peer connection object localConnection');
sendChannel = localConnection.createDataChannel('sendDataChannel');
trace('Created send data channel');
localConnection.onicecandidate = iceCallback1;
sendChannel.onopen = onSendChannelStateChange;
sendChannel.onclose = onSendChannelStateChange;
sendChannel.onmessage = onSendChannelReceivedMessage;
// Add remoteConnection to global scope to make it visible from the browser console.
remoteConnection =
new RTCPeerConnection(config);
trace('Created remote peer connection object remoteConnection');
remoteConnection.onicecandidate = iceCallback2;
remoteConnection.ondatachannel = receiveChannelCallback;
localConnection.createOffer(gotDescription1, onCreateSessionDescriptionError);
}
function onCreateSessionDescriptionError(error) {
trace('Failed to create session description: ' + error.toString());
}
function sendData() {
var data = Date.now();
sendChannel.send(data);
trace('Sent Data: ' + data);
}
function closeDataChannels() {
trace('Closing data...