ceremonyclient/go-libp2p/examples/ipfs-camp-2019/07-Messaging/pubsub.go
2023-08-20 23:07:43 -05:00

47 lines
912 B
Go

package main
import (
"context"
"fmt"
"os"
"github.com/gogo/protobuf/proto"
pubsub "github.com/libp2p/go-libp2p-pubsub"
peer "github.com/libp2p/go-libp2p/core/peer"
)
const pubsubTopic = "/libp2p/example/chat/1.0.0"
func pubsubMessageHandler(id peer.ID, msg *SendMessage) {
fmt.Printf("%s: %s\n", id.ShortString(), msg.Data)
}
func pubsubUpdateHandler(id peer.ID, msg *UpdatePeer) {
}
func pubsubHandler(ctx context.Context, sub *pubsub.Subscription) {
defer sub.Cancel()
for {
msg, err := sub.Next(ctx)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
req := &Request{}
err = proto.Unmarshal(msg.Data, req)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
switch *req.Type {
case Request_SEND_MESSAGE:
pubsubMessageHandler(msg.GetFrom(), req.SendMessage)
case Request_UPDATE_PEER:
pubsubUpdateHandler(msg.GetFrom(), req.UpdatePeer)
}
}
}