MovieNight/connection.go
Zorchenhimer fdbf39f00c Add some logging
Functions added:
- LogErrorf()
- LogErrorln()
- LogChatf()
- LogChatln()
- LogInfof()
- LogInfoln()
- LogDebugf()
- LogDebugln()
- LogDevf()
- LogDevln()

New settings configure the logging: LogLevel and LogFile.  LogLevel can
be set to one of: error, chat, info, or debug and will default to
error.  LogFile is an optional file to write to.  Providing a file will
not prevent output in the console.

LogDevf() and LogDevln() only compile when the "dev" flag passed to
build.  This will cause Travis-CI to fail the build if it finds any
calls to either function.
2019-03-24 18:58:59 -04:00

53 lines
1002 B
Go

package main
import (
"fmt"
"net"
"sync"
"github.com/gorilla/websocket"
"github.com/zorchenhimer/MovieNight/common"
)
type chatConnection struct {
*websocket.Conn
mutex sync.RWMutex
forwardedFor string
clientName string
}
func (cc *chatConnection) ReadData(data interface{}) error {
cc.mutex.RLock()
defer cc.mutex.RUnlock()
stats.msgInInc()
return cc.ReadJSON(data)
}
func (cc *chatConnection) WriteData(data interface{}) error {
cc.mutex.Lock()
defer cc.mutex.Unlock()
stats.msgOutInc()
err := cc.WriteJSON(data)
if err != nil {
if operr, ok := err.(*net.OpError); ok {
common.LogDebugln("OpError: " + operr.Err.Error())
}
return fmt.Errorf("Error writing data to %s %s: %v", cc.clientName, cc.Host(), err)
}
return nil
}
func (cc *chatConnection) Host() string {
if len(cc.forwardedFor) > 0 {
return cc.forwardedFor
}
host, _, err := net.SplitHostPort(cc.RemoteAddr().String())
if err != nil {
return cc.RemoteAddr().String()
}
return host
}