Skip to content

Instantly share code, notes, and snippets.

@leoantony72
Created October 19, 2022 15:12
Show Gist options
  • Save leoantony72/635b2ab49347a777f2feb171379bafd2 to your computer and use it in GitHub Desktop.
Save leoantony72/635b2ab49347a777f2feb171379bafd2 to your computer and use it in GitHub Desktop.
Go websockets - send message gorilla websocket
var clients = make(map[string]*websocket.Conn)
func Send() {
for {
msg := <-broadcast
message := Message{}
if err := json.Unmarshal([]byte(msg.Payload), &message); err != nil {
panic(err)
}
JsonData, err := json.Marshal(message)
utils.CheckErr(err)
client := clients[message.Receiver]
err = client.WriteMessage(websocket.TextMessage, []byte(JsonData))
if err != nil {
client.Close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment