Add some comments clarifying some things

This commit is contained in:
Zorchenhimer 2019-03-28 10:18:17 -04:00
parent 9bcbad6a07
commit e879112b1b
2 changed files with 19 additions and 1 deletions

View File

@ -55,6 +55,7 @@ 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")
}
@ -83,6 +84,7 @@ 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")
}
@ -515,6 +517,9 @@ 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.")

15
main.go
View File

@ -80,6 +80,8 @@ func main() {
HandlePlay: handlePlay,
HandlePublish: handlePublish,
}
// Define this here so we can set some timeouts and things.
chatServer := &http.Server{
Addr: addr,
ReadTimeout: 10 * time.Second,
@ -88,8 +90,13 @@ func main() {
}
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() {
@ -111,7 +118,9 @@ func main() {
// Chat and HTTP server
go func() {
// Chat websocket
// 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)
@ -128,6 +137,8 @@ func main() {
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())
@ -138,6 +149,8 @@ func main() {
// 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())