Created
October 19, 2022 15:12
-
-
Save leoantony72/635b2ab49347a777f2feb171379bafd2 to your computer and use it in GitHub Desktop.
Go websockets - send message gorilla websocket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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