add newWebsocketClient()
This commit is contained in:
parent
191a90cf8c
commit
478ff99fdd
1 changed files with 18 additions and 5 deletions
|
@ -20,6 +20,22 @@ var upgrader = websocket.Upgrader{
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// initialize a new WebsocketClient
|
||||||
|
func newWebsocketClient(ws *websocket.Conn, address string) models.WebsocketClient {
|
||||||
|
wsclient := models.WebsocketClient{
|
||||||
|
Conn: ws,
|
||||||
|
LastPong: time.Now(),
|
||||||
|
RealIP: address,
|
||||||
|
}
|
||||||
|
|
||||||
|
wsclient.Conn.SetPongHandler(func(appData string) error {
|
||||||
|
wsclient.LastPong = time.Now()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return wsclient
|
||||||
|
}
|
||||||
|
|
||||||
// Upgrades HTTP requests to WebSocket connections and manages the client lifecycle
|
// Upgrades HTTP requests to WebSocket connections and manages the client lifecycle
|
||||||
func HandleConnection(w http.ResponseWriter, r *http.Request) {
|
func HandleConnection(w http.ResponseWriter, r *http.Request) {
|
||||||
// Upgrade initial GET request to a WebSocket
|
// Upgrade initial GET request to a WebSocket
|
||||||
|
@ -31,11 +47,8 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) {
|
||||||
defer ws.Close()
|
defer ws.Close()
|
||||||
|
|
||||||
// Register the new client
|
// Register the new client
|
||||||
client := models.WebsocketClient{
|
client := newWebsocketClient(ws, r.RemoteAddr)
|
||||||
Conn: ws,
|
|
||||||
LastPong: time.Now(),
|
|
||||||
RealIP: r.RemoteAddr,
|
|
||||||
}
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
Clients[ws.LocalAddr()] = &client
|
Clients[ws.LocalAddr()] = &client
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
Loading…
Reference in a new issue