การใช้งาน Java ของ check-host.net API
หากคุณพบปัญหาใด ๆ โปรดรายงานปัญหาเหล่านั้นด้วยเครื่องมือติดตามปัญหา
หากคุณเพียงต้องการพูดคุยหรือต้องการความช่วยเหลือเกี่ยวกับ CheckHost4J โปรดเข้าร่วม Discord ของฉัน
หากต้องการใช้ CheckHost4J กับ Gradle/Maven คุณสามารถใช้ Maven Central, Maven ของ Lenni0451 หรือ Jitpack ได้
คุณสามารถดูคำแนะนำวิธีนำไปใช้กับสคริปต์บิลด์ของคุณได้ที่นั่น
หากคุณต้องการเพียงไฟล์ jar ล่าสุด คุณสามารถดาวน์โหลดได้จาก GitHub Actions หรือใช้ Release
ไลบรารีนี้ต้องการให้คุณมี Gson ในเส้นทางชั้นเรียนของคุณ
คลาสหลักของ API คือ CheckHost4J ซึ่งมีวิธีการทั้งหมดในการโต้ตอบกับ API
final CheckHost4J checkHost = CheckHost4J . INSTANCE ; คุณสามารถใช้ CheckHost4J.INSTANCE หรือสร้างอินสแตนซ์ใหม่ของ CheckHost4J ด้วยตัวเอง โดยการสร้างอินสแตนซ์ของตัวเอง คุณสามารถกำหนด IRequester ได้ด้วยตัวเอง ซึ่งใช้ในการส่งคำขอไปยัง API
final CheckHost4J checkHost = new CheckHost4J (...); IRequester เริ่มต้นคือ JavaRequester ซึ่งสามารถเข้าถึงได้ผ่าน JavaRequester.INSTANCE คุณยังสามารถสร้างอินสแตนซ์ใหม่โดยใช้ new JavaRequester("<user-agent>")
final CheckHost4J checkHost = new CheckHost4J ( new JavaRequester ( "MyUserAgent" )); คุณสามารถใช้เมธอด CheckHost4J#ping , CheckHost4J#http , CheckHost4J#tcpPort , CheckHost4J#udpPort และ CheckHost4J#dns เพื่อรับ ResultNode<T> โดยที่ T คือประเภทผลลัพธ์ของคำขอ (เช่น PingResult , TCPResult )
final ResultNode < PingResult > pingResult = checkHost . ping ( "example.com" , 80 /* max nodes */ ); หลังจากที่คุณได้รับ ResultNode<T> แล้ว คุณสามารถใช้ tickResults() เพื่ออัปเดตรายการ getResults()
// This will update the results list by sending the check-result request to the API,
// This might not update all results, because some might not be finished yet
// Which means you have to call this method multiple times to get all results (e.g. with a delay of 5 seconds)
pingResult . tickResults ();
final Map < ServerNode , PingResult > results = pingResult . getResults ();
results . forEach (( serverNode , result ) -> {
if ( result == null ) { // All results which are not finished yet will be null
System . out . println ( serverNode . name + " is still checking..." );
} else if ( result . getErrorMessage () != null ) {
System . out . println ( serverNode . name + " failed: " + result . getErrorMessage ());
} else {
System . out . println ( serverNode . name + " responded: " + result . getSuccessfulPings () + "/" + result . getTotalPings ());
}
}); คุณยังสามารถรับโหนดเซิร์ฟเวอร์ทั้งหมดที่กำลังตรวจสอบได้โดยใช้เมธอด getNodes()
final List < ServerNode > nodes = pingResult . getNodes (); แพ็คเกจ de.florianmichael.checkhost4j.model.result มีคลาสผลลัพธ์ทั้งหมด ซึ่งใช้เพื่อจัดเก็บผลลัพธ์ของการร้องขอ
หากต้องการรับรายการประเภทคำขอทั้งหมด คุณสามารถใช้ ResultType enum