Add stream stats, minor changes in main, and adding ctrl+c handling
Stream statistics will now print when enabled in settings.json. There is messages in, messages out, and total time running. The main function was changed so the server setups for the main and rtmp are in separate functions, and both are started with their own goroutine. OS Interrupt catching was added, and if stream stats is true, the stats will be printed.
This commit is contained in:
parent
bc983d9ce3
commit
fac6e285bd
|
@ -16,14 +16,14 @@ type chatConnection struct {
|
||||||
func (cc *chatConnection) ReadData(data interface{}) error {
|
func (cc *chatConnection) ReadData(data interface{}) error {
|
||||||
defer cc.mutex.Unlock()
|
defer cc.mutex.Unlock()
|
||||||
cc.mutex.Lock()
|
cc.mutex.Lock()
|
||||||
|
stats.msgInInc()
|
||||||
return cc.ReadJSON(data)
|
return cc.ReadJSON(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cc *chatConnection) WriteData(data interface{}) error {
|
func (cc *chatConnection) WriteData(data interface{}) error {
|
||||||
defer cc.mutex.Unlock()
|
defer cc.mutex.Unlock()
|
||||||
cc.mutex.Lock()
|
cc.mutex.Lock()
|
||||||
|
stats.msgOutInc()
|
||||||
return cc.WriteJSON(data)
|
return cc.WriteJSON(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
93
main.go
93
main.go
|
@ -5,27 +5,72 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
"github.com/nareix/joy4/format"
|
"github.com/nareix/joy4/format"
|
||||||
"github.com/nareix/joy4/format/rtmp"
|
"github.com/nareix/joy4/format/rtmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
addr = flag.String("l", ":8089", "host:port of the MovieNight")
|
addr string
|
||||||
sKey = flag.String("k", "", "Stream key, to protect your stream")
|
sKey string
|
||||||
|
stats streamStats
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
format.RegisterAll()
|
format.RegisterAll()
|
||||||
|
|
||||||
|
flag.StringVar(&addr, "l", ":8089", "host:port of the MovieNight")
|
||||||
|
flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream")
|
||||||
|
|
||||||
|
stats = newStreamStats()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
server := &rtmp.Server{}
|
|
||||||
|
|
||||||
server.HandlePlay = handlePlay
|
exit := make(chan bool)
|
||||||
server.HandlePublish = handlePublish
|
go handleInterrupt(exit)
|
||||||
|
|
||||||
|
// Load emotes before starting server.
|
||||||
|
var err error
|
||||||
|
if chat, err = newChatRoom(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if addr != "" {
|
||||||
|
addr = settings.ListenAddress
|
||||||
|
}
|
||||||
|
|
||||||
|
// A stream key was passed on the command line. Use it, but don't save
|
||||||
|
// it over the stream key in the settings.json file.
|
||||||
|
if sKey != "" {
|
||||||
|
settings.SetTempKey(sKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Stream key: ", settings.GetStreamKey())
|
||||||
|
fmt.Println("Admin password: ", settings.AdminPassword)
|
||||||
|
fmt.Println("Listen and serve ", addr)
|
||||||
|
|
||||||
|
go startServer()
|
||||||
|
go startRmptServer()
|
||||||
|
|
||||||
|
<-exit
|
||||||
|
}
|
||||||
|
|
||||||
|
func startRmptServer() {
|
||||||
|
server := &rtmp.Server{
|
||||||
|
HandlePlay: handlePlay,
|
||||||
|
HandlePublish: handlePublish,
|
||||||
|
}
|
||||||
|
err := server.ListenAndServe()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error trying to start server: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func startServer() {
|
||||||
// Chat websocket
|
// Chat websocket
|
||||||
http.HandleFunc("/ws", wsHandler)
|
http.HandleFunc("/ws", wsHandler)
|
||||||
http.HandleFunc("/static/js/", wsStaticFiles)
|
http.HandleFunc("/static/js/", wsStaticFiles)
|
||||||
|
@ -40,29 +85,19 @@ func main() {
|
||||||
|
|
||||||
http.HandleFunc("/", handleDefault)
|
http.HandleFunc("/", handleDefault)
|
||||||
|
|
||||||
address := settings.ListenAddress
|
err := http.ListenAndServe(addr, nil)
|
||||||
if addr != nil && len(*addr) != 0 {
|
if err != nil {
|
||||||
address = *addr
|
fmt.Printf("Error trying to start rmtp server: %v\n", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Load emotes before starting server.
|
|
||||||
var err error
|
func handleInterrupt(exit chan bool) {
|
||||||
if chat, err = newChatRoom(); err != nil {
|
ch := make(chan os.Signal)
|
||||||
fmt.Println(err)
|
signal.Notify(ch, os.Interrupt)
|
||||||
os.Exit(1)
|
<-ch
|
||||||
}
|
fmt.Println("Closing server")
|
||||||
|
if settings.StreamStats {
|
||||||
// A stream key was passed on the command line. Use it, but don't save
|
stats.Print()
|
||||||
// it over the stream key in the settings.json file.
|
}
|
||||||
if sKey != nil && len(*sKey) != 0 {
|
exit <- true
|
||||||
settings.SetTempKey(*sKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Stream key: ", settings.GetStreamKey())
|
|
||||||
fmt.Println("Admin password: ", settings.AdminPassword)
|
|
||||||
|
|
||||||
go http.ListenAndServe(address, nil)
|
|
||||||
fmt.Println("Listen and serve ", *addr)
|
|
||||||
|
|
||||||
server.ListenAndServe()
|
|
||||||
}
|
}
|
||||||
|
|
10
settings.go
10
settings.go
|
@ -15,14 +15,18 @@ var settings *Settings
|
||||||
var settingsMtx sync.Mutex
|
var settingsMtx sync.Mutex
|
||||||
|
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
filename string
|
// Non-Saved settings
|
||||||
cmdLineKey string // stream key from the command line
|
filename string
|
||||||
|
cmdLineKey string // stream key from the command line
|
||||||
|
|
||||||
|
// Saved settings
|
||||||
|
StreamStats bool
|
||||||
MaxMessageCount int
|
MaxMessageCount int
|
||||||
TitleLength int // maximum length of the title that can be set with the /playing
|
TitleLength int // maximum length of the title that can be set with the /playing
|
||||||
AdminPassword string
|
AdminPassword string
|
||||||
Bans []BanInfo
|
|
||||||
StreamKey string
|
StreamKey string
|
||||||
ListenAddress string
|
ListenAddress string
|
||||||
|
Bans []BanInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
type BanInfo struct {
|
type BanInfo struct {
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type streamStats struct {
|
||||||
|
messageIn int
|
||||||
|
messageOut int
|
||||||
|
start time.Time
|
||||||
|
mutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func newStreamStats() streamStats {
|
||||||
|
return streamStats{start: time.Now()}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *streamStats) msgInInc() {
|
||||||
|
s.mutex.Lock()
|
||||||
|
s.messageIn++
|
||||||
|
s.mutex.Unlock()
|
||||||
|
}
|
||||||
|
func (s *streamStats) msgOutInc() {
|
||||||
|
s.mutex.Lock()
|
||||||
|
s.messageOut++
|
||||||
|
s.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *streamStats) Print() {
|
||||||
|
fmt.Printf("Messages In: %d\n", s.messageIn)
|
||||||
|
fmt.Printf("Messages Out: %d\n", s.messageOut)
|
||||||
|
fmt.Printf("Total Time: %s\n", time.Since(s.start))
|
||||||
|
}
|
Loading…
Reference in New Issue