Skip to content

Commit

Permalink
refactor: refatorar codigo remove Unreachable code
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo-Lopes-Estevao committed Jul 6, 2023
1 parent 2cbb97d commit 55af46e
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions app/ws/websocket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ var upgrader = websocket.Upgrader{
},
}

var client = make(map[*websocket.Conn]bool)
var lock sync.RWMutex

// WebsocketServer godoc
// @Summary Websocket server
// @Description websocket url ws://host/ws or use authentication ssl wss://host/ws
Expand All @@ -32,6 +35,9 @@ func WebsocketServer(ctx echo.Context) error {
}

defer func(conn *websocket.Conn) {
lock.Lock()
delete(client, conn)
lock.Unlock()
err := conn.Close()
if err != nil {
return
Expand All @@ -45,24 +51,20 @@ func WebsocketServer(ctx echo.Context) error {
channel := make(chan string)
go broadcast(channel)

// remover unreachable code
// lock.Lock() // lock
// delete(client, conn)
// lock.Unlock() // unlock

for {
_, msg, err := conn.ReadMessage()
if err != nil {
return ctx.JSON(400, err)
}
channel <- string(msg)
}

lock.Lock()
delete(client, conn)
lock.Unlock()

return nil
}

var client = make(map[*websocket.Conn]bool)
var lock sync.RWMutex

func broadcast(c chan string) {
for {
msg := <-c
Expand Down

0 comments on commit 55af46e

Please sign in to comment.