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

View File

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