2022-01-18 03:42:55 -08:00
|
|
|
import { setupVideo } from "./video.mjs?v=7";
|
|
|
|
import { setupChat, logEventToChat } from "./chat.mjs?v=7";
|
2021-11-09 05:21:14 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} sessionId
|
|
|
|
* @param {string} nickname
|
|
|
|
* @returns {WebSocket}
|
|
|
|
*/
|
2022-01-18 03:42:55 -08:00
|
|
|
const createWebSocket = (sessionId, nickname, colour) => {
|
2021-11-09 05:21:14 -08:00
|
|
|
const wsUrl = new URL(
|
|
|
|
`/sess/${sessionId}/subscribe` +
|
2022-01-18 03:42:55 -08:00
|
|
|
`?nickname=${encodeURIComponent(nickname)}` +
|
|
|
|
`&colour=${encodeURIComponent(colour)}`,
|
2021-11-09 05:21:14 -08:00
|
|
|
window.location.href
|
|
|
|
);
|
|
|
|
wsUrl.protocol = { "http:": "ws:", "https:": "wss:" }[wsUrl.protocol];
|
|
|
|
const socket = new WebSocket(wsUrl.toString());
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
};
|
|
|
|
|
|
|
|
let outgoingDebounce = false;
|
|
|
|
let outgoingDebounceCallbackId = null;
|
|
|
|
|
|
|
|
const setDebounce = () => {
|
|
|
|
outgoingDebounce = true;
|
|
|
|
|
|
|
|
if (outgoingDebounceCallbackId) {
|
|
|
|
cancelIdleCallback(outgoingDebounceCallbackId);
|
|
|
|
outgoingDebounceCallbackId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
outgoingDebounceCallbackId = setTimeout(() => {
|
|
|
|
outgoingDebounce = false;
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* @param {WebSocket} socket
|
|
|
|
*/
|
|
|
|
const setupIncomingEvents = (video, socket) => {
|
|
|
|
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);
|
|
|
|
|
2021-11-10 06:29:52 -08:00
|
|
|
if (!event.reflected) {
|
|
|
|
switch (event.op) {
|
|
|
|
case "SetPlaying":
|
|
|
|
setDebounce();
|
2021-11-09 05:21:14 -08:00
|
|
|
|
2021-11-10 06:29:52 -08:00
|
|
|
if (event.data.playing) {
|
|
|
|
await video.play();
|
|
|
|
} else {
|
|
|
|
video.pause();
|
|
|
|
}
|
2021-11-09 05:21:14 -08:00
|
|
|
|
2021-11-10 06:29:52 -08:00
|
|
|
setVideoTime(event.data.time);
|
2021-11-09 05:21:14 -08:00
|
|
|
|
2021-11-10 06:29:52 -08:00
|
|
|
break;
|
|
|
|
case "SetTime":
|
|
|
|
setDebounce();
|
|
|
|
setVideoTime(event.data);
|
|
|
|
break;
|
|
|
|
}
|
2021-11-09 05:21:14 -08:00
|
|
|
}
|
2021-11-10 06:29:52 -08:00
|
|
|
|
|
|
|
logEventToChat(event);
|
2022-01-18 03:42:55 -08:00
|
|
|
} catch (_err) {}
|
2021-11-09 05:21:14 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* @param {WebSocket} socket
|
|
|
|
*/
|
|
|
|
const setupOutgoingEvents = (video, socket) => {
|
|
|
|
const currentVideoTime = () => (video.currentTime * 1000) | 0;
|
|
|
|
|
|
|
|
video.addEventListener("pause", async (event) => {
|
2021-11-19 12:29:49 -08:00
|
|
|
if (outgoingDebounce || !video.controls) {
|
2021-11-09 05:21:14 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-22 17:22:19 -08:00
|
|
|
// don't send a pause event for the video ending
|
|
|
|
if (video.currentTime == video.duration) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-09 05:21:14 -08:00
|
|
|
socket.send(
|
|
|
|
JSON.stringify({
|
|
|
|
op: "SetPlaying",
|
|
|
|
data: {
|
|
|
|
playing: false,
|
|
|
|
time: currentVideoTime(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
video.addEventListener("play", (event) => {
|
2021-11-19 12:29:49 -08:00
|
|
|
if (outgoingDebounce || !video.controls) {
|
2021-11-09 05:21:14 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-19 12:29:49 -08:00
|
|
|
if (outgoingDebounce || !video.controls) {
|
2021-11-09 05:21:14 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.send(
|
|
|
|
JSON.stringify({
|
|
|
|
op: "SetTime",
|
|
|
|
data: currentVideoTime(),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} nickname
|
|
|
|
* @param {string} sessionId
|
|
|
|
*/
|
2022-01-18 03:42:55 -08:00
|
|
|
export const joinSession = async (nickname, sessionId, colour) => {
|
2021-11-09 05:21:14 -08:00
|
|
|
try {
|
|
|
|
window.location.hash = sessionId;
|
|
|
|
|
|
|
|
const { video_url, subtitle_tracks, current_time_ms, is_playing } =
|
|
|
|
await fetch(`/sess/${sessionId}`).then((r) => r.json());
|
|
|
|
|
2022-01-18 03:42:55 -08:00
|
|
|
const socket = createWebSocket(sessionId, nickname, colour);
|
2021-11-09 05:21:14 -08:00
|
|
|
socket.addEventListener("open", async () => {
|
|
|
|
const video = await setupVideo(
|
|
|
|
video_url,
|
|
|
|
subtitle_tracks,
|
|
|
|
current_time_ms,
|
|
|
|
is_playing
|
|
|
|
);
|
|
|
|
|
2021-12-03 12:20:08 -08:00
|
|
|
// By default, we should disable video controls if the video is already playing.
|
|
|
|
// This solves an issue where Safari users join and seek to 00:00:00 because of
|
|
|
|
// outgoing events.
|
|
|
|
if (current_time_ms != 0) {
|
|
|
|
video.controls = false;
|
|
|
|
}
|
|
|
|
|
2021-11-09 05:21:14 -08:00
|
|
|
setupOutgoingEvents(video, socket);
|
|
|
|
setupIncomingEvents(video, socket);
|
|
|
|
setupChat(socket);
|
|
|
|
});
|
|
|
|
// TODO: Close listener ?
|
|
|
|
} catch (err) {
|
|
|
|
// TODO: Show an error on the screen
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
};
|
2021-11-11 09:26:30 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} videoUrl
|
|
|
|
* @param {Array} subtitleTracks
|
|
|
|
*/
|
|
|
|
export const createSession = async (videoUrl, subtitleTracks) => {
|
|
|
|
const { id } = await fetch("/start_session", {
|
|
|
|
method: "POST",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: JSON.stringify({
|
|
|
|
video_url: videoUrl,
|
|
|
|
subtitle_tracks: subtitleTracks,
|
|
|
|
}),
|
|
|
|
}).then((r) => r.json());
|
|
|
|
|
|
|
|
window.location = `/?created=true#${id}`;
|
|
|
|
};
|