Created
December 17, 2017 02:11
-
-
Save jleskovar/796b7e52012884295314e4724a3e5832 to your computer and use it in GitHub Desktop.
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
package com.github.jleskovar.btcrpc.examples | |
import com.neovisionaries.ws.client.WebSocket | |
import com.neovisionaries.ws.client.WebSocketAdapter | |
import com.neovisionaries.ws.client.WebSocketFactory | |
import com.neovisionaries.ws.client.WebSocketListener | |
import java.net.URL | |
import java.security.cert.X509Certificate | |
import javax.net.ssl.SSLContext | |
import javax.net.ssl.TrustManager | |
import javax.net.ssl.X509TrustManager | |
/** | |
* Created by james on 17/12/17. | |
*/ | |
fun main(args: Array<String>) { | |
URL("") | |
readLine() | |
var socket: WebSocket? = null | |
try { | |
val dummyTrustManager = arrayOf<TrustManager>(object : X509TrustManager { | |
override fun checkClientTrusted(p0: Array<out X509Certificate>?, p1: String?) {} | |
override fun checkServerTrusted(p0: Array<out X509Certificate>?, p1: String?) {} | |
override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf() | |
}) | |
val sslContext = SSLContext.getInstance("SSL") | |
sslContext.init(null, dummyTrustManager, java.security.SecureRandom()) | |
val webSocketFactory = WebSocketFactory() | |
webSocketFactory.sslContext = sslContext | |
socket = webSocketFactory.createSocket("wss://james-machine:8334/ws") | |
socket.addListener(object: WebSocketAdapter() { | |
override fun onTextMessage(websocket: WebSocket?, text: String?) { | |
println(text) | |
} | |
}) | |
socket.connect() | |
val username = "james" | |
val passphrase = "password!" | |
socket.sendText(""" {"jsonrpc": "2.0", "id":"1", "method": "authenticate", "params": ["$username","$passphrase"] }""") | |
for (i in 0..499693) { | |
socket.sendText(""" {"jsonrpc": "2.0", "id":"block-$i", "method": "getblockhash", "params":[$i] }""") | |
} | |
println("Enter key to terminate...") | |
readLine() | |
socket.sendClose() | |
} finally { | |
socket?.sendClose() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment