Created
July 22, 2020 04:28
-
-
Save domino14/23fbc98229566d7356159c830e3096d6 to your computer and use it in GitHub Desktop.
Process pubsub messages
This file contains 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
// subchans is a map[string]chan *nats.Msg | |
func (h *Hub) PubsubProcess() { | |
for { | |
select { | |
case msg := <-h.pubsub.subchans["lobby.>"]: | |
h.sendToRealm(LobbyRealm, msg.Data) | |
case msg := <-h.pubsub.subchans["user.>"]: | |
// Send the message to every socket belonging to this user. | |
subtopics := strings.Split(msg.Subject, ".") | |
userID := subtopics[1] | |
for client := range h.clientsByUserID[userID] { | |
client.send <- msg.Data | |
} | |
case msg := <-h.pubsub.subchans["gametv.>"]: | |
// A gametv message is meant for people who are observing a user's games. | |
log.Debug().Str("topic", msg.Subject).Msg("got gametv message, forwarding along") | |
subtopics := strings.Split(msg.Subject, ".") | |
gameID := subtopics[1] | |
h.sendToRealm(Realm("gametv-"+gameID), msg.Data) | |
// Handle more messages here... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment