1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2025-07-05 23:03:53 +02:00

Use messaging in web package

This commit is contained in:
Alexandre Iooss
2020-10-17 12:38:18 +02:00
parent 68d4ad8aee
commit b8ee60ce9f
3 changed files with 28 additions and 14 deletions

View File

@ -13,14 +13,12 @@ import (
"github.com/markbates/pkger"
"gitlab.crans.org/nounous/ghostream/internal/monitoring"
"gitlab.crans.org/nounous/ghostream/stream/srt"
"gitlab.crans.org/nounous/ghostream/stream/telnet"
"gitlab.crans.org/nounous/ghostream/stream/webrtc"
)
var (
// Precompile regex
validPath = regexp.MustCompile("^/[a-z0-9_-]*$")
validPath = regexp.MustCompile("^/[a-z0-9@_-]*$")
)
// Handle WebRTC session description exchange via POST
@ -152,14 +150,19 @@ func staticHandler() http.Handler {
}
func statisticsHandler(w http.ResponseWriter, r *http.Request) {
// Display connected users stats, from WebRTC or streaming directly from a video player
streamID := strings.Replace(r.URL.Path[7:], "/", "", -1)
name := strings.Replace(r.URL.Path[7:], "/", "", -1)
userCount := 0
// Get requested stream
stream, ok := streams[name]
if ok {
// Get number of output channels
userCount = stream.Count()
}
// Display connected users statistics
enc := json.NewEncoder(w)
err := enc.Encode(struct {
ConnectedViewers int
}{webrtc.GetNumberConnectedSessions(streamID) +
srt.GetNumberConnectedSessions(streamID) +
telnet.GetNumberConnectedSessions(streamID)})
err := enc.Encode(struct{ ConnectedViewers int }{userCount})
if err != nil {
http.Error(w, "Failed to generate JSON.", http.StatusInternalServerError)
log.Printf("Failed to generate JSON: %s", err)