0

make consts; clean up get status

This commit is contained in:
Ginger Wong 2020-07-05 01:08:22 -07:00
parent 5efa67d605
commit 9bd1df8530
6 changed files with 93 additions and 84 deletions

View File

@ -79,7 +79,7 @@ GW TODO:
autoplay autoplay
playsinline playsinline
muted muted
poster="/thumbnail.jpg" poster="./img/logo.png"
> >
</video> </video>
</div> </div>

View File

@ -4,7 +4,7 @@ async function setupApp() {
window.app = new Vue({ window.app = new Vue({
el: "#app-container", el: "#app-container",
data: { data: {
streamStatus: "Stream is offline.", // Default state. streamStatus: MESSAGE_OFFLINE, // Default state.
viewerCount: 0, viewerCount: 0,
sessionMaxViewerCount: 0, sessionMaxViewerCount: 0,
overallMaxViewerCount: 0, overallMaxViewerCount: 0,
@ -59,7 +59,7 @@ async function setupApp() {
app.extraUserContent = descriptionHTML; app.extraUserContent = descriptionHTML;
return this; return this;
} catch (error) { } catch (error) {
console.log("Error",error); console.log("Error", error);
} }
} }
@ -67,17 +67,13 @@ var websocketReconnectTimer;
function setupWebsocket() { function setupWebsocket() {
clearTimeout(websocketReconnectTimer); clearTimeout(websocketReconnectTimer);
// Uncomment to point to somewhere other than goth.land var ws = new WebSocket(URL_WEBSOCKET);
const protocol = location.protocol == "https:" ? "wss" : "ws"
// var ws = new WebSocket(protocol + "://" + location.host + "/entry")
var ws = new WebSocket("wss://goth.land/entry")
ws.onmessage = (e) => { ws.onmessage = (e) => {
const model = JSON.parse(e.data) const model = JSON.parse(e.data);
// Ignore non-chat messages (such as keepalive PINGs) // Ignore non-chat messages (such as keepalive PINGs)
if (model.type !== SocketMessageTypes.CHAT) { return; } if (model.type !== SOCKET_MESSAGE_TYPES.CHAT) { return; }
const message = new Message(model); const message = new Message(model);

View File

@ -1,8 +1,3 @@
const SocketMessageTypes = {
CHAT: 'CHAT',
PING: 'PING'
}
class Message { class Message {
constructor(model) { constructor(model) {
this.author = model.author; this.author = model.author;
@ -39,10 +34,6 @@ class Messaging {
this.maxMessageLength = 500; this.maxMessageLength = 500;
this.maxMessageBuffer = 20; this.maxMessageBuffer = 20;
this.keyUsername = 'owncast_username';
this.keyUserAvatar = 'owncast_avatar';
this.keyChatDisplayed = 'owncast_chat';
this.tagAppContainer = document.querySelector('#app-container'); this.tagAppContainer = document.querySelector('#app-container');
this.tagChatToggle = document.querySelector('#chat-toggle'); this.tagChatToggle = document.querySelector('#chat-toggle');
this.tagUserInfoChanger = document.querySelector('#user-info-change'); this.tagUserInfoChanger = document.querySelector('#user-info-change');
@ -89,16 +80,11 @@ class Messaging {
} }
initLocalStates() { initLocalStates() {
this.username = getLocalStorage(this.keyUsername) || generateUsername(); this.username = getLocalStorage(KEY_USERNAME) || generateUsername();
console.log("=== initt state", getLocalStorage(this.keyUserAvatar)) this.imgUsernameAvatar.src = getLocalStorage(KEY_AVATAR) || generateAvatar(this.username);
this.imgUsernameAvatar.src = getLocalStorage(this.keyUserAvatar) || generateAvatar(this.username);
console.log("===",this.imgUsernameAvatar.src)
this.updateUsernameFields(this.username); this.updateUsernameFields(this.username);
this.chatDisplayed = getLocalStorage(this.keyChatDisplayed) || false; this.chatDisplayed = getLocalStorage(KEY_CHAT_DISPLAYED) || false;
this.displayChat(); this.displayChat();
} }
updateUsernameFields(username) { updateUsernameFields(username) {
@ -134,9 +120,9 @@ class Messaging {
handleChatToggle() { handleChatToggle() {
this.chatDisplayed = !this.chatDisplayed; this.chatDisplayed = !this.chatDisplayed;
if (this.chatDisplayed) { if (this.chatDisplayed) {
setLocalStorage(this.keyChatDisplayed, this.chatDisplayed); setLocalStorage(KEY_CHAT_DISPLAYED, this.chatDisplayed);
} else { } else {
clearLocalStorage(this.keyChatDisplayed); clearLocalStorage(KEY_CHAT_DISPLAYED);
} }
this.displayChat(); this.displayChat();
} }
@ -164,8 +150,8 @@ class Messaging {
this.username = newValue; this.username = newValue;
this.updateUsernameFields(newValue); this.updateUsernameFields(newValue);
this.imgUsernameAvatar.src = generateAvatar(newValue); this.imgUsernameAvatar.src = generateAvatar(newValue);
setLocalStorage(this.keyUsername, newValue); setLocalStorage(KEY_USERNAME, newValue);
setLocalStorage(this.keyUserAvatar, this.imgUsernameAvatar.src); setLocalStorage(KEY_AVATAR, this.imgUsernameAvatar.src);
} }
this.handleHideChangeNameForm(); this.handleHideChangeNameForm();
} }

View File

@ -1,11 +1,8 @@
// const streamURL = '/hls/stream.m3u8';
const streamURL = 'https://goth.land/hls/stream.m3u8'; // Uncomment me to point to remote video
// style hackings // style hackings
window.VIDEOJS_NO_DYNAMIC_STYLE = true; window.VIDEOJS_NO_DYNAMIC_STYLE = true;
// Create the player for the first time // Create the player for the first time
const player = videojs('video', null, function () { const player = videojs(VIDEO_ID, null, function () {
getStatus(); getStatus();
setInterval(getStatus, 5000); setInterval(getStatus, 5000);
setupPlayerEventHandlers(); setupPlayerEventHandlers();
@ -18,17 +15,12 @@ player.ready(function () {
function resetPlayer(player) { function resetPlayer(player) {
player.reset(); player.reset();
player.src({ type: 'application/x-mpegURL', src: streamURL }); player.src({ type: 'application/x-mpegURL', src: URL_STREAM });
if (app.isOnline) { setVideoPoster(app.isOnline);
player.poster('/thumbnail.jpg');
} else {
// Change this to some kind of offline image.
player.poster('/img/logo.png');
}
} }
function setupPlayerEventHandlers() { function setupPlayerEventHandlers() {
const player = videojs('video'); const player = videojs(VIDEO_ID);
player.on('error', function (e) { player.on('error', function (e) {
console.log("Player error: ", e); console.log("Player error: ", e);
@ -68,8 +60,8 @@ function setupPlayerEventHandlers() {
function restartPlayer() { function restartPlayer() {
try { try {
console.log('restarting') console.log('restarting', player.src())
const player = videojs('video'); const player = videojs(VIDEO_ID);
player.pause(); player.pause();
player.src(player.src()); // Reload the same video player.src(player.src()); // Reload the same video
player.load(); player.load();
@ -78,4 +70,4 @@ function restartPlayer() {
console.log(e) console.log(e)
} }
} }

View File

@ -1,42 +1,45 @@
var playerRestartTimer; var playerRestartTimer;
async function getStatus() {
const url = "/status";
try { function handleStatus(status) {
const response = await fetch(url); clearTimeout(playerRestartTimer);
const status = await response.json(); if (!app.isOnline && status.online) {
// The stream was offline, but now it's online. Force start of playback after an arbitrary delay to make sure the stream has actual data ready to go.
clearTimeout(playerRestartTimer); playerRestartTimer = setTimeout(restartPlayer, 3000);
if (!app.isOnline && status.online) {
// The stream was offline, but now it's online. Force start of playback after an arbitrary
// delay to make sure the stream has actual data ready to go.
playerRestartTimer = setTimeout(function () {
restartPlayer();
}, 3000);
}
app.streamStatus = status.online
? "Stream is online."
: "Stream is offline."
const player = videojs('video');
if (app.isOnline) {
player.poster('/thumbnail.jpg');
} else {
// Change this to some kind of offline image.
player.poster('/img/logo.png');
}
app.viewerCount = status.viewerCount;
app.sessionMaxViewerCount = status.sessionMaxViewerCount;
app.overallMaxViewerCount = status.overallMaxViewerCount;
app.isOnline = status.online;
} catch (e) {
app.streamStatus = "Stream server is offline."
app.viewerCount = 0
} }
} app.streamStatus = status.online ? MESSAGE_ONLINE : MESSAGE_OFFLINE;
app.viewerCount = status.viewerCount;
app.sessionMaxViewerCount = status.sessionMaxViewerCount;
app.overallMaxViewerCount = status.overallMaxViewerCount;
app.isOnline = status.online;
setVideoPoster(app.isOnline);
}
function handleOffline() {
const player = videojs(VIDEO_ID);
player.poster(POSTER_DEFAULT);
app.streamStatus = MESSAGE_OFFLINE;
app.viewerCount = 0;
}
function getStatus() {
const options = {
// mode: 'no-cors',
}
fetch(URL_STATUS, options)
.then(response => {
if (!response.ok) {
throw new Error(`Network response was not ok ${response.ok}`);
}
return response.json();
})
.then(json => {
handleStatus(json);
})
.catch(error => {
handleOffline();
});
}

View File

@ -1,3 +1,29 @@
const MESSAGE_OFFLINE = 'Stream is offline.';
const MESSAGE_ONLINE = 'Stream is online.';
// const URL_PREFIX = '';
const URL_PREFIX = 'https://goth.land';
const URL_STATUS = `${URL_PREFIX}/status`;
const URL_STREAM = `${URL_PREFIX}/hls/stream.m3u8`;
const URL_WEBSOCKET = 'wss://goth.land/entry';
// const URL_WEBSOCKET = `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}/entry`;
const POSTER_DEFAULT = `${URL_PREFIX}/img/logo.png`;
const POSTER_THUMB = `${URL_PREFIX}/thumbnail.jpg`;
const SOCKET_MESSAGE_TYPES = {
CHAT: 'CHAT',
PING: 'PING'
}
const KEY_USERNAME = 'owncast_username';
const KEY_AVATAR = 'owncast_avatar';
const KEY_CHAT_DISPLAYED = 'owncast_chat';
const VIDEO_ID = 'video';
function getLocalStorage(key) { function getLocalStorage(key) {
try { try {
return localStorage.getItem(key); return localStorage.getItem(key);
@ -87,4 +113,10 @@ function generateAvatar(hash) {
function generateUsername() { function generateUsername() {
return `User ${(Math.floor(Math.random() * 42) + 1)}`; return `User ${(Math.floor(Math.random() * 42) + 1)}`;
} }
function setVideoPoster(online) {
const player = videojs(VIDEO_ID);
const poster = online ? POSTER_THUMB : POSTER_DEFAULT;
player.poster(poster);
}