JRakNet 是一個 Java 網路庫,它實作了基於 UDP 的協定 RakNet。該程式庫旨在用於 Minecraft 伺服器和客戶端,但仍可用於輕鬆為其他視訊遊戲創建遊戲伺服器和用戶端。您也可以閱讀 JavaDocs
協議資訊 | 版本 |
---|---|
目前協議 | 10 |
支援的伺服器協議 | 10 |
支援的客戶端協議 | 10 |
如果您使用的是發布版本,請使用此依賴項:
< dependency >
< groupId >com.whirvis</ groupId >
< artifactId >jraknet</ artifactId >
< version >2.12.3</ version >
</ dependency >
如果您想使用快照版本,請使用此儲存庫和相依性:
< repository >
< id >codemc-repo</ id >
< url >https://repo.codemc.org/repository/maven-public/</ url >
</ repository >
< dependency >
< groupId >com.whirvis</ groupId >
< artifactId >jraknet</ artifactId >
< version >2.12.4-SNAPSHOT</ version >
</ dependency >
在 JRakNet 中建立伺服器非常簡單,建立伺服器所需的一切都可以在這裡看到:
// Add loopback exemption for Minecraft
if (! UniversalWindowsProgram . MINECRAFT . setLoopbackExempt ( true )) {
System . err . println ( "Failed to add loopback exemption for Minecraft" );
}
// Create server
RakNetServer server = new RakNetServer ( 19132 , 10 );
server . setIdentifier ( new MinecraftIdentifier ( "JRakNet Example Server" , 354 , "1.11" , 0 , 10 ,
server . getGloballyUniqueId (), "New World" , "Survival" ));
// Add listener
server . addListener ( new RakNetServerListener () {
// Client connected
@ Override
public void onConnect ( RakNetServer server , InetSocketAddress address , ConnectionType connectionType ) {
System . out . println ( "Client from address " + address + " has connected to the server" );
}
// Client logged in
@ Override
public void onLogin ( RakNetServer server , RakNetClientPeer peer ) {
System . out . println ( "Client from address " + peer . getAddress () + " has logged in" );
}
// Client disconnected
@ Override
public void onDisconnect ( RakNetServer server , InetSocketAddress address , RakNetClientPeer peer , String reason ) {
System . out . println ( "Client from address " + address
+ " has disconnected from the server for reason " " + reason + " " " );
}
// Packet received
@ Override
public void handleMessage ( RakNetServer server , RakNetClientPeer peer , RakNetPacket packet , int channel ) {
System . out . println ( "Client from address " + peer . getAddress () + " sent packet with ID "
+ RakNet . toHexStringId ( packet ) + " on channel " + channel );
}
});
// Start server
server . start ();
這是一個簡單的 RakNet 伺服器,可以透過 Minecraft 進行測試,方法是轉到應顯示伺服器的「朋友」標籤。伺服器彈出後,您應該能夠單擊它來觸發連接和資料包掛鉤。
在 Windows 10 上,預設使用通用 Windows 程式框架的應用程式無法連接到與其在同一台電腦上執行的伺服器。只要建立一個UniversalWindowsProgram
對象,其中第一個也是唯一一個參數是您希望能夠連接的應用程式的 ID,就可以停用這個煩人的功能。一個例子是 Microsoft 的 Edge,即Microsoft.MicrosoftEdge_8wekyb3d8bbwe
。因此,為了啟用環回豁免,只需這樣做:
UniversalWindowsProgram microsoftEdge = new UniversalWindowsProgram ( "Microsoft.MicrosoftEdge_8wekyb3d8bbwe" );
if (! microsoftEdge . setLoopbackExempt ( true )) {
System . err . println ( "Failed to enable loopback exemption for Microsoft Edge" );
}
很簡單,對吧?如果您在非 Windows 10 電腦上執行,請隨意實施此操作。即使您的電腦不執行 Windows 10 或未安裝 Windows PowerShell,此實作也是專門設計的。當然,如果您沒有使用安裝了 Windows PowerShell 的 Windows 10 計算機,則確實無法正確檢查您的應用程式是否不受環回影響。不過,我確信如果需要的話,可以在使用 Windows 10 和 Windows PowerShell 的使用者的幫助下解決這個問題。
在JRakNet 中建立客戶端也非常容易。創建客戶端所需的程式碼可以在這裡看到:
// Create client
RakNetClient client = new RakNetClient ();
// Add listener
client . addListener ( new RakNetClientListener () {
// Connected to server
@ Override
public void onConnect ( RakNetClient client , InetSocketAddress address , ConnectionType connectionType ) {
System . out . println ( "Successfully connected to server with address " + address );
}
// Logged into server
@ Override
public void onLogin ( RakNetClient client , RakNetServerPeer peer ) {
System . out . println ( "Successfully logged into server" );
client . disconnect ();
}
// Disconnected from server
@ Override
public void onDisconnect ( RakNetClient client , InetSocketAddress address , RakNetServerPeer peer , String reason ) {
System . out . println ( "Successfully disconnected from server with address " + address + " for reason " " + reason + " " " );
}
});
// Connect to server
client . connect ( "sg.lbsg.net" , 19132 );
這是一個簡單的 RakNet 用戶端,嘗試連接到主 LBSG 伺服器。連接後,它會關閉連接。