A method of PubSub that continuously reads from the subscription until either the subscription or pubsub context closes. The received message is parsed sent into the inbound channel
()
| 221 | // until either the subscription or pubsub context closes. |
| 222 | // The received message is parsed sent into the inbound channel |
| 223 | func (cr *PubSub) SubLoop() { |
| 224 | // Start loop |
| 225 | for { |
| 226 | select { |
| 227 | case <-cr.psctx.Done(): |
| 228 | return |
| 229 | |
| 230 | default: |
| 231 | // Read a message from the subscription |
| 232 | message, err := cr.psub.Next(cr.psctx) |
| 233 | // Check error |
| 234 | if err != nil { |
| 235 | // Close the messages queue (subscription has closed) |
| 236 | close(cr.Inbound) |
| 237 | cr.Logs <- chatlog{logprefix: "suberr", logmsg: "subscription has closed"} |
| 238 | return |
| 239 | } |
| 240 | |
| 241 | // Check if message is from self |
| 242 | //if message.ReceivedFrom == cr.selfid { |
| 243 | // continue |
| 244 | //} |
| 245 | |
| 246 | // Declare a ChatMessage |
| 247 | cm := &chatmessage{} |
| 248 | // Unmarshal the message data into a ChatMessage |
| 249 | err = json.Unmarshal(message.Data, cm) |
| 250 | if err != nil { |
| 251 | cr.Logs <- chatlog{logprefix: "suberr", logmsg: "could not unmarshal JSON"} |
| 252 | continue |
| 253 | } |
| 254 | |
| 255 | // Send the ChatMessage into the message queue |
| 256 | cr.Inbound <- *cm |
| 257 | logrus.Infof("Inbound Message: %s", cm.Message) |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | func (cr *PubSub) Writer() { |
| 263 | for { |
no test coverage detected