2021-03-11 20:27:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-12 19:06:20 +00:00
|
|
|
"context"
|
2021-03-11 20:27:12 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-03-12 19:06:20 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2021-03-11 20:27:12 +00:00
|
|
|
|
2021-03-23 14:46:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-03-11 20:27:12 +00:00
|
|
|
golog "github.com/ipfs/go-log/v2"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/node"
|
2021-03-12 19:06:20 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol"
|
2021-03-11 20:27:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
golog.SetAllLoggers(golog.LevelInfo) // Change to INFO for extra info
|
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
hostAddr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:60001")
|
|
|
|
extAddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:60001")
|
2021-03-11 20:27:12 +00:00
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
key := "9ceff459635becbab13190132172fc9612357696c176a9e2b6e22f28a73a54de"
|
2021-03-23 14:46:16 +00:00
|
|
|
prvKey, err := crypto.HexToECDSA(key)
|
2021-03-11 20:27:12 +00:00
|
|
|
|
2021-03-12 19:06:20 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
wakuNode, err := node.New(ctx, prvKey, hostAddr, extAddr)
|
2021-03-11 20:27:12 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
wakuNode.MountRelay()
|
2021-03-18 16:40:47 +00:00
|
|
|
wakuNode.MountStore()
|
2021-03-15 16:07:23 +00:00
|
|
|
|
|
|
|
sub, err := wakuNode.Subscribe(nil)
|
2021-03-12 19:06:20 +00:00
|
|
|
if err != nil {
|
2021-03-15 16:07:23 +00:00
|
|
|
fmt.Println("Could not subscribe:", err)
|
2021-03-12 19:06:20 +00:00
|
|
|
}
|
2021-03-11 20:27:12 +00:00
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
// Read loop
|
|
|
|
go func() {
|
2021-03-18 16:40:47 +00:00
|
|
|
for value := range sub.C {
|
|
|
|
payload, err := node.DecodePayload(value, &node.KeyInfo{Kind: node.None})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
2021-03-15 21:17:36 +00:00
|
|
|
}
|
2021-03-18 16:40:47 +00:00
|
|
|
|
|
|
|
fmt.Println("Received message:", string(payload))
|
|
|
|
// sub.Unsubscribe()
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
2021-03-18 16:40:47 +00:00
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
}()
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
// Write loop
|
|
|
|
go func() {
|
|
|
|
for {
|
2021-03-15 23:59:18 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
var contentTopic uint32 = 1
|
|
|
|
var version uint32 = 0
|
|
|
|
|
|
|
|
payload, err := node.Encode([]byte("Hello World"), &node.KeyInfo{Kind: node.None}, 0)
|
|
|
|
msg := &protocol.WakuMessage{Payload: payload, Version: &version, ContentTopic: &contentTopic}
|
|
|
|
err = wakuNode.Publish(msg, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error sending a message", err)
|
|
|
|
} else {
|
|
|
|
fmt.Println("Sent message...")
|
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
2021-03-15 21:17:36 +00:00
|
|
|
}()
|
2021-03-12 19:06:20 +00:00
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
// Wait for a SIGINT or SIGTERM signal
|
2021-03-12 19:06:20 +00:00
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-ch
|
2021-03-15 21:17:36 +00:00
|
|
|
fmt.Println("\n\n\nReceived signal, shutting down...")
|
2021-03-11 20:27:12 +00:00
|
|
|
|
2021-03-12 19:06:20 +00:00
|
|
|
// shut the node down
|
|
|
|
if err := wakuNode.Stop(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-11 20:27:12 +00:00
|
|
|
}
|