Skip to content

Instantly share code, notes, and snippets.

@beber89
Created September 24, 2019 10:56
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 beber89/c710e8f9f488a2a9a27bd410efea767f to your computer and use it in GitHub Desktop.
Save beber89/c710e8f9f488a2a9a27bd410efea767f to your computer and use it in GitHub Desktop.
mine-dapp-wasm/chainfabric/node/Connect
// Connect connects node to remote server via TCP
func (nd *Node) Connect() bool {
// connect to this socket
nd.ws = js.Global().Get("WebSocket").New(fmt.Sprintf("ws://%s:%d/ws", nd.toIP, nd.toPort))
nd.ws.Call("addEventListener", "open", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return nil
}))
nd.ws.Call("addEventListener", "message", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
message := args[0].Get("data").String()
fmt.Println("message received ")
fmt.Println(message)
flds := strings.Fields(message)
if flds[0] == "TRANSACTION" {
// execute callback to start mining
nd.newTransactionCallback(flds[1])
} else {
// receive nonce from tracker
nd.response = message
}
return nil
}))
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment