From f83fccfa89f81ee2badf7ec0ee64f24983ed9cd9 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Tue, 2 Jun 2020 17:35:49 -0700 Subject: [PATCH] Show viewer count and reconnect to websocket --- main.go | 17 +++++++++++------ server.go | 6 ++++++ status.go | 6 ++++++ webroot/index.html | 2 +- webroot/js/app.js | 27 ++++++++++++++++++++++++++- 5 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 status.go diff --git a/main.go b/main.go index b6deef331..5fc3e8567 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,8 @@ import ( var ipfs icore.CoreAPI var configuration = getConfig() +var server *Server + var online = false func main() { @@ -46,7 +48,7 @@ func startChatServer() { // log.SetFlags(log.Lshortfile) // websocket server - server := NewServer("/entry") + server = NewServer("/entry") go server.Listen() // static files @@ -60,15 +62,12 @@ func startChatServer() { func getStatus(w http.ResponseWriter, r *http.Request) { status := Status{ - Online: online, + Online: online, + ViewerCount: server.ClientCount(), } json.NewEncoder(w).Encode(status) } -type Status struct { - Online bool `json:"online"` -} - func streamConnected() { online = true } @@ -76,3 +75,9 @@ func streamConnected() { func streamDisconnected() { online = false } + +func viewerAdded() { +} + +func viewerRemoved() { +} diff --git a/server.go b/server.go index 398f7c0b6..bb6e6ccf9 100644 --- a/server.go +++ b/server.go @@ -41,6 +41,10 @@ func NewServer(pattern string) *Server { } } +func (s *Server) ClientCount() int { + return len(s.clients) +} + func (s *Server) Add(c *Client) { s.addCh <- c } @@ -99,12 +103,14 @@ func (s *Server) Listen() { log.Println("Added new client") s.clients[c.id] = c log.Println("Now", len(s.clients), "clients connected.") + viewerAdded() s.sendPastMessages(c) // del a client case c := <-s.delCh: log.Println("Delete client") delete(s.clients, c.id) + viewerRemoved() // broadcast message for all clients case msg := <-s.sendAllCh: diff --git a/status.go b/status.go new file mode 100644 index 000000000..cea323b98 --- /dev/null +++ b/status.go @@ -0,0 +1,6 @@ +package main + +type Status struct { + Online bool `json:"online"` + ViewerCount int `json:"viewerCount"` +} diff --git a/webroot/index.html b/webroot/index.html index e3ef40aca..8a37cb987 100644 --- a/webroot/index.html +++ b/webroot/index.html @@ -25,7 +25,7 @@ style="width: 100%;" >
- {{ streamStatus }} + {{ streamStatus }} {{ viewerCount }} {{ 'viewer' | plural(viewerCount) }}.
diff --git a/webroot/js/app.js b/webroot/js/app.js index 3e2c3b772..900c06c22 100644 --- a/webroot/js/app.js +++ b/webroot/js/app.js @@ -1,8 +1,17 @@ function setupApp() { + Vue.filter('plural', function (string, count) { + if (count === 1) { + return string + } else { + return string + "s" + } + }) + window.app = new Vue({ el: "#app", data: { streamStatus: "", + viewerCount: 0, }, }); @@ -44,9 +53,12 @@ async function getStatus() { app.streamStatus = status.online ? "Stream is online." : "Stream is offline." + + app.viewerCount = status.viewerCount } catch (e) { app.streamStatus = "Stream server is offline." + app.viewerCount = 0 } } @@ -60,14 +72,27 @@ function setupWebsocket() { this.messagesContainer.messages.push(message) scrollSmoothToBottom("messages-container") } + + ws.onclose = (e) => { + // connection closed, discard old websocket and create a new one in 5s + ws = null + setTimeout(setupWebsocket, 5000) + } + + ws.onerror = (e) => { + console.log("ERROR") + setupWebsocket() + } + window.ws = ws } setupApp() -getStatus(); +getStatus() setupWebsocket() setInterval(getStatus, 5000) +// HLS Video setup var video = document.getElementById("video") var videoSrc = "hls/stream.m3u8" if (Hls.isSupported()) {