A method of PubSub that publishes a chatmessage to the PubSub topic until the pubsub context closes
()
| 123 | // A method of PubSub that publishes a chatmessage |
| 124 | // to the PubSub topic until the pubsub context closes |
| 125 | func (cr *PubSub) PubLoop() { |
| 126 | for { |
| 127 | select { |
| 128 | case <-cr.psctx.Done(): |
| 129 | return |
| 130 | |
| 131 | case message := <-cr.Outbound: |
| 132 | // Create a ChatMessage |
| 133 | m := chatmessage{ |
| 134 | Message: message, |
| 135 | SenderID: cr.selfid.String(), |
| 136 | SenderName: cr.ClientName, |
| 137 | } |
| 138 | |
| 139 | // Marshal the ChatMessage into a JSON |
| 140 | messagebytes, err := json.Marshal(m) |
| 141 | if err != nil { |
| 142 | cr.Logs <- chatlog{logprefix: "puberr", logmsg: "could not marshal JSON"} |
| 143 | continue |
| 144 | } |
| 145 | |
| 146 | // Publish the message to the topic |
| 147 | err = cr.pstopic.Publish(cr.psctx, messagebytes) |
| 148 | if err != nil { |
| 149 | cr.Logs <- chatlog{logprefix: "puberr", logmsg: "could not publish to topic"} |
| 150 | continue |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // A method of PubSub that continuously reads from the subscription |
| 157 | // until either the subscription or pubsub context closes. |
no test coverage detected