mirror of
https://source.quilibrium.com/quilibrium/ceremonyclient.git
synced 2024-11-11 02:35:18 +00:00
47 lines
912 B
Go
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)
|
||
|
}
|
||
|
}
|
||
|
}
|