2019-03-16 18:44:18 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-03-20 21:57:29 +01:00
|
|
|
"net"
|
2019-03-16 18:44:18 +01:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
type chatConnection struct {
|
|
|
|
*websocket.Conn
|
2019-03-20 21:57:29 +01:00
|
|
|
mutex sync.Mutex
|
|
|
|
forwardedFor string
|
2019-03-16 18:44:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *chatConnection) ReadData(data interface{}) error {
|
|
|
|
defer cc.mutex.Unlock()
|
|
|
|
cc.mutex.Lock()
|
2019-03-21 21:20:50 +01:00
|
|
|
stats.msgInInc()
|
2019-03-16 18:44:18 +01:00
|
|
|
return cc.ReadJSON(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *chatConnection) WriteData(data interface{}) error {
|
|
|
|
defer cc.mutex.Unlock()
|
|
|
|
cc.mutex.Lock()
|
2019-03-21 21:20:50 +01:00
|
|
|
stats.msgOutInc()
|
2019-03-16 18:44:18 +01:00
|
|
|
return cc.WriteJSON(data)
|
|
|
|
}
|
2019-03-20 21:57:29 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|