mirror of
https://github.com/WiiLink24/wfc-server.git
synced 2026-09-06 23:55:28 -05:00
feat: implement user counting and return on /online
This commit is contained in:
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
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"wwfc/logging"
|
||||
"wwfc/sake"
|
||||
"wwfc/common"
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
@@ -75,10 +76,9 @@ func (route *Route) Handle() http.Handler {
|
||||
|
||||
// Check for /online
|
||||
if strings.HasPrefix(r.URL.String(), "/online") {
|
||||
logging.Notice("test")
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len("Success")))
|
||||
w.Write([]byte("Success"))
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(strconv.Itoa(common.OnlineUsers))))
|
||||
w.Write([]byte(strconv.Itoa(common.OnlineUsers)))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user