2021-10-24 22:48:10 +00:00
|
|
|
/**
|
|
|
|
* @param {string} videoUrl
|
|
|
|
* @param {{name: string, url: string}[]} subtitles
|
|
|
|
*/
|
|
|
|
const createVideoElement = (videoUrl, subtitles) => {
|
|
|
|
const video = document.createElement("video");
|
|
|
|
video.controls = true;
|
|
|
|
video.autoplay = false;
|
2021-11-05 20:40:42 +00:00
|
|
|
video.crossOrigin = "anonymous";
|
2021-10-24 22:48:10 +00:00
|
|
|
|
|
|
|
const source = document.createElement("source");
|
|
|
|
source.src = videoUrl;
|
|
|
|
|
|
|
|
video.appendChild(source);
|
|
|
|
|
|
|
|
let first = true;
|
|
|
|
for (const { name, url } of subtitles) {
|
|
|
|
const track = document.createElement("track");
|
|
|
|
track.label = name;
|
|
|
|
track.src = url;
|
|
|
|
track.kind = "captions";
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
track.default = true;
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
video.appendChild(track);
|
|
|
|
}
|
|
|
|
|
|
|
|
return video;
|
|
|
|
}
|
|
|
|
|
2021-10-25 00:51:26 +00:00
|
|
|
let outgoingDebounce = false;
|
|
|
|
let outgoingDebounceCallbackId = null;
|
|
|
|
|
2021-10-25 01:59:52 +00:00
|
|
|
const setDebounce = () => {
|
|
|
|
outgoingDebounce = true;
|
|
|
|
|
|
|
|
if (outgoingDebounceCallbackId) {
|
|
|
|
cancelIdleCallback(outgoingDebounceCallbackId);
|
|
|
|
outgoingDebounceCallbackId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
outgoingDebounceCallbackId = setTimeout(() => {
|
|
|
|
outgoingDebounce = false;
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
|
2021-11-05 13:07:21 +00:00
|
|
|
const clearChat = () => {
|
|
|
|
document.querySelector("#chatbox").innerHTML = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const printToChat = (elem) => {
|
|
|
|
const chatbox = document.querySelector("#chatbox");
|
|
|
|
chatbox.appendChild(elem);
|
|
|
|
chatbox.scrollTop = chatbox.scrollHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleChatEvent = (event) => {
|
|
|
|
switch (event.op) {
|
|
|
|
case "UserJoin": {
|
|
|
|
// print something to the chat
|
|
|
|
const chatMessage = document.createElement("div");
|
|
|
|
chatMessage.classList.add("chat-message");
|
|
|
|
chatMessage.classList.add("user-join");
|
|
|
|
const userName = document.createElement("strong");
|
|
|
|
userName.textContent = event.data;
|
|
|
|
chatMessage.appendChild(userName);
|
|
|
|
chatMessage.appendChild(document.createTextNode(" joined"));
|
|
|
|
printToChat(chatMessage);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "UserLeave": {
|
|
|
|
const chatMessage = document.createElement("div");
|
|
|
|
chatMessage.classList.add("chat-message");
|
|
|
|
chatMessage.classList.add("user-leave");
|
|
|
|
const userName = document.createElement("strong");
|
|
|
|
userName.textContent = event.data;
|
|
|
|
chatMessage.appendChild(userName);
|
|
|
|
chatMessage.appendChild(document.createTextNode(" left"));
|
|
|
|
printToChat(chatMessage);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ChatMessage": {
|
|
|
|
const chatMessage = document.createElement("div");
|
|
|
|
chatMessage.classList.add("chat-message");
|
|
|
|
const userName = document.createElement("strong");
|
|
|
|
userName.innerText = event.data.user;
|
|
|
|
chatMessage.appendChild(userName);
|
|
|
|
chatMessage.appendChild(document.createTextNode(" "));
|
|
|
|
const messageContent = document.createElement("span");
|
|
|
|
messageContent.classList.add("message-content");
|
|
|
|
messageContent.textContent = event.data.message;
|
|
|
|
chatMessage.appendChild(messageContent);
|
|
|
|
printToChat(chatMessage);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 22:48:10 +00:00
|
|
|
/**
|
|
|
|
* @param {WebSocket} socket
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
*/
|
|
|
|
const setupSocketEvents = (socket, video) => {
|
|
|
|
const setVideoTime = time => {
|
|
|
|
const timeSecs = time / 1000.0;
|
|
|
|
|
|
|
|
if (Math.abs(video.currentTime - timeSecs) > 0.5) {
|
|
|
|
video.currentTime = timeSecs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.addEventListener("message", async messageEvent => {
|
|
|
|
try {
|
|
|
|
const event = JSON.parse(messageEvent.data);
|
|
|
|
console.log(event);
|
|
|
|
|
|
|
|
switch (event.op) {
|
|
|
|
case "SetPlaying":
|
2021-10-25 01:59:52 +00:00
|
|
|
setDebounce();
|
|
|
|
|
2021-10-24 22:48:10 +00:00
|
|
|
if (event.data.playing) {
|
|
|
|
await video.play();
|
|
|
|
} else {
|
|
|
|
video.pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
setVideoTime(event.data.time);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case "SetTime":
|
2021-10-25 01:59:52 +00:00
|
|
|
setDebounce();
|
2021-10-24 22:48:10 +00:00
|
|
|
setVideoTime(event.data);
|
|
|
|
break;
|
2021-11-05 13:07:21 +00:00
|
|
|
case "UserJoin":
|
|
|
|
case "UserLeave":
|
|
|
|
case "ChatMessage":
|
|
|
|
handleChatEvent(event);
|
|
|
|
break;
|
2021-10-24 22:48:10 +00:00
|
|
|
}
|
|
|
|
} catch (_err) {
|
|
|
|
}
|
2021-10-25 00:51:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} sessionId
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* @param {WebSocket} socket
|
|
|
|
*/
|
|
|
|
const setupVideoEvents = (sessionId, video, socket) => {
|
|
|
|
const currentVideoTime = () => (video.currentTime * 1000) | 0;
|
|
|
|
|
|
|
|
video.addEventListener("pause", async event => {
|
|
|
|
if (outgoingDebounce) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
"op": "SetPlaying",
|
|
|
|
"data": {
|
|
|
|
"playing": false,
|
|
|
|
"time": currentVideoTime(),
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
video.addEventListener("play", event => {
|
|
|
|
if (outgoingDebounce) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
"op": "SetPlaying",
|
|
|
|
"data": {
|
|
|
|
"playing": true,
|
|
|
|
"time": currentVideoTime(),
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
let firstSeekComplete = false;
|
|
|
|
video.addEventListener("seeked", async event => {
|
|
|
|
if (!firstSeekComplete) {
|
|
|
|
// The first seeked event is performed by the browser when the video is loading
|
|
|
|
firstSeekComplete = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outgoingDebounce) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
"op": "SetTime",
|
|
|
|
"data": currentVideoTime(),
|
|
|
|
}));
|
2021-10-24 22:48:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} videoUrl
|
|
|
|
* @param {{name: string, url: string}[]} subtitles
|
|
|
|
* @param {number} currentTime
|
|
|
|
* @param {boolean} playing
|
|
|
|
* @param {WebSocket} socket
|
|
|
|
*/
|
|
|
|
const setupVideo = async (sessionId, videoUrl, subtitles, currentTime, playing, socket) => {
|
2021-10-25 01:59:52 +00:00
|
|
|
document.querySelector("#pre-join-controls").style["display"] = "none";
|
2021-10-24 22:48:10 +00:00
|
|
|
const video = createVideoElement(videoUrl, subtitles);
|
2021-10-25 01:59:52 +00:00
|
|
|
document.querySelector("#video-container").appendChild(video);
|
2021-10-24 22:48:10 +00:00
|
|
|
|
|
|
|
video.currentTime = (currentTime / 1000.0);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (playing) {
|
|
|
|
await video.play()
|
|
|
|
} else {
|
|
|
|
video.pause()
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// Auto-play is probably disabled, we should uhhhhhhh do something about it
|
|
|
|
}
|
|
|
|
|
|
|
|
setupSocketEvents(socket, video);
|
2021-10-25 00:51:26 +00:00
|
|
|
setupVideoEvents(sessionId, video, socket);
|
2021-10-24 22:48:10 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 13:07:21 +00:00
|
|
|
const fixChatSize = () => {
|
|
|
|
const video = document.querySelector("video");
|
|
|
|
const chatbox = document.querySelector("#chatbox");
|
|
|
|
const chatboxContainer = document.querySelector("#chatbox-container");
|
|
|
|
|
|
|
|
if (video && chatbox && chatboxContainer) {
|
|
|
|
const delta = chatboxContainer.clientHeight - chatbox.clientHeight;
|
|
|
|
|
|
|
|
chatbox.style["height"] = `calc(${(window.innerHeight - video.clientHeight)}px - ${delta}px - 1em)`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const setupChatboxEvents = (socket) => {
|
|
|
|
// clear events by just reconstructing the form
|
|
|
|
const oldChatForm = document.querySelector("#chatbox-send");
|
|
|
|
const chatForm = oldChatForm.cloneNode(true);
|
|
|
|
oldChatForm.replaceWith(chatForm);
|
|
|
|
|
|
|
|
chatForm.addEventListener("submit", e => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const input = chatForm.querySelector("input");
|
|
|
|
const content = input.value;
|
2021-11-05 20:57:10 +00:00
|
|
|
if (content.trim().length) {
|
|
|
|
input.value = "";
|
|
|
|
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
"op": "ChatMessage",
|
|
|
|
"data": {
|
|
|
|
"message": content,
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2021-11-05 13:07:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-25 01:59:52 +00:00
|
|
|
/**
|
|
|
|
* @param {string} sessionId
|
|
|
|
* @param {WebSocket} socket
|
|
|
|
*/
|
|
|
|
const setupChat = async (sessionId, socket) => {
|
2021-11-05 13:07:21 +00:00
|
|
|
document.querySelector("#chatbox-container").style["display"] = "block";
|
|
|
|
setupChatboxEvents(socket);
|
|
|
|
fixChatSize();
|
2021-10-25 01:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} nickname
|
|
|
|
* @param {string} sessionId
|
|
|
|
*/
|
|
|
|
const joinSession = async (nickname, sessionId) => {
|
2021-10-24 22:48:10 +00:00
|
|
|
try {
|
|
|
|
window.location.hash = sessionId;
|
|
|
|
|
|
|
|
const {
|
|
|
|
video_url, subtitle_tracks,
|
|
|
|
current_time_ms, is_playing
|
|
|
|
} = await fetch(`/sess/${sessionId}`).then(r => r.json());
|
|
|
|
|
2021-10-25 01:59:52 +00:00
|
|
|
const wsUrl = new URL(`/sess/${sessionId}/subscribe?nickname=${encodeURIComponent(nickname)}`, window.location.href);
|
2021-10-24 22:48:10 +00:00
|
|
|
wsUrl.protocol = { "http:": "ws:", "https:": "wss:" }[wsUrl.protocol];
|
|
|
|
const socket = new WebSocket(wsUrl.toString());
|
|
|
|
|
2021-10-25 01:59:52 +00:00
|
|
|
socket.addEventListener("open", () => {
|
|
|
|
setupVideo(sessionId, video_url, subtitle_tracks, current_time_ms, is_playing, socket);
|
|
|
|
setupChat(sessionId, socket);
|
|
|
|
});
|
2021-10-24 22:48:10 +00:00
|
|
|
} catch (err) {
|
|
|
|
// TODO: Show an error on the screen
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const main = () => {
|
2021-10-25 01:59:52 +00:00
|
|
|
document.querySelector("#join-session-nickname").value = localStorage.getItem("watch-party-nickname");
|
|
|
|
|
2021-10-24 22:48:10 +00:00
|
|
|
document.querySelector("#join-session-form").addEventListener("submit", event => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2021-10-25 01:59:52 +00:00
|
|
|
const nickname = document.querySelector("#join-session-nickname").value;
|
2021-10-24 22:48:10 +00:00
|
|
|
const sessionId = document.querySelector("#join-session-id").value;
|
2021-10-25 01:59:52 +00:00
|
|
|
|
|
|
|
localStorage.setItem("watch-party-nickname", nickname);
|
|
|
|
joinSession(nickname, sessionId);
|
2021-10-24 22:48:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (window.location.hash.match(/#[0-9a-f\-]+/)) {
|
|
|
|
document.querySelector("#join-session-id").value = window.location.hash.substring(1);
|
|
|
|
}
|
2021-11-05 13:07:21 +00:00
|
|
|
|
|
|
|
window.addEventListener("resize", event => {
|
|
|
|
fixChatSize();
|
|
|
|
});
|
2021-10-24 22:48:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (document.readyState === "complete") {
|
|
|
|
main();
|
|
|
|
} else {
|
|
|
|
document.addEventListener("DOMContentLoaded", main);
|
|
|
|
}
|