go-waku/main.go

96 lines
1.9 KiB
Go
Raw Normal View History

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
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-15 16:07:23 +00:00
ethNodeCrypto "github.com/status-im/status-go/eth-node/crypto"
2021-03-11 20:27:12 +00:00
)
2021-03-15 16:07:23 +00:00
/*
2021-03-12 19:06:20 +00:00
func readLoop(sub *pubsub.Subscription, ctx context.Context) {
for {
msg, err := sub.Next(ctx)
if err != nil {
fmt.Println(err)
return
}
cm := new(Test)
err = json.Unmarshal(msg.Data, cm)
if err != nil {
return
}
fmt.Println("Received: " + cm.Message)
}
}
2021-03-15 16:07:23 +00:00
*/
2021-03-12 19:06:20 +00:00
2021-03-11 20:27:12 +00:00
func main() {
golog.SetAllLoggers(golog.LevelInfo) // Change to INFO for extra info
hostAddr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:5555")
extAddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:5555")
2021-03-15 16:07:23 +00:00
key := "9ceff459635becbab13190132172fc9612357696c176a9e2b6e22f28a73a54ce"
prvKey, err := ethNodeCrypto.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()
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 16:07:23 +00:00
go func(sub chan *protocol.WakuMessage) {
for {
fmt.Println("Waiting for a message...")
x := <-sub
fmt.Println("Received a message: ", string(x.Payload))
}
}(sub)
for {
time.Sleep(4 * time.Second)
fmt.Println("Sending 'Hello World'...")
var contentTopic uint32 = 1
var version uint32 = 0
msg := &protocol.WakuMessage{Payload: []byte("Hello World"), Version: &version, ContentTopic: &contentTopic}
err = wakuNode.Publish(msg, nil)
if err != nil {
fmt.Println("ERROR SENDING MESSAGE", err)
} else {
fmt.Println("Sent...")
}
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
fmt.Println("Received 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
}