Decentralized instant messaging framework
go get github.com/h2so5/murcott
package main
import (
"fmt"
"github.com/h2so5/murcott"
"os"
"strings"
)
func main() {
// Private key identifies the ownership of your node.
key := utils.GeneratePrivateKey()
fmt.Println("Your node id: " + key.PublicKeyHash().String())
// Storage keeps client's persistent data.
storage := utils.NewStorage("storage.sqlite3")
// Create a client with the private key and the storage.
client, _ := utils.NewClient(key, storage, utils.DefaultConfig)
// Handle incoming messages.
client.HandleMessages(func(src utils.NodeID, msg utils.ChatMessage) {
fmt.Println(msg.Text() + " from " + src.String())
})
// Start client's mainloop.
go client.Run()
// Parse a base58-encoded node identifier of your friend.
dst, _ := utils.NewNodeIDFromString("3CjjdZLV4DqXkc3KtPZPTfBU1AAY")
for {
b := make([]byte, 1024)
len, err := os.Stdin.Read(b)
if err != nil {
break
}
str := strings.TrimSpace(string(b[:len]))
if str == "quit" {
break
}
// Send message to the destination node.
client.SendMessage(dst, utils.NewPlainChatMessage(str), func(ok bool) {
if !ok {
fmt.Println("Failed to deliver the message to the node...")
}
})
}
// Stop client's mainloop.
client.Close()
}
MIT License