summaryrefslogtreecommitdiff
path: root/example/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'example/index.js')
-rw-r--r--example/index.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/example/index.js b/example/index.js
new file mode 100644
index 0000000..35fdbba
--- /dev/null
+++ b/example/index.js
@@ -0,0 +1,48 @@
+window.onload = function demo() {
+ // create peer connection
+ let pc = new RTCPeerConnection({
+ iceCandidatePoolSize: 4
+ });
+
+ // create websocket for signaling channel
+ let ws = new WebSocket('ws://localhost:8080');
+
+ // (callback) called upon discovery of new local ICE candidate
+ pc.onicecandidate = function(event) {
+ if (event.candidate) {
+ console.log("local candidate:\n%c%s", "color: green", event.candidate.candidate);
+ ws.send(JSON.stringify(event.candidate));
+ }
+ };
+
+ // (callback) called upon reception of new remote video track
+ pc.ontrack = function(event) {
+ document.getElementById("remoteVideo").srcObject = event.streams[0];
+ };
+
+ // receive remote answer or candidate
+ ws.onmessage = function(event) {
+ try {
+ let d = JSON.parse(event.data);
+ if ('candidate' in d) {
+ if (d.candidate) {
+ console.log("remote candidate:\n%c%s", "color: blue;", event.data);
+ }
+ } else if ('sdp' in d) {
+ console.log("remote answer:\n%c%s", "color: blue;", event.data);
+ }
+ } catch(e) {
+ console.log("remote error:\n%c%s", "color: red;", event.data);
+ }
+ };
+
+ // create offer and set to remote peer
+ ws.onopen = function(event) {
+ pc.createOffer({ offerToReceiveVideo: true }).then(function(offer) {
+ console.log("local offer:\n%c%s", "color: green;", offer.sdp);
+ pc.setLocalDescription(offer);
+ ws.send(JSON.stringify(offer));
+ });
+ };
+}
+