From 358a2c068d92c61ceca4c3c2c54aa350b46d1847 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sat, 30 Mar 2019 23:58:26 -0400 Subject: [PATCH 1/2] Add build constraints to wasm source files Add constraints to the wasm's source files so they're only built for the js/wasm target. --- wasm/main.go | 2 ++ wasm/suggestions.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/wasm/main.go b/wasm/main.go index 7092d2a..77231ac 100644 --- a/wasm/main.go +++ b/wasm/main.go @@ -1,3 +1,5 @@ +// +build js,wasm + package main import ( diff --git a/wasm/suggestions.go b/wasm/suggestions.go index ecfdf94..14eb8eb 100644 --- a/wasm/suggestions.go +++ b/wasm/suggestions.go @@ -1,3 +1,5 @@ +// +build js,wasm + package main import ( From 9c559c38180bd97518f07aba1fdb47b340d4890f Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sun, 31 Mar 2019 00:07:19 -0400 Subject: [PATCH 2/2] Revert "Merge branch 'server-shutdown'" This reverts commit 13d74faf01986ecf6d8a35e564bcb24f72fac72d, reversing changes made to ec0c6752bc236ada82d4c6d4bb957b6f7b7f8f75. These changes completely broke the RTMP stream. Reverting until I figure out why. --- chatroom.go | 35 -------------- main.go | 134 ++++++++++++++++++++-------------------------------- 2 files changed, 50 insertions(+), 119 deletions(-) diff --git a/chatroom.go b/chatroom.go index e084b5e..dc5fe56 100644 --- a/chatroom.go +++ b/chatroom.go @@ -30,8 +30,6 @@ type ChatRoom struct { modPasswords []string // single-use mod passwords modPasswordsMtx sync.Mutex - - isShutdown bool } //initializing the chatroom @@ -55,11 +53,6 @@ func newChatRoom() (*ChatRoom, error) { } func (cr *ChatRoom) JoinTemp(conn *chatConnection) (string, error) { - // Don't allow new joins when the server is closing. - if cr.isShutdown { - return "", fmt.Errorf("Server is shutting down") - } - defer cr.clientsMtx.Unlock() cr.clientsMtx.Lock() @@ -84,11 +77,6 @@ func (cr *ChatRoom) JoinTemp(conn *chatConnection) (string, error) { //registering a new client //returns pointer to a Client, or Nil, if the name is already taken func (cr *ChatRoom) Join(name, uid string) (*Client, error) { - // Don't allow new joins when the server is closing. - if cr.isShutdown { - return nil, fmt.Errorf("Server is shutting down") - } - defer cr.clientsMtx.Unlock() cr.clientsMtx.Lock() @@ -512,26 +500,3 @@ func (cr *ChatRoom) changeName(oldName, newName string, forced bool) error { return fmt.Errorf("Client not found with name %q", oldName) } - -// Shutdown the chatroom. First, dissallow new joins by setting -// isShutdown, then close each client connection. This would be -// a good place to put a final command that gets sent to the client. -func (cr *ChatRoom) Shutdown() { - cr.isShutdown = true - common.LogInfoln("ChatRoom is shutting down.") - - cr.clientsMtx.Lock() - defer cr.clientsMtx.Unlock() - - for uuid, client := range cr.clients { - common.LogDebugf("Closing connection for %s", uuid) - client.conn.Close() - } - - for uuid, conn := range cr.tempConn { - common.LogDebugf("Closing connection for temp %s", uuid) - conn.Close() - } - - common.LogInfoln("ChatRoom Shutdown() finished") -} diff --git a/main.go b/main.go index 1a448f4..f73900a 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,11 @@ package main import ( - "context" "flag" "fmt" "net/http" "os" "os/signal" - "time" "github.com/nareix/joy4/format" "github.com/nareix/joy4/format/rtmp" @@ -15,10 +13,9 @@ import ( ) var ( - addr string - sKey string - stats = newStreamStats() - chatServer *http.Server + addr string + sKey string + stats = newStreamStats() ) func setupSettings() error { @@ -51,6 +48,9 @@ func main() { os.Exit(1) } + exit := make(chan bool) + go handleInterrupt(exit) + // Load emotes before starting server. var err error if chat, err = newChatRoom(); err != nil { @@ -72,87 +72,53 @@ func main() { common.LogInfoln("Admin password: ", settings.AdminPassword) common.LogInfoln("Listen and serve ", addr) + go startServer() + go startRmtpServer() + + <-exit +} + +func startRmtpServer() { server := &rtmp.Server{ HandlePlay: handlePlay, HandlePublish: handlePublish, } - - // Define this here so we can set some timeouts and things. - chatServer := &http.Server{ - Addr: addr, - ReadTimeout: 10 * time.Second, - WriteTimeout: 10 * time.Second, - MaxHeaderBytes: 1 << 20, + err := server.ListenAndServe() + if err != nil { + // If the server cannot start, don't pretend we can continue. + panic("Error trying to start rtmp server: " + err.Error()) } - - chatServer.RegisterOnShutdown(func() { chat.Shutdown() }) - // rtmp.Server does not implement .RegisterOnShutdown() - //server.RegisterOnShutdown(func() { common.LogDebugln("server shutdown callback called.") }) - - // These have been moved back to annon functitons so I could use - // `server`, `chatServer`, and `exit` in them without needing to - // pass them as parameters. - - // Signal handler - exit := make(chan bool) - go func() { - ch := make(chan os.Signal) - signal.Notify(ch, os.Interrupt) - <-ch - common.LogInfoln("Closing server") - if settings.StreamStats { - stats.Print() - } - - if err := chatServer.Shutdown(context.Background()); err != nil { - common.LogErrorf("Error shutting down chat server: %v", err) - } - - common.LogInfoln("Shutdown() sent. Sending exit.") - exit <- true - }() - - // Chat and HTTP server - go func() { - // Use a ServeMux here instead of the default, global, - // http handler. It's a good idea when we're starting more - // than one server. - mux := http.NewServeMux() - mux.HandleFunc("/ws", wsHandler) - mux.HandleFunc("/static/js/", wsStaticFiles) - mux.HandleFunc("/static/css/", wsStaticFiles) - mux.HandleFunc("/static/img/", wsImages) - mux.HandleFunc("/static/main.wasm", wsWasmFile) - mux.HandleFunc("/emotes/", wsEmotes) - mux.HandleFunc("/favicon.ico", wsStaticFiles) - mux.HandleFunc("/chat", handleIndexTemplate) - mux.HandleFunc("/video", handleIndexTemplate) - mux.HandleFunc("/help", handleHelpTemplate) - - mux.HandleFunc("/", handleDefault) - - chatServer.Handler = mux - err := chatServer.ListenAndServe() - // http.ErrServerClosed is returned when server.Shuddown() - // is called. - if err != http.ErrServerClosed { - // If the server cannot start, don't pretend we can continue. - panic("Error trying to start chat/http server: " + err.Error()) - } - common.LogDebugln("ChatServer closed.") - }() - - // RTMP server - go func() { - err := server.ListenAndServe() - // http.ErrServerClosed is returned when server.Shuddown() - // is called. - if err != http.ErrServerClosed { - // If the server cannot start, don't pretend we can continue. - panic("Error trying to start rtmp server: " + err.Error()) - } - common.LogDebugln("RTMP server closed.") - }() - - <-exit +} + +func startServer() { + // Chat websocket + http.HandleFunc("/ws", wsHandler) + http.HandleFunc("/static/js/", wsStaticFiles) + http.HandleFunc("/static/css/", wsStaticFiles) + http.HandleFunc("/static/img/", wsImages) + http.HandleFunc("/static/main.wasm", wsWasmFile) + http.HandleFunc("/emotes/", wsEmotes) + http.HandleFunc("/favicon.ico", wsStaticFiles) + http.HandleFunc("/chat", handleIndexTemplate) + http.HandleFunc("/video", handleIndexTemplate) + http.HandleFunc("/help", handleHelpTemplate) + + http.HandleFunc("/", handleDefault) + + err := http.ListenAndServe(addr, nil) + if err != nil { + // If the server cannot start, don't pretend we can continue. + panic("Error trying to start chat/http server: " + err.Error()) + } +} + +func handleInterrupt(exit chan bool) { + ch := make(chan os.Signal) + signal.Notify(ch, os.Interrupt) + <-ch + common.LogInfoln("Closing server") + if settings.StreamStats { + stats.Print() + } + exit <- true }