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
()
| 157 | // until either the subscription or pubsub context closes. |
| 158 | // The received message is parsed sent into the inbound channel |
| 159 | func (cr *PubSub) SubLoop() { |
| 160 | // Start loop |
| 161 | for { |
| 162 | select { |
| 163 | case <-cr.psctx.Done(): |
| 164 | return |
| 165 | |
| 166 | default: |
| 167 | // Read a message from the subscription |
| 168 | message, err := cr.psub.Next(cr.psctx) |
| 169 | // Check error |
| 170 | if err != nil { |
| 171 | // Close the messages queue (subscription has closed) |
| 172 | close(cr.Inbound) |
| 173 | cr.Logs <- chatlog{logprefix: "suberr", logmsg: "subscription has closed"} |
| 174 | return |
| 175 | } |
| 176 | |
| 177 | // Check if message is from self |
| 178 | if message.ReceivedFrom == cr.selfid { |
| 179 | continue |
| 180 | } |
| 181 | |
| 182 | // Declare a ChatMessage |
| 183 | cm := &chatmessage{} |
| 184 | // Unmarshal the message data into a ChatMessage |
| 185 | err = json.Unmarshal(message.Data, cm) |
| 186 | if err != nil { |
| 187 | cr.Logs <- chatlog{logprefix: "suberr", logmsg: "could not unmarshal JSON"} |
| 188 | continue |
| 189 | } |
| 190 | |
| 191 | // Send the ChatMessage into the message queue |
| 192 | cr.Inbound <- *cm |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // A method of PubSub that returns a list |
| 198 | // of all peer IDs connected to it |
no test coverage detected