Show viewer count and reconnect to websocket
This commit is contained in:
parent
380dad2b87
commit
f83fccfa89
17
main.go
17
main.go
@ -11,6 +11,8 @@ import (
|
|||||||
|
|
||||||
var ipfs icore.CoreAPI
|
var ipfs icore.CoreAPI
|
||||||
var configuration = getConfig()
|
var configuration = getConfig()
|
||||||
|
var server *Server
|
||||||
|
|
||||||
var online = false
|
var online = false
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -46,7 +48,7 @@ func startChatServer() {
|
|||||||
// log.SetFlags(log.Lshortfile)
|
// log.SetFlags(log.Lshortfile)
|
||||||
|
|
||||||
// websocket server
|
// websocket server
|
||||||
server := NewServer("/entry")
|
server = NewServer("/entry")
|
||||||
go server.Listen()
|
go server.Listen()
|
||||||
|
|
||||||
// static files
|
// static files
|
||||||
@ -60,15 +62,12 @@ func startChatServer() {
|
|||||||
|
|
||||||
func getStatus(w http.ResponseWriter, r *http.Request) {
|
func getStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
status := Status{
|
status := Status{
|
||||||
Online: online,
|
Online: online,
|
||||||
|
ViewerCount: server.ClientCount(),
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(status)
|
json.NewEncoder(w).Encode(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Status struct {
|
|
||||||
Online bool `json:"online"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func streamConnected() {
|
func streamConnected() {
|
||||||
online = true
|
online = true
|
||||||
}
|
}
|
||||||
@ -76,3 +75,9 @@ func streamConnected() {
|
|||||||
func streamDisconnected() {
|
func streamDisconnected() {
|
||||||
online = false
|
online = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func viewerAdded() {
|
||||||
|
}
|
||||||
|
|
||||||
|
func viewerRemoved() {
|
||||||
|
}
|
||||||
|
@ -41,6 +41,10 @@ func NewServer(pattern string) *Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) ClientCount() int {
|
||||||
|
return len(s.clients)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) Add(c *Client) {
|
func (s *Server) Add(c *Client) {
|
||||||
s.addCh <- c
|
s.addCh <- c
|
||||||
}
|
}
|
||||||
@ -99,12 +103,14 @@ func (s *Server) Listen() {
|
|||||||
log.Println("Added new client")
|
log.Println("Added new client")
|
||||||
s.clients[c.id] = c
|
s.clients[c.id] = c
|
||||||
log.Println("Now", len(s.clients), "clients connected.")
|
log.Println("Now", len(s.clients), "clients connected.")
|
||||||
|
viewerAdded()
|
||||||
s.sendPastMessages(c)
|
s.sendPastMessages(c)
|
||||||
|
|
||||||
// del a client
|
// del a client
|
||||||
case c := <-s.delCh:
|
case c := <-s.delCh:
|
||||||
log.Println("Delete client")
|
log.Println("Delete client")
|
||||||
delete(s.clients, c.id)
|
delete(s.clients, c.id)
|
||||||
|
viewerRemoved()
|
||||||
|
|
||||||
// broadcast message for all clients
|
// broadcast message for all clients
|
||||||
case msg := <-s.sendAllCh:
|
case msg := <-s.sendAllCh:
|
||||||
|
6
status.go
Normal file
6
status.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type Status struct {
|
||||||
|
Online bool `json:"online"`
|
||||||
|
ViewerCount int `json:"viewerCount"`
|
||||||
|
}
|
@ -25,7 +25,7 @@
|
|||||||
style="width: 100%;"
|
style="width: 100%;"
|
||||||
></video>
|
></video>
|
||||||
<div id="app">
|
<div id="app">
|
||||||
{{ streamStatus }}
|
{{ streamStatus }} {{ viewerCount }} {{ 'viewer' | plural(viewerCount) }}.
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -1,8 +1,17 @@
|
|||||||
function setupApp() {
|
function setupApp() {
|
||||||
|
Vue.filter('plural', function (string, count) {
|
||||||
|
if (count === 1) {
|
||||||
|
return string
|
||||||
|
} else {
|
||||||
|
return string + "s"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
window.app = new Vue({
|
window.app = new Vue({
|
||||||
el: "#app",
|
el: "#app",
|
||||||
data: {
|
data: {
|
||||||
streamStatus: "",
|
streamStatus: "",
|
||||||
|
viewerCount: 0,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -44,9 +53,12 @@ async function getStatus() {
|
|||||||
app.streamStatus = status.online
|
app.streamStatus = status.online
|
||||||
? "Stream is online."
|
? "Stream is online."
|
||||||
: "Stream is offline."
|
: "Stream is offline."
|
||||||
|
|
||||||
|
app.viewerCount = status.viewerCount
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
app.streamStatus = "Stream server is offline."
|
app.streamStatus = "Stream server is offline."
|
||||||
|
app.viewerCount = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -60,14 +72,27 @@ function setupWebsocket() {
|
|||||||
this.messagesContainer.messages.push(message)
|
this.messagesContainer.messages.push(message)
|
||||||
scrollSmoothToBottom("messages-container")
|
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
|
window.ws = ws
|
||||||
}
|
}
|
||||||
|
|
||||||
setupApp()
|
setupApp()
|
||||||
getStatus();
|
getStatus()
|
||||||
setupWebsocket()
|
setupWebsocket()
|
||||||
setInterval(getStatus, 5000)
|
setInterval(getStatus, 5000)
|
||||||
|
|
||||||
|
// HLS Video setup
|
||||||
var video = document.getElementById("video")
|
var video = document.getElementById("video")
|
||||||
var videoSrc = "hls/stream.m3u8"
|
var videoSrc = "hls/stream.m3u8"
|
||||||
if (Hls.isSupported()) {
|
if (Hls.isSupported()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user