Skip to content

Commit be538bb

Browse files
committed
Enable API to get current WebSocket connection number
* Background: I want to kill gotty main process to release resources if not one use it, so required a method to get current WebSocket connections number About use it definition: * open in browser even without any input command. Or without any operation * have any operations Signed-off-by: Chenyang Yan <[email protected]>
1 parent bfed9b4 commit be538bb

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ gotty
22
builds
33
js/dist
44
js/node_modules/*
5+
.idea/*

server/handlers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"net/http"
1010
"net/url"
11+
"strconv"
1112
"sync/atomic"
1213

1314
"github.com/gorilla/websocket"
@@ -254,3 +255,10 @@ func (server *Server) titleVariables(order []string, varUnits map[string]map[str
254255

255256
return titleVars
256257
}
258+
259+
func (server *Server) handleWSCount(c *counter) http.HandlerFunc {
260+
return func(w http.ResponseWriter, r *http.Request) {
261+
w.Header().Set("Content-Type", "application/json; chatset=utf-8")
262+
w.Write([]byte(`{"number":` + strconv.Itoa(c.count()) + `}`))
263+
}
264+
}

server/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ func (server *Server) setupHandlers(ctx context.Context, cancel context.CancelFu
211211
siteMux.HandleFunc(pathPrefix+"auth_token.js", server.handleAuthToken)
212212
siteMux.HandleFunc(pathPrefix+"config.js", server.handleConfig)
213213

214+
siteMux.HandleFunc(pathPrefix+"ws_count", server.handleWSCount(counter))
215+
214216
siteHandler := http.Handler(siteMux)
215217

216218
if server.options.EnableBasicAuth {

0 commit comments

Comments
 (0)