안녕하세요.
public void subscribeToContractEventsTest() throws Exception {
String contractAddress = "0x8a2...98";
// 2. KAS 인증 정보를 포함한 WSS URL 생성
String wssUrl = String.format(
"wss://%s:%s@node-api.klaytnapi.com/v1/ws/open?chain-id=%s",
accessKey,
// secretAccessKey,
stringEncryptor.decrypt(secretAccessKey),
chainId
);
System.out.println("Connecting to KAS WebSocket: " + wssUrl.replaceAll(secretAccessKey, "[SECRET]"));
// 3. WebSocketService 생성 및 연결
WebSocketService wsService = new WebSocketService(wssUrl, false);
try {
wsService.connect(); // WSS 연결
} catch (ConnectException e) {
System.err.println("❌ KAS WSS 연결 실패: " + e.getMessage());
System.err.println("AccessKey, SecretKey 또는 Chain ID를 확인해주세요.");
throw e; // ⭐️ 테스트 실패 처리
}
Web3j web3j = Web3j.build(wsService);
System.out.println("✅ KAS WebSocket 연결 성공!");
// 4. 구독할 이벤트 필터 생성
EthFilter filter = new EthFilter(
DefaultBlockParameterName.LATEST,
DefaultBlockParameterName.LATEST,
contractAddress
);
System.out.println("Subscribing to logs for contract: " + contractAddress);
// 5. 이벤트 스트림 구독 시작 (web3j.ethLogFlowable)
Disposable subscription = web3j.ethLogFlowable(filter)
.subscribe(
log -> {
// --- ✅ 이벤트 수신 성공 시 ---
System.out.println("------- 📦 NEW EVENT RECEIVED! -------");
System.out.println(" Block #: " + log.getBlockNumber());
System.out.println(" Tx Hash: " + log.getTransactionHash());
System.out.println(" Topics: " + log.getTopics());
System.out.println(" Data: " + log.getData());
System.out.println("-------------------------------------");
},
error -> {
// --- ❌ 스트림 에러 발생 시 ---
System.err.println("❌ Subscription Error: " + error.getMessage());
error.printStackTrace();
},
() -> {
// --- ℹ️ 스트림 종료 시 ---
System.out.println("ℹ️ Subscription Completed.");
}
);
// 6. ⭐️ 테스트가 바로 종료되지 않도록 대기 (필수)
// JUnit 테스트는 메인 스레드가 끝나면 바로 종료되므로, WSS 응답을 기다려야 함
System.out.println("Listening for events for 1 minute... (테스트 대기 중)");
try {
TimeUnit.MINUTES.sleep(1); // 1분간 대기
} catch (InterruptedException e) {
System.out.println("Sleep interrupted.");
}
// 7. 테스트 종료 및 자원 해제
System.out.println("Cleaning up and closing connection...");
subscription.dispose(); // 구독 해제
wsService.close(); // WSS 연결 종료
System.out.println("Done.");
}
위와 같이 웹훅을 통해서 이벤트를 구독하고 싶습니다.
제가 알기로는 지원이 안되는 것으로 알고 있는데 시간이 지나서 다시 지원하는지 궁금하네요