Merge pull request #5 from WiiLink24/online-count

feat: online count
This commit is contained in:
hytracer
2023-11-22 20:29:18 +01:00
committed by GitHub
4 changed files with 34 additions and 0 deletions

12
common/stats.go Normal file
View File

@@ -0,0 +1,12 @@
package common
var OnlineUsers int
func init(){
OnlineUsers = 0
}
func OnlineStatUpdate(t int) {
OnlineUsers += t
}

View File

@@ -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
View 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)))
}

View File

@@ -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")