I am having difficulty understanding the implementation of something that I did using a blog
I am creating a new websocket connection, in which, I am running an infinite loop
As per my understanding
- The infinite loop should run indefinitely regardless the message received by the websocket
But it doesn't, the logic inside it is only triggered when a "new payload/message" is sent from the frontend ws connection:
func (s *SocketHandlers) NewConnectionHandler(w http.ResponseWriter, r *http.Request) {
// upgrade the http request to a ws request
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
}
defer ws.Close()
for { // infinite loop
log.Println("Checking to see if this repeats") // <=== PRINTS ONLY ONCE!
var payload core.NewSessionPayload
if err := ws.ReadJSON(&payload); err != nil {
log.Println("Cannot read socket conection payload")
log.Fatal(err)
}
s.clientsMap[ws] = core.ClientNode{
Active: true,
Username: payload.Username,
}
// broadcast the latest users list to all the users
s.broadcaster <- payload.Username
}
}