130 lines
4.5 KiB
JavaScript
130 lines
4.5 KiB
JavaScript
// 2013, @muazkh » github.com/muaz-khan
|
|
// MIT License » https://webrtc-experiment.appspot.com/licence/
|
|
// Documentation » https://github.com/muaz-khan/WebRTC-Experiment/tree/master/websocket
|
|
|
|
var config = {
|
|
openSocket: function(config) {
|
|
config.channel = config.channel || this.channel || location.hash.substr(1) || 'websocket-experiment';
|
|
var websocket = new WebSocket('wss://www.webrtc-experiment.com:8563');
|
|
websocket.channel = config.channel;
|
|
websocket.onopen = function() {
|
|
websocket.push(JSON.stringify({
|
|
open: true,
|
|
channel: config.channel
|
|
}));
|
|
if (config.callback) config.callback(websocket);
|
|
};
|
|
websocket.onmessage = function(event) {
|
|
config.onmessage(JSON.parse(event.data));
|
|
};
|
|
websocket.push = websocket.send;
|
|
websocket.send = function(data) {
|
|
websocket.push(JSON.stringify({
|
|
data: data,
|
|
channel: config.channel
|
|
}));
|
|
};
|
|
},
|
|
onRemoteStream: function(media) {
|
|
var video = media.video;
|
|
|
|
video.setAttribute('controls', true);
|
|
video.setAttribute('id', media.stream.id);
|
|
|
|
participants.insertBefore(video, participants.firstChild);
|
|
|
|
video.play();
|
|
rotateVideo(video);
|
|
},
|
|
onRemoteStreamEnded: function(stream) {
|
|
var video = document.getElementById(stream.id);
|
|
if (video) video.parentNode.removeChild(video);
|
|
},
|
|
onRoomFound: function(room) {
|
|
var alreadyExist = document.getElementById(room.broadcaster);
|
|
if (alreadyExist) return;
|
|
|
|
if (typeof roomsList === 'undefined') roomsList = document.body;
|
|
|
|
var tr = document.createElement('tr');
|
|
tr.setAttribute('id', room.broadcaster);
|
|
tr.innerHTML = '<td>' + room.roomName + '</td>' +
|
|
'<td><button class="join" id="' + room.roomToken + '">Join</button></td>';
|
|
roomsList.insertBefore(tr, roomsList.firstChild);
|
|
|
|
tr.onclick = function() {
|
|
var tr = this;
|
|
captureUserMedia(function() {
|
|
rtcLib.joinRoom({
|
|
roomToken: tr.querySelector('.join').id,
|
|
joinUser: tr.id
|
|
});
|
|
});
|
|
hideUnnecessaryStuff();
|
|
};
|
|
}
|
|
};
|
|
|
|
function createButtonClickHandler() {
|
|
captureUserMedia(function() {
|
|
rtcLib.createRoom({
|
|
roomName: (document.getElementById('room-name') || { }).value || 'Anonymous'
|
|
});
|
|
});
|
|
hideUnnecessaryStuff();
|
|
}
|
|
|
|
function captureUserMedia(callback) {
|
|
var video = document.createElement('video');
|
|
video.setAttribute('autoplay', true);
|
|
video.setAttribute('controls', true);
|
|
participants.insertBefore(video, participants.firstChild);
|
|
|
|
getUserMedia({
|
|
video: video,
|
|
onsuccess: function(stream) {
|
|
config.attachStream = stream;
|
|
callback && callback();
|
|
|
|
video.setAttribute('muted', true);
|
|
rotateVideo(video);
|
|
},
|
|
onerror: function() {
|
|
alert('unable to get access to your webcam');
|
|
callback && callback();
|
|
}
|
|
});
|
|
}
|
|
|
|
// You can use! window.onload = function() {}
|
|
var rtcLib = RTCLib(config);
|
|
|
|
/* UI specific */
|
|
var participants = document.getElementById("participants") || document.body;
|
|
var startConferencing = document.getElementById('share-camera');
|
|
var roomsList = document.getElementById('rooms-list');
|
|
|
|
if (startConferencing) startConferencing.onclick = createButtonClickHandler;
|
|
|
|
function hideUnnecessaryStuff() {
|
|
var visibleElements = document.getElementsByClassName('visible'),
|
|
length = visibleElements.length;
|
|
for (var i = 0; i < length; i++) {
|
|
visibleElements[i].style.display = 'none';
|
|
}
|
|
}
|
|
|
|
function rotateVideo(video) {
|
|
video.style[navigator.mozGetUserMedia ? 'transform' : '-webkit-transform'] = 'rotate(0deg)';
|
|
setTimeout(function() {
|
|
video.style[navigator.mozGetUserMedia ? 'transform' : '-webkit-transform'] = 'rotate(360deg)';
|
|
}, 1000);
|
|
}
|
|
|
|
(function() {
|
|
var uniqueToken = document.getElementById('unique-token');
|
|
if (uniqueToken)
|
|
if (location.hash.length > 2) uniqueToken.parentNode.parentNode.parentNode.innerHTML = '<h2 style="text-align:center;"><a href="' + location.href + '" target="_blank">Share this link</a></h2>';
|
|
else uniqueToken.innerHTML = uniqueToken.parentNode.parentNode.href = '#' + (Math.random() * new Date().getTime()).toString(36).toUpperCase().replace( /\./g , '-');
|
|
})();
|