Merge branch 'master' into room-access-restrictions

This is mainly for the server-shutdown merge revert.

# Conflicts:
#	main.go
This commit is contained in:
Zorchenhimer 2019-03-31 00:09:42 -04:00
commit 38f2ca9972
4 changed files with 55 additions and 120 deletions

View File

@ -30,8 +30,6 @@ type ChatRoom struct {
modPasswords []string // single-use mod passwords modPasswords []string // single-use mod passwords
modPasswordsMtx sync.Mutex modPasswordsMtx sync.Mutex
isShutdown bool
} }
//initializing the chatroom //initializing the chatroom
@ -55,11 +53,6 @@ func newChatRoom() (*ChatRoom, error) {
} }
func (cr *ChatRoom) JoinTemp(conn *chatConnection) (string, 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() defer cr.clientsMtx.Unlock()
cr.clientsMtx.Lock() cr.clientsMtx.Lock()
@ -84,11 +77,6 @@ func (cr *ChatRoom) JoinTemp(conn *chatConnection) (string, error) {
//registering a new client //registering a new client
//returns pointer to a Client, or Nil, if the name is already taken //returns pointer to a Client, or Nil, if the name is already taken
func (cr *ChatRoom) Join(name, uid string) (*Client, error) { 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() defer cr.clientsMtx.Unlock()
cr.clientsMtx.Lock() 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) 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")
}

136
main.go
View File

@ -1,13 +1,11 @@
package main package main
import ( import (
"context"
"flag" "flag"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"time"
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
"github.com/nareix/joy4/format" "github.com/nareix/joy4/format"
@ -16,10 +14,9 @@ import (
) )
var ( var (
addr string addr string
sKey string sKey string
stats = newStreamStats() stats = newStreamStats()
chatServer *http.Server
) )
func setupSettings() error { func setupSettings() error {
@ -54,6 +51,9 @@ func main() {
os.Exit(1) os.Exit(1)
} }
exit := make(chan bool)
go handleInterrupt(exit)
// Load emotes before starting server. // Load emotes before starting server.
var err error var err error
if chat, err = newChatRoom(); err != nil { if chat, err = newChatRoom(); err != nil {
@ -77,88 +77,54 @@ func main() {
common.LogInfoln("RoomAccess: ", settings.RoomAccess) common.LogInfoln("RoomAccess: ", settings.RoomAccess)
common.LogInfoln("RoomAccessPin: ", settings.RoomAccessPin) common.LogInfoln("RoomAccessPin: ", settings.RoomAccessPin)
go startServer()
go startRmtpServer()
<-exit
}
func startRmtpServer() {
server := &rtmp.Server{ server := &rtmp.Server{
HandlePlay: handlePlay, HandlePlay: handlePlay,
HandlePublish: handlePublish, HandlePublish: handlePublish,
} }
err := server.ListenAndServe()
// Define this here so we can set some timeouts and things. if err != nil {
chatServer := &http.Server{ // If the server cannot start, don't pretend we can continue.
Addr: addr, panic("Error trying to start rtmp server: " + err.Error())
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
} }
}
chatServer.RegisterOnShutdown(func() { chat.Shutdown() })
// rtmp.Server does not implement .RegisterOnShutdown() func startServer() {
//server.RegisterOnShutdown(func() { common.LogDebugln("server shutdown callback called.") }) // Chat websocket
http.HandleFunc("/ws", wsHandler)
// These have been moved back to annon functitons so I could use http.HandleFunc("/static/js/", wsStaticFiles)
// `server`, `chatServer`, and `exit` in them without needing to http.HandleFunc("/static/css/", wsStaticFiles)
// pass them as parameters. http.HandleFunc("/static/img/", wsImages)
http.HandleFunc("/static/main.wasm", wsWasmFile)
// Signal handler http.HandleFunc("/emotes/", wsEmotes)
exit := make(chan bool) http.HandleFunc("/favicon.ico", wsStaticFiles)
go func() { http.HandleFunc("/chat", handleIndexTemplate)
ch := make(chan os.Signal) http.HandleFunc("/video", handleIndexTemplate)
signal.Notify(ch, os.Interrupt) http.HandleFunc("/help", handleHelpTemplate)
<-ch http.HandleFunc("/pin", handlePin)
common.LogInfoln("Closing server")
if settings.StreamStats { http.HandleFunc("/", handleDefault)
stats.Print()
} err := http.ListenAndServe(addr, nil)
if err != nil {
if err := chatServer.Shutdown(context.Background()); err != nil { // If the server cannot start, don't pretend we can continue.
common.LogErrorf("Error shutting down chat server: %v", err) panic("Error trying to start chat/http server: " + err.Error())
} }
}
common.LogInfoln("Shutdown() sent. Sending exit.")
exit <- true func handleInterrupt(exit chan bool) {
}() ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt)
// Chat and HTTP server <-ch
go func() { common.LogInfoln("Closing server")
// Use a ServeMux here instead of the default, global, if settings.StreamStats {
// http handler. It's a good idea when we're starting more stats.Print()
// than one server. }
mux := http.NewServeMux() exit <- true
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("/pin", handlePin)
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
} }

View File

@ -1,3 +1,5 @@
// +build js,wasm
package main package main
import ( import (

View File

@ -1,3 +1,5 @@
// +build js,wasm
package main package main
import ( import (