owncast/webroot/js/app.js

110 lines
2.8 KiB
JavaScript
Raw Normal View History

async function setupApp() {
2020-06-18 20:24:54 +03:00
Vue.filter('plural', pluralize);
2020-06-02 23:56:59 +03:00
window.app = new Vue({
el: "#app-container",
2020-06-02 23:56:59 +03:00
data: {
streamStatus: "Stream is offline.", // Default state.
viewerCount: 0,
2020-06-14 11:10:26 +03:00
sessionMaxViewerCount: 0,
overallMaxViewerCount: 0,
messages: [],
2020-06-21 11:31:08 +03:00
extraUserContent: "",
2020-06-19 06:46:00 +03:00
isOnline: false,
2020-06-21 11:31:08 +03:00
// from config
logo: null,
socialHandles: [],
2020-06-21 11:31:08 +03:00
streamerName: "",
summary: "",
tags: [],
title: "",
2020-06-02 23:56:59 +03:00
},
watch: {
messages: {
deep: true,
handler: function (newMessages, oldMessages) {
if (newMessages.length !== oldMessages.length) {
// jump to bottom
jumpToBottom(appMessaging.scrollableMessagesContainer);
}
},
},
},
2020-06-02 23:56:59 +03:00
});
2020-06-15 07:14:42 +03:00
// init messaging interactions
var appMessaging = new Messaging();
appMessaging.init();
2020-06-15 01:18:43 +03:00
const config = await new Config().init();
2020-06-21 11:31:08 +03:00
app.logo = config.logo;
app.socialHandles = config.socialHandles;
2020-06-21 11:31:08 +03:00
app.streamerName = config.name;
app.summary = config.summary && addNewlines(config.summary);
app.tags = config.tags;
app.title = config.title;
// const configFileLocation = "../js/config.json";
try {
const pageContentFile = "../static/content.md"
const response = await fetch(pageContentFile);
const descriptionMarkdown = await response.text()
const descriptionHTML = new showdown.Converter().makeHtml(descriptionMarkdown);
2020-06-21 11:31:08 +03:00
app.extraUserContent = descriptionHTML;
return this;
} catch (error) {
console.log(error);
}
2020-06-02 23:56:59 +03:00
}
2020-06-14 06:15:31 +03:00
var websocketReconnectTimer;
2020-06-02 23:56:59 +03:00
function setupWebsocket() {
clearTimeout(websocketReconnectTimer);
// Uncomment to point to somewhere other than goth.land
const protocol = location.protocol == "https:" ? "wss" : "ws"
var ws = new WebSocket(protocol + "://" + location.host + "/entry")
// var ws = new WebSocket("wss://goth.land/entry")
2020-06-02 23:56:59 +03:00
ws.onmessage = (e) => {
const model = JSON.parse(e.data)
// Ignore non-chat messages (such as keepalive PINGs)
if (model.type !== SocketMessageTypes.CHAT) { return; }
const message = new Message(model);
const existing = this.app.messages.filter(function (item) {
return item.id === message.id;
})
if (existing.length === 0 || !existing) {
this.app.messages = [...this.app.messages, message];
}
2020-06-02 23:56:59 +03:00
}
ws.onclose = (e) => {
// connection closed, discard old websocket and create a new one in 5s
ws = null;
console.log("Websocket closed.")
websocketReconnectTimer = setTimeout(setupWebsocket, 5000);
}
// On ws error just close the socket and let it re-connect again for now.
ws.onerror = (e) => {
console.log("Websocket error: ", e);
ws.close();
}
2020-06-16 03:40:12 +03:00
window.ws = ws;
2020-06-02 23:56:59 +03:00
}
setupApp();
setupWebsocket();
2020-06-02 23:56:59 +03:00