Add support for polling subscribed messages

This commit is contained in:
Oskar Thoren 2021-06-10 22:14:08 +08:00
parent 452d4c5304
commit 484a63daf1
No known key found for this signature in database
GPG Key ID: B2ECCFD3BC2EF77E
1 changed files with 19 additions and 13 deletions

View File

@ -26,6 +26,7 @@ type Chat struct {
// TODO Replace this // TODO Replace this
//sub *node.Subscription //sub *node.Subscription
pubsubTopic string
// TODO Replace with wrapper // TODO Replace with wrapper
//node *node.WakuNode //node *node.WakuNode
client *rpc.Client client *rpc.Client
@ -39,17 +40,19 @@ type Chat struct {
// NewChat tries to subscribe to the PubSub topic for the room name, returning // NewChat tries to subscribe to the PubSub topic for the room name, returning
// a ChatRoom on success. // a ChatRoom on success.
func NewChat(client *rpc.Client, selfID peer.ID, contentTopic string, useV1Payload bool, nickname string) (*Chat, error) { func NewChat(client *rpc.Client, selfID peer.ID, contentTopic string, useV1Payload bool, nickname string) (*Chat, error) {
var defaultTopic = "/waku/2/default-waku/proto"
// join the default waku topic and subscribe to it // join the default waku topic and subscribe to it
// TODO Do this with JSON RPC _, err := nwaku.PostWakuRelaySubscriptions(client, []string{defaultTopic})
// sub, err := n.Subscribe(nil) if err != nil {
// if err != nil { return nil, err
// return nil, err }
// }
c := &Chat{ c := &Chat{
//node: n, //node: n,
client: client, client: client,
// XXX Not used directly anymore
//sub: sub, //sub: sub,
pubsubTopic: defaultTopic,
self: selfID, self: selfID,
contentTopic: contentTopic, contentTopic: contentTopic,
nick: nickname, nick: nickname,
@ -134,11 +137,7 @@ func (cr *Chat) Publish(ctx context.Context, message string) error {
//Timestamp: timestamp, //Timestamp: timestamp,
} }
// TODO Error handling // TODO Error handling
// XXX: Somewhere it panics
// gonwaku > testpanic: runtime error: invalid memory address or nil pointer dereference
var _, _ = nwaku.PostWakuRelayMessage(cr.client, wakuMsg) var _, _ = nwaku.PostWakuRelayMessage(cr.client, wakuMsg)
//XXX dont see this?
//log.Printf("NYI Publish", res, err2)
return nil return nil
} }
@ -165,11 +164,18 @@ func (cr *Chat) decodeMessage(wakumsg nwaku.WakuMessage) {
} }
// readLoop pulls messages from the pubsub topic and pushes them onto the Messages channel. // readLoop pulls messages from the pubsub topic and pushes them onto the Messages channel.
// TODO Improve polling with channels etc here
// XXX This also means that we don't see our message straight away currently
func (cr *Chat) readLoop() { func (cr *Chat) readLoop() {
// TODO Replace with JSON RPC for {
// for value := range cr.sub.C { var wakuMessages, _ = nwaku.GetWakuRelayMessages(cr.client, cr.pubsubTopic)
// cr.decodeMessage(value.Message()) // for value := range cr.sub.C {
// } for _, msg := range wakuMessages {
cr.decodeMessage(msg)
}
time.Sleep(2 * time.Second)
}
} }
func (cr *Chat) displayMessages(messages []nwaku.WakuMessage) { func (cr *Chat) displayMessages(messages []nwaku.WakuMessage) {