Skip to content

Instantly share code, notes, and snippets.

@mitche50
Last active May 8, 2019 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitche50/e1ab0c85b63a00ae6fd5966ab1eb4fb9 to your computer and use it in GitHub Desktop.
Save mitche50/e1ab0c85b63a00ae6fd5966ab1eb4fb9 to your computer and use it in GitHub Desktop.
//nano_golang_websocket.go creates a connection to the beta nano node and logs the hash
//of confirmed transactions sent from the websocket endpoint. The hash and subtype
//are printed to the terminal also.
package main
import (
"encoding/json"
"log"
"os"
"os/signal"
"strings"
"github.com/sacOO7/gowebsocket"
)
//WebsocketMessage contains the Topic, Time and Message sent to the websocket client
type WebsocketMessage struct {
Topic string `json:"topic"`
Time string `json:"time"`
Message NodeMessage `json:"message"`
}
//NodeMessage is the contents of the message sent from the Nano node.
type NodeMessage struct {
Account string `json:"account"`
Amount string `json:"amount"`
Hash string `json:"hash"`
Block Block `json:"block"`
}
//Block is the information for the provided block
type Block struct {
Type string `json:"state"`
Account string `json:"account"`
Previous string `json:"previous"`
Representative string `json:"representative"`
Balance string `json:"balance"`
Link string `json:"link"`
LinkAsAccount string `json:"link_as_account"`
Work string `json:"work"`
Subtype string `json:"subtype"`
}
func main() {
workLog := make(map[string]string)
previousLog := make(map[string]string)
timeLog := make(map[string]string)
count := 0
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
socket := gowebsocket.New("ws://[::1]:57000/")
socket.OnConnected = func(socket gowebsocket.Socket) {
log.Println("Connected to server")
data := map[string]string{"action": "subscribe", "topic": "confirmation"}
dataJSON, _ := json.Marshal(data)
socket.SendBinary(dataJSON)
}
socket.OnTextMessage = func(message string, socket gowebsocket.Socket) {
//log.Println("Recieved message " + message)
var responseJSON WebsocketMessage
d := json.NewDecoder(strings.NewReader(message))
d.Decode(&responseJSON)
_, wok := workLog[responseJSON.Message.Hash]
if !wok {
workLog[responseJSON.Message.Hash] = responseJSON.Message.Block.Work
}
_, pok := previousLog[responseJSON.Message.Hash]
if !pok {
previousLog[responseJSON.Message.Hash] = responseJSON.Message.Block.Previous
}
_, tok := timeLog[responseJSON.Message.Hash]
if !tok {
timeLog[responseJSON.Message.Hash] = responseJSON.Time
}
count++
log.Println(count)
}
socket.Connect()
for {
select {
case <-interrupt:
f, err := os.OpenFile("text.log",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
logger := log.New(f, "prefix", log.LstdFlags)
for k := range workLog {
logger.Printf(",%v,%v,%v,%v\n", k, workLog[k], previousLog[k], timeLog[k])
}
log.Println("Websocket connection closed.")
socket.Close()
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment