use of org.web3j.protocol.websocket.WebSocketService in project besu by hyperledger.
the class BesuNode method nodeRequests.
private NodeRequests nodeRequests() {
Optional<WebSocketService> websocketService = Optional.empty();
if (nodeRequests == null) {
final Web3jService web3jService;
if (useWsForJsonRpc) {
final String url = wsRpcBaseUrl().orElse("ws://" + LOCALHOST + ":" + 8546);
final Map<String, String> headers = new HashMap<>();
if (token != null) {
headers.put("Authorization", "Bearer " + token);
}
final WebSocketClient wsClient = new WebSocketClient(URI.create(url), headers);
web3jService = new WebSocketService(wsClient, false);
try {
((WebSocketService) web3jService).connect();
} catch (final ConnectException e) {
throw new RuntimeException(e);
}
websocketService = Optional.of((WebSocketService) web3jService);
} else {
final String url = jsonRpcBaseUrl().orElse(HTTP + LOCALHOST + ":" + 8545);
web3jService = new HttpService(url);
if (token != null) {
((HttpService) web3jService).addHeader("Authorization", "Bearer " + token);
}
}
final ConsensusType bftType = getGenesisConfig().map(gc -> gc.toLowerCase().contains("ibft") ? ConsensusType.IBFT2 : ConsensusType.QBFT).orElse(ConsensusType.IBFT2);
nodeRequests = new NodeRequests(new JsonRpc2_0Web3j(web3jService, 2000, Async.defaultExecutorService()), new CliqueRequestFactory(web3jService), new BftRequestFactory(web3jService, bftType), new PermissioningJsonRpcRequestFactory(web3jService), new AdminRequestFactory(web3jService), new PrivacyRequestFactory(web3jService), new CustomRequestFactory(web3jService), new MinerRequestFactory(web3jService), new TxPoolRequestFactory(web3jService), websocketService, loginRequestFactory());
}
return nodeRequests;
}
use of org.web3j.protocol.websocket.WebSocketService in project wuhanchain by BSN-DDC.
the class EventListenerByBlock method getBlockEventByListener.
/**
* get block event
*
* @param startBlockNum blockNumber
* @return ddc official contract event data
* @throws Exception
*/
public void getBlockEventByListener(BigInteger startBlockNum, String wsUrl, Map<String, String> httpHeaders) {
try {
// webSocketClient
WebSocketClient webSocketClient = new WebSocketClient(new URI(wsUrl), httpHeaders);
WebSocketService webSocketService = new WebSocketService(webSocketClient, false);
webSocketService.connect();
web3j = Web3j.build(webSocketService);
// ThreadPool
executorService = new ThreadPoolExecutor(8, 8, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(100000), new ThreadPoolExecutor.CallerRunsPolicy());
// startBlock, endBlock, fullTransactionObjects
subscribe = web3j.replayPastAndFutureBlocksFlowable(DefaultBlockParameter.valueOf(startBlockNum), true).doOnError(e -> logger.error("doOnError:" + e.getMessage())).subscribe(this::executeBlock, ex -> logger.error("subscribe:" + ex.getMessage()), () -> onCompleted());
while (true) {
if (subscriptions) {
break;
}
}
} catch (Exception e) {
logger.error("analysisResult:" + e);
}
}
use of org.web3j.protocol.websocket.WebSocketService in project starcoin-search by starcoinorg.
the class SubscribeHandler method run.
@Override
public void run() {
try {
WebSocketService service = new WebSocketService("ws://" + seedHost + ":9870", true);
service.connect();
StarcoinSubscriber subscriber = new StarcoinSubscriber(service);
Flowable<PendingTransactionNotification> flowableTxns = subscriber.newPendingTransactionsNotifications();
TransactionRPCClient rpc = new TransactionRPCClient(new URL("http://" + seedHost + ":9850"));
for (PendingTransactionNotification notifications : flowableTxns.blockingIterable()) {
for (String notification : notifications.getParams().getResult()) {
logger.info("notification: {}", notification);
PendingTransaction transaction = rpc.getPendingTransaction(notification);
elasticSearchHandler.savePendingTransaction(transaction);
}
}
} catch (ConnectException | MalformedURLException | JSONRPC2SessionException e) {
logger.error("handle subscribe exception:", e);
}
}
use of org.web3j.protocol.websocket.WebSocketService in project starcoin-java by starcoinorg.
the class SubscribeSample method main.
public static void main(String... args) throws ConnectException {
WebSocketService service = new WebSocketService("ws://localhost:9870", true);
service.connect();
StarcoinSubscriber subscriber = new StarcoinSubscriber(service);
EventFilter eventFilter = new EventFilter(0, "b75994d55eae88219dc57e7e62a11bc0");
Flowable<EventNotification> flowableTxns = subscriber.newTxnSendRecvEventNotifications(eventFilter);
for (EventNotification notification : flowableTxns.blockingIterable()) {
System.out.println(notification.getParams().getResult().toString());
}
}
Aggregations