Use a ticker for getting next message

This commit is contained in:
Richard Ramos 2021-03-15 19:59:18 -04:00
parent 32cdc4cadd
commit 783ad01d92
No known key found for this signature in database
GPG Key ID: 80D4B01265FDFE8F
2 changed files with 9 additions and 3 deletions

View File

@ -49,7 +49,7 @@ func main() {
}
fmt.Println("Received message:", string(payload))
//sub.Unsubscribe()
// sub.Unsubscribe()
}
}
}()
@ -57,7 +57,8 @@ func main() {
// Write loop
go func() {
for {
time.Sleep(2 * time.Second)
time.Sleep(1 * time.Second)
var contentTopic uint32 = 1
var version uint32 = 0

View File

@ -8,6 +8,7 @@ import (
"log"
"net"
"sync"
"time"
proto "github.com/golang/protobuf/proto"
"github.com/libp2p/go-libp2p"
@ -181,6 +182,9 @@ func (node *WakuNode) Subscribe(topic *Topic) (*Subscription, error) {
subscription.quit = make(chan struct{})
go func(ctx context.Context, sub *pubsub.Subscription) {
nextMsgTicker := time.NewTicker(time.Millisecond * 10)
defer nextMsgTicker.Stop()
for {
select {
case <-subscription.quit:
@ -189,10 +193,11 @@ func (node *WakuNode) Subscribe(topic *Topic) (*Subscription, error) {
close(subscription.C)
subscription.closed = true
return
default:
case <-nextMsgTicker.C:
msg, err := sub.Next(ctx)
if err != nil {
fmt.Println("Error receiving message", err)
return // Should close channel?
}