MovieNight/static/js/client.js

162 lines
4.2 KiB
JavaScript
Raw Normal View History

function initPlayer() {
if (flvjs.isSupported()) {
var videoElement = document.getElementById('videoElement');
var flvPlayer = flvjs.createPlayer({
type: 'flv',
url: '/live'
});
flvPlayer.attachMediaElement(videoElement);
flvPlayer.load();
flvPlayer.play();
}
}
function setPlaying(title, link) {
if (title === "") {
$('#playing').hide();
$('#playinglink').hide();
document.title = "Movie Night"
return;
}
$('#playing').show();
$('#playing').text(title);
document.title = "Movie Night | " + title;
if (link === "") {
$('#playinglink').hide();
return;
}
$('#playinglink').show();
$('#playinglink').text('[Info Link]');
$('#playinglink').attr('href', link);
}
2019-03-12 04:15:42 +01:00
function startGo() {
if (!WebAssembly.instantiateStreaming) { // polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
const go = new Go();
WebAssembly.instantiateStreaming(fetch("/static/main.wasm"), go.importObject).then((result) => {
go.run(result.instance);
2019-03-12 04:15:42 +01:00
}).catch((err) => {
console.error(err);
});
}
function getWsUri() {
port = window.location.port;
if (port == "") {
port = "8089";
}
return "ws://" + window.location.hostname + ":" + port + "/ws";
}
let maxMessageCount = 0
2019-03-12 04:15:42 +01:00
function appendMessages(msg) {
let msgs = $("#messages").find('div');
// let's just say that if the max count is less than 1, then the count is infinite
// the server side should take care of chaking max count ranges
if (msgs.length > maxMessageCount) {
msgs.first().remove();
}
2019-03-12 04:15:42 +01:00
$("#messages").append(msg).scrollTop(9e6);
}
inChat = false
2019-03-12 04:15:42 +01:00
function openChat() {
console.log("chat opening");
$("#joinbox").css("display", "none");
$("#chat").css("display", "grid");
$("#msg").val("");
$("#msg").focus();
inChat = true;
2019-03-12 04:15:42 +01:00
}
function closeChat() {
console.log("chat closing");
$("#joinbox").css("display", "");
$("#chat").css("display", "none");
$("#error").html("That name was already used!");
inChat = false;
2019-03-12 04:15:42 +01:00
}
function join() {
let name = $("#name").val();
if (name.length < 1 || name.length > 36) {
2019-03-12 04:15:42 +01:00
$("#error").html("Please input a name between 3 and 36 characters");
return;
}
if (!sendMessage($("#name").val())) {
$("#error").val("could not join");
return;
}
$("#error").val("");
2019-03-12 04:15:42 +01:00
openChat();
}
let ws = new WebSocket(getWsUri());
ws.onmessage = (m) => recieveMessage(m.data);
ws.onopen = (e) => console.log("Websocket Open:", e);
ws.onclose = () => closeChat();
ws.onerror = (e) => console.log("Websocket Error:", e);
function websocketSend(data) {
ws.send(data);
2019-03-12 04:15:42 +01:00
}
function sendChat() {
sendMessage($("#msg").val());
$("#msg").val("");
}
function updateSuggestionCss(m) {
if ($("#suggestions").children().length > 0) {
div = $("#suggestions")[0]
$(div).css("bottom", `calc(${$("#chat").css("height")} - ${$("#messages").css("height")} - 10px)`)
$(div).css("right", `calc(${$("#chat").css("width")} - ${$(div).css("width")} + 5px)`)
}
}
2019-03-13 03:55:35 +01:00
function chatOnload() {
$("#name").keypress(function (evt) {
if (evt.originalEvent.keyCode == 13) {
$("#join").trigger("click");
}
});
$("#msg").keypress(function (evt) {
if (evt.originalEvent.keyCode == 13 && !evt.originalEvent.shiftKey) {
$("#send").trigger("click");
evt.preventDefault();
}
});
window.onresize = updateSuggestionCss;
var suggestionObserver = new MutationObserver(
(mutations) => mutations.forEach(() => updateSuggestionCss())
).observe($("#suggestions")[0], { childList: true })
$("#send").click(() => $("#msg").focus());
$("#msg").on("keydown", (e) => {
if (processMessageKey(e)) {
e.preventDefault();
}
});
$("#msg").on("input", () => processMessage());
$("#name").focus();
// make sure startGo is last function called ignoring the autologin code
startGo();
}