mirror of https://github.com/status-im/go-waku.git
Use a ticker for getting next message
This commit is contained in:
parent
32cdc4cadd
commit
783ad01d92
5
main.go
5
main.go
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue