Skip to content

Commit

Permalink
Merge 9439737 into 41ad1d4
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoshinonyaruko authored Jul 13, 2024
2 parents 41ad1d4 + 9439737 commit 87dae2a
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func WsHandler(w http.ResponseWriter, r *http.Request, config *config.Config) {
messageType, p, err := conn.ReadMessage()
if err != nil {
log.Printf("Error reading message: %v", err)
removeFromClients(conn) // Remove the faulty connection
break
}

Expand All @@ -128,6 +129,19 @@ func WsHandler(w http.ResponseWriter, r *http.Request, config *config.Config) {
}
}

func removeFromClients(conn *websocket.Conn) {
lock.Lock()
defer lock.Unlock()
for i, client := range clients {
if client.Conn == conn {
// Remove the client safely without memory leak
clients[i] = clients[len(clients)-1]
clients = clients[:len(clients)-1]
break
}
}
}

// 处理收到的信息
func processWSMessage(msg []byte) {
var genericMap map[string]interface{}
Expand Down

0 comments on commit 87dae2a

Please sign in to comment.