Created
September 24, 2019 10:56
-
-
Save beber89/c710e8f9f488a2a9a27bd410efea767f to your computer and use it in GitHub Desktop.
mine-dapp-wasm/chainfabric/node/Connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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