2018-08-27 18:05:57 +00:00
|
|
|
// Broadcast Types
|
|
|
|
|
|
|
|
const JOIN_ROOM = "JOIN_ROOM";
|
|
|
|
const EXCHANGE = "EXCHANGE";
|
|
|
|
const REMOVE_USER = "REMOVE_USER";
|
|
|
|
|
|
|
|
// DOM Elements
|
|
|
|
let currentUser;
|
|
|
|
let localVideo;
|
|
|
|
let remoteVideoContainer;
|
|
|
|
|
|
|
|
// Objects
|
|
|
|
let pcPeers = {}; // peer connection
|
|
|
|
let localstream;
|
|
|
|
|
|
|
|
window.onload = () => {
|
2018-08-31 19:47:33 +00:00
|
|
|
currentUser = document.getElementById("current-user").innerHTML;
|
|
|
|
localVideo = document.getElementById("local-video");
|
|
|
|
remoteVideoContainer = document.getElementById("remote-video-container");
|
2018-08-27 18:05:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Ice Credentials
|
|
|
|
const ice = { iceServers: [{urls: ['stun:stun.l.google.com:19302', 'stun:stun.1.google.com:19302']}]};
|
|
|
|
|
|
|
|
// Initialize user's own video
|
|
|
|
document.onreadystatechange = async () => {
|
|
|
|
if (document.readyState === "interactive") {
|
|
|
|
try {
|
|
|
|
const stream = await navigator.mediaDevices.getUserMedia({
|
|
|
|
audio: true,
|
|
|
|
video: true
|
|
|
|
})
|
|
|
|
|
|
|
|
localstream = stream;
|
2018-08-31 19:47:33 +00:00
|
|
|
localVideo.srcObject = stream
|
|
|
|
localVideo.muted = true
|
2018-08-27 18:05:57 +00:00
|
|
|
} catch (e) { console.error(e); }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-31 19:47:33 +00:00
|
|
|
|
|
|
|
const chatroomId = document.getElementById('chatroom-hook').dataset["chatroomId"]
|
2018-08-27 18:05:57 +00:00
|
|
|
|
|
|
|
const handleJoinSession = async () => {
|
2018-08-30 17:42:42 +00:00
|
|
|
App['chatroom' + chatroomId] = await App.cable.subscriptions.create({
|
2018-08-30 02:00:09 +00:00
|
|
|
channel: "ChatRoomsChannel",
|
|
|
|
room: chatroomId
|
|
|
|
}, {
|
2018-08-27 18:05:57 +00:00
|
|
|
connected: () => {
|
|
|
|
broadcastData({
|
|
|
|
type: JOIN_ROOM,
|
2018-08-30 17:42:42 +00:00
|
|
|
from: currentUser,
|
|
|
|
room: chatroomId
|
2018-08-27 18:05:57 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
received: data => {
|
|
|
|
if (data.from === currentUser) return;
|
|
|
|
switch (data.type) {
|
|
|
|
case JOIN_ROOM:
|
|
|
|
return joinRoom(data);
|
|
|
|
case EXCHANGE:
|
|
|
|
if (data.to !== currentUser) return;
|
|
|
|
return exchange(data);
|
|
|
|
case REMOVE_USER:
|
|
|
|
return removeUser(data);
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleLeaveSession = () => {
|
|
|
|
for (user in pcPeers) {
|
|
|
|
pcPeers[user].close();
|
|
|
|
}
|
|
|
|
pcPeers = {};
|
|
|
|
|
|
|
|
App.session.unsubscribe();
|
|
|
|
|
2018-09-05 11:01:24 +00:00
|
|
|
// remoteVideoContainer.innerHTML = "";
|
2018-08-27 18:05:57 +00:00
|
|
|
|
|
|
|
broadcastData({
|
|
|
|
type: REMOVE_USER,
|
|
|
|
from: currentUser
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const joinRoom = data => {
|
|
|
|
createPC(data.from, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeUser = data => {
|
2018-09-05 11:01:24 +00:00
|
|
|
remoteVideo = document.querySelector("#remote-video-container>video");
|
|
|
|
if (video.srcObject) video.srcObject = null;
|
|
|
|
|
2018-08-27 18:05:57 +00:00
|
|
|
delete pcPeers[data.from];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const broadcastData = data => {
|
2018-08-30 17:42:42 +00:00
|
|
|
fetch("chat_room_sessions", {
|
2018-08-27 18:05:57 +00:00
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(data),
|
2018-08-30 17:42:42 +00:00
|
|
|
headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content }
|
2018-08-27 18:05:57 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const createPC = (userId, isOffer) => {
|
|
|
|
let pc = new RTCPeerConnection(ice);
|
2018-08-30 17:42:42 +00:00
|
|
|
let test = userId
|
2018-08-27 18:05:57 +00:00
|
|
|
pcPeers[userId] = pc;
|
|
|
|
pc.addStream(localstream);
|
|
|
|
if (isOffer) {
|
|
|
|
pc
|
|
|
|
.createOffer()
|
|
|
|
.then(offer => {
|
|
|
|
pc.setLocalDescription(offer);
|
|
|
|
broadcastData({
|
|
|
|
type: EXCHANGE,
|
|
|
|
from: currentUser,
|
|
|
|
to: userId,
|
2018-08-30 17:42:42 +00:00
|
|
|
sdp: JSON.stringify(pc.localDescription),
|
|
|
|
room: chatroomId
|
2018-08-27 18:05:57 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(logError);
|
|
|
|
}
|
|
|
|
|
|
|
|
pc.onicecandidate = event => {
|
|
|
|
if (event.candidate) {
|
|
|
|
broadcastData({
|
|
|
|
type: EXCHANGE,
|
|
|
|
from: currentUser,
|
|
|
|
to: userId,
|
2018-08-30 17:42:42 +00:00
|
|
|
candidate: JSON.stringify(event.candidate),
|
|
|
|
room: chatroomId
|
2018-08-27 18:05:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pc.onaddstream = event => {
|
2018-09-05 11:01:24 +00:00
|
|
|
const remoteVideo = document.querySelector("#remote-video-container>video");
|
|
|
|
remoteVideo.srcObject = event.stream;
|
|
|
|
remoteVideo.height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
|
2018-08-28 19:36:56 +00:00
|
|
|
localVideo.classList.add("video-sm");
|
2018-08-27 18:05:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pc.oniceconnectionstatechange = event => {
|
|
|
|
if (pc.iceConnectionState == "disconnected") {
|
|
|
|
console.log("Disconnected:", userId);
|
|
|
|
broadcastData({
|
|
|
|
type: REMOVE_USER,
|
|
|
|
from: userId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return pc;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const exchange = data => {
|
|
|
|
let pc;
|
|
|
|
|
|
|
|
if (!pcPeers[data.from]) {
|
|
|
|
pc = createPC(data.from, false);
|
|
|
|
} else {
|
|
|
|
pc = pcPeers[data.from];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.candidate) {
|
|
|
|
pc
|
|
|
|
.addIceCandidate(new RTCIceCandidate(JSON.parse(data.candidate)))
|
|
|
|
.then(() => console.log("Ice candidate added"))
|
|
|
|
.catch(logError);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.sdp) {
|
2018-08-31 19:47:33 +00:00
|
|
|
const sdp = JSON.parse(data.sdp);
|
2018-08-27 18:05:57 +00:00
|
|
|
pc
|
|
|
|
.setRemoteDescription(new RTCSessionDescription(sdp))
|
|
|
|
.then(() => {
|
|
|
|
if (sdp.type === "offer") {
|
|
|
|
pc.createAnswer().then(answer => {
|
|
|
|
pc.setLocalDescription(answer);
|
|
|
|
broadcastData({
|
|
|
|
type: EXCHANGE,
|
|
|
|
from: currentUser,
|
|
|
|
to: data.from,
|
2018-08-30 17:42:42 +00:00
|
|
|
sdp: JSON.stringify(pc.localDescription),
|
|
|
|
room: chatroomId
|
2018-08-27 18:05:57 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(logError);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-05 11:01:24 +00:00
|
|
|
const logError = error => {
|
|
|
|
console.error("Whoops! Error:", error);
|
|
|
|
// console.info("Reloading Page...")
|
|
|
|
// window.reload();
|
|
|
|
}
|
2018-08-31 19:47:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
const joinButton = document.getElementById("join-btn")
|
|
|
|
joinButton.addEventListener('click', event => {
|
|
|
|
handleJoinSession()
|
|
|
|
})
|
2018-08-31 20:24:38 +00:00
|
|
|
|
|
|
|
// WARNING: COMPLETELY HACKISH SOLUTION --> MUST INTRODUCE SOME SORT OF DELAY!
|
|
|
|
setTimeout(function() {
|
|
|
|
joinButton.click()
|
|
|
|
}, 5000);
|
|
|
|
|
2018-09-05 11:01:24 +00:00
|
|
|
|
|
|
|
(function addReloadToWindowObject() {
|
|
|
|
window.__proto__.reload = () => { window.location = window.location }
|
|
|
|
})();
|
|
|
|
|
2018-09-06 11:33:22 +00:00
|
|
|
|
|
|
|
// // have enter with JS
|
|
|
|
// const form = document.getElementById("chat-form")
|
|
|
|
// document.getElementById("chat-input").addEventListener("keyup", event => {
|
|
|
|
// if(event == "Enter"){
|
|
|
|
// form.submit();
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
document.getElementById("chat-form").addEventListener("click", function(event){
|
|
|
|
event.preventDefault()
|
|
|
|
});
|
|
|
|
|
|
|
|
|