MCPcopy Create free account
hub / github.com/daodst/chat / subscribe

Method subscribe

server/session.go:212–239  ·  view source on GitHub ↗

Request to subscribe to a topic

(msg *ClientComMessage)

Source from the content-addressed store, hash-verified

210
211// Request to subscribe to a topic
212func (s *Session) subscribe(msg *ClientComMessage) {
213 log.Printf("Sub to '%s' from '%s'", msg.Sub.Topic, msg.from)
214
215 var topic, expanded string
216
217 if msg.Sub.Topic == "new" {
218 // Request to create a new named topic
219 topic = msg.Sub.Topic
220 expanded = genTopicName()
221 } else {
222 var err *ServerComMessage
223 topic, expanded, err = s.validateTopicName(msg.Sub.Id, msg.Sub.Topic, msg.timestamp)
224 if err != nil {
225 s.QueueOut(err)
226 return
227 }
228 }
229
230 if _, ok := s.subs[expanded]; ok {
231 log.Printf("sess.subscribe: already subscribed to '%s'", expanded)
232 s.QueueOut(InfoAlreadySubscribed(msg.Sub.Id, msg.Sub.Topic, msg.timestamp))
233 return
234 }
235
236 log.Printf("Sub to '%s' (%s) from '%s' as '%s' -- OK!", expanded, msg.Sub.Topic, msg.from, topic)
237 globals.hub.reg <- &sessionJoin{topic: expanded, pkt: msg.Sub, sess: s}
238 // Hub will send Ctrl success/failure packets back to session
239}
240
241// Leave/Unsubscribe a topic
242func (s *Session) leave(msg *ClientComMessage) {

Callers 1

dispatchMethod · 0.95

Calls 3

validateTopicNameMethod · 0.95
QueueOutMethod · 0.95
InfoAlreadySubscribedFunction · 0.85

Tested by

no test coverage detected