mirror of
https://github.com/WiiLink24/wfc-server.git
synced 2026-08-27 19:14:02 -05:00
12
common/stats.go
Normal file
12
common/stats.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package common
|
||||
|
||||
var OnlineUsers int
|
||||
|
||||
func init(){
|
||||
OnlineUsers = 0
|
||||
}
|
||||
|
||||
func OnlineStatUpdate(t int) {
|
||||
OnlineUsers += t
|
||||
}
|
||||
|
||||
@@ -122,6 +122,7 @@ func handleRequest(conn net.Conn) {
|
||||
conn.Write([]byte(payload))
|
||||
|
||||
logging.Notice(session.ModuleName, "Connection established from", conn.RemoteAddr())
|
||||
common.OnlineStatUpdate(1)
|
||||
|
||||
// Here we go into the listening loop
|
||||
for {
|
||||
@@ -132,10 +133,12 @@ func handleRequest(conn net.Conn) {
|
||||
if errors.Is(err, io.EOF) {
|
||||
// Client closed connection, terminate.
|
||||
logging.Notice(session.ModuleName, "Client closed connection")
|
||||
common.OnlineStatUpdate(-1)
|
||||
return
|
||||
}
|
||||
|
||||
logging.Error(session.ModuleName, "Connection lost")
|
||||
common.OnlineStatUpdate(-1)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
13
nas/online.go
Normal file
13
nas/online.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package nas
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"wwfc/common"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func returnOnlineStats(w http.ResponseWriter) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(strconv.Itoa(common.OnlineUsers))))
|
||||
w.Write([]byte(strconv.Itoa(common.OnlineUsers)))
|
||||
}
|
||||
@@ -73,6 +73,12 @@ func (route *Route) Handle() http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
// Check for /online
|
||||
if strings.HasPrefix(r.URL.String(), "/online") {
|
||||
returnOnlineStats(w)
|
||||
return
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
logging.Error(moduleName, "Failed to parse form")
|
||||
|
||||
Reference in New Issue
Block a user