add ipfs pubsub methods

This commit is contained in:
imw
2018-12-28 12:37:36 +01:00
parent b4b687eeda
commit 6ad2f16c24

View File

@@ -8,7 +8,7 @@ import (
shell "github.com/ipfs/go-ipfs-api"
)
func publish(object []byte) (string) {
func publish(object []byte) string {
sh := shell.NewShell("localhost:5001")
cid, err := sh.Add(bytes.NewBuffer(object))
if err != nil {
@@ -28,7 +28,7 @@ func pin(path string) {
}
func retrieve(hash string) ([]byte){
func retrieve(hash string) []byte {
sh := shell.NewShell("localhost:5001")
reader, err := sh.Cat(hash)
if err != nil {
@@ -42,3 +42,22 @@ func retrieve(hash string) ([]byte){
}
return content
}
func PsSubscribe(topic string) *shell.PubSubSubscription {
sh := shell.NewShell("localhost:5001")
sub, err := sh.PubSubSubscribe(topic)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
os.Exit(1)
}
return sub
}
func PsPublish(topic, data string) {
sh := shell.NewShell("localhost:5001")
err := sh.PubSubPublish(topic, data)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
os.Exit(1)
}
}