forked from miguelmota/ethereum-development-with-go-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whisper_poll.go
54 lines (48 loc) · 1.1 KB
/
whisper_poll.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"context"
"fmt"
"log"
"github.com/ethereum/go-ethereum/whisper/shhclient"
"github.com/ethereum/go-ethereum/whisper/whisperv6"
)
func main() {
client, err := shhclient.Dial("ws://127.0.0.1:8546")
if err != nil {
log.Fatal(err)
}
filterID, err := client.NewMessageFilter(context.Background(), whisperv6.Criteria{
PrivateKeyID: privateKeyID,
})
if err != nil {
log.Fatal(err)
}
fmt.Println(filterID) // 21171f8b4e7ac0d7a1ce0d121b647ce10d4f0293b95d8fba69c5b4e9d0f235a6
messages := make(chan *whisperv6.Message)
sub, err := client.SubscribeMessages(context.Background(), whisper6.Criteria{
PrivateKeyID: privateKeyID,
}, messages)
if err != nil {
log.Fatal(err)
}
go func() {
for {
select {
case err := <-sub.Err():
log.Fatal(err)
case message := <-messages:
fmt.Printf(string(message.Payload)) // "Hello"
}
}
}()
/*
messages, err := client.FilterMessages(context.Background(), filterID)
if err != nil {
log.Fatal(err)
}
fmt.Println(messages)
for _, message := range messages {
fmt.Printf(string(message.Payload)) // "Hello"
}
*/
}