* setup infra for pusher service * setup client sdk for pusher service * fix: ping parse failure * fix: send pong back to client avoid disconnections every 2.5 minutes * increase replicas * feat: show presence and compose indicator
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package pusher
|
|
|
|
// Channel tracks the local connections subscribed to a channel on this pod.
|
|
// All methods are only called from the Hub goroutine — no locks needed.
|
|
type Channel struct {
|
|
id string
|
|
members map[*Conn]string // conn → humanID
|
|
}
|
|
|
|
func newChannel(id string) *Channel {
|
|
return &Channel{
|
|
id: id,
|
|
members: make(map[*Conn]string),
|
|
}
|
|
}
|
|
|
|
func (ch *Channel) addMember(conn *Conn, humanID string) {
|
|
ch.members[conn] = humanID
|
|
}
|
|
|
|
func (ch *Channel) removeMember(conn *Conn) {
|
|
delete(ch.members, conn)
|
|
}
|
|
|
|
func (ch *Channel) isEmpty() bool {
|
|
return len(ch.members) == 0
|
|
}
|
|
|
|
// localHumanIDs returns the deduplicated set of humanIDs connected on this pod.
|
|
func (ch *Channel) localHumanIDs() []string {
|
|
seen := make(map[string]bool, len(ch.members))
|
|
ids := make([]string, 0, len(ch.members))
|
|
for _, hid := range ch.members {
|
|
if !seen[hid] {
|
|
seen[hid] = true
|
|
ids = append(ids, hid)
|
|
}
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// hasHumanID returns true if the given humanID has at least one local connection.
|
|
func (ch *Channel) hasHumanID(humanID string) bool {
|
|
for _, hid := range ch.members {
|
|
if hid == humanID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// broadcast sends a message to all local connections except the excluded one.
|
|
func (ch *Channel) broadcast(msg ServerMessage, exclude *Conn) {
|
|
for conn := range ch.members {
|
|
if conn != exclude {
|
|
conn.Send(msg)
|
|
}
|
|
}
|
|
}
|