Java Integration Guide
Use the webrpc Native SDK via JNA against the same Native API, matching the official Go sample flow: login → callback port → send/receive.
Overview
- The SDK exposes a C ABI (
libwebrpc-*.h+ shared library per platform). - The sample below is meant to be buildable/runnable after you replace placeholders and place the correct platform library files.
- Get token/password from My Tokens in the console.
Dependencies & platforms
| Platform | Header | Link library (typical) |
|---|---|---|
| macOS | libwebrpc-Mac.h | -lwebrpc-Mac |
| Linux x86_64 | libwebrpc-Linux.h | -lwebrpc-Linux |
| Linux arm64 | libwebrpc-Linux-arm64.h | -lwebrpc-Linux-arm64 |
| Windows | libwebrpc-Windows.h | webrpc-Windows (see .lib / .dll in the SDK zip) |
Download the matching SDK from the console, place the header and shared library next to your project (or configure include/lib paths).
Replace
YOUR_TOKEN/YOUR_PASSWORDwith real values from My Tokens.
Integration flow
Typical lifecycle:
1. WebrpcClient_New
2. Poll WebrpcClient_LoginStatus until non-zero
3. WebrpcClient_GetReceivePort
4. Background thread connects to the callback port and parses frames
5. Optional: WebrpcClient_OpenSession to dial a peer
6. Main loop may print WebrpcClient_SessionSize
7. WebrpcClient_Free before exit
Native API reference
| API | Description |
|---|---|
WebrpcClient_New(token, passwd, permission) | Create a client handle (uintptr). permission may be an empty string. |
WebrpcClient_LoginStatus(handle) | Login status; non-zero means ready. |
WebrpcClient_GetReceivePort(handle) | Local callback TCP port on 127.0.0.1. |
WebrpcClient_OpenSession(handle, toToken, permission) | Open a session to a peer token; returns sessionId. |
WebrpcClient_SessionSize(handle) | Current session count. |
WebrpcClient_TarTokenBySession(handle, sessionId) | Peer token for a session. |
WebrpcClient_CloseSession(handle, sessionId) | Close a session. |
WebrpcClient_SendData(handle, sessionId, data, dataLen, timeOutMs) | Send bytes; timeOutMs is timeout in milliseconds. |
WebrpcClient_SendFile(handle, sessionId, filePath) | Send a file. |
WebrpcClient_Free(handle) | Free the client. Always call before exit. |
Callback TCP protocol
After the client is created, the SDK opens a local TCP callback port. Your process should connect("127.0.0.1", port) and read frames in a loop:
sessionId : uint32 big-endian (4 bytes)
type : uint8 (1 byte)
- 2 = data stream
- 1 = file stream
Data stream (type=2)
dataLen : uint32 BE
payload : dataLen bytes
File stream (type=1)
nameLen : uint32 BE
fileName : nameLen bytes
dataLen : uint32 BE
fileData : dataLen bytes
On a data stream, the sample replies with WebrpcClient_SendData(..., "Received", ..., 3000).
Complete example
// Requires JNA: https://github.com/java-native-access/jna
// Maven: <dependency><groupId>net.java.dev.jna</groupId><artifactId>jna</artifactId><version>5.14.0</version></dependency>
// Put libwebrpc-*.dylib/.so/.dll on java.library.path or absolute path below.
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class Main {
public interface WebrpcLib extends Library {
long WebrpcClient_New(String token, String passwd, String permission);
int WebrpcClient_LoginStatus(long handle);
int WebrpcClient_GetReceivePort(long handle);
int WebrpcClient_OpenSession(long handle, String toToken, String permission);
short WebrpcClient_SessionSize(long handle);
Pointer WebrpcClient_TarTokenBySession(long handle, int sessionId);
int WebrpcClient_CloseSession(long handle, int sessionId);
int WebrpcClient_SendData(long handle, int sessionId, String data, int dataLen, long timeOut);
int WebrpcClient_SendFile(long handle, int sessionId, String filePath);
void WebrpcClient_Free(long handle);
}
// Change library name per platform: webrpc-Mac / webrpc-Linux / webrpc-Linux-arm64 / webrpc-Windows
static final WebrpcLib LIB = Native.load(resolveLibName(), WebrpcLib.class);
static String resolveLibName() {
String os = System.getProperty("os.name", "").toLowerCase();
String arch = System.getProperty("os.arch", "").toLowerCase();
if (os.contains("mac") || os.contains("darwin")) return "webrpc-Mac";
if (os.contains("win")) return "webrpc-Windows";
if (arch.contains("aarch64") || arch.contains("arm64")) return "webrpc-Linux-arm64";
return "webrpc-Linux";
}
static void startReceivingCallbackData(long webrpc, int tcpPort) {
try (Socket socket = new Socket("127.0.0.1", tcpPort)) {
DataInputStream in = new DataInputStream(socket.getInputStream());
System.out.println("Connected to callback server, port: " + tcpPort);
while (true) {
int sessionId = in.readInt();
int dataType = in.readUnsignedByte();
if (dataType == 2) {
handleDataStream(webrpc, in, sessionId);
} else if (dataType == 1) {
handleFileStream(in, sessionId);
} else {
System.out.println("Unknown data type: " + dataType);
}
}
} catch (IOException e) {
System.out.println("Callback connection closed: " + e.getMessage());
}
}
static void handleDataStream(long webrpc, DataInputStream in, int sessionId) throws IOException {
int dataLength = in.readInt();
byte[] data = in.readNBytes(dataLength);
if (dataLength > 0 && dataLength < 1024) {
System.out.println("Data content: " + new String(data, StandardCharsets.UTF_8));
}
String msg = "Received";
int ret = LIB.WebrpcClient_SendData(webrpc, sessionId, msg, msg.length(), 3000);
System.out.println("SendData ret = " + ret);
}
static void handleFileStream(DataInputStream in, int sessionId) throws IOException {
int fileNameLength = in.readInt();
byte[] fileName = in.readNBytes(fileNameLength);
int fileDataLength = in.readInt();
byte[] fileData = in.readNBytes(fileDataLength);
System.out.println("File stream sessionId=" + sessionId
+ " name=" + new String(fileName, StandardCharsets.UTF_8)
+ " size=" + fileData.length);
}
public static void main(String[] args) throws Exception {
long webrpc = LIB.WebrpcClient_New("YOUR_TOKEN", "YOUR_PASSWORD", "");
if (webrpc == 0L) {
throw new IllegalStateException("WebrpcClient_New failed");
}
System.out.println("Client handle: " + webrpc);
while (true) {
int status = LIB.WebrpcClient_LoginStatus(webrpc);
System.out.println("Login status: " + status);
if (status != 0) break;
Thread.sleep(2000);
}
int callbackPort = LIB.WebrpcClient_GetReceivePort(webrpc);
System.out.println("Callback TCP port: " + callbackPort);
Thread t = new Thread(() -> startReceivingCallbackData(webrpc, callbackPort), "webrpc-callback");
t.setDaemon(true);
t.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> LIB.WebrpcClient_Free(webrpc)));
while (true) {
System.out.println("Session count = " + LIB.WebrpcClient_SessionSize(webrpc));
Thread.sleep(3000);
}
}
}
FAQ
1. Cannot find the shared library
Ensure -L. / LD_LIBRARY_PATH / DYLD_LIBRARY_PATH / java.library.path points at the SDK library folder.
2. LoginStatus stays 0
Verify token/password, network access, and that you loaded the correct platform SDK.
3. Callback connect fails
Connect to 127.0.0.1:port after GetReceivePort. Do not hard-code the port.
4. Dial a peer
Call WebrpcClient_OpenSession(handle, peerToken, permission), then SendData with the returned sessionId.
5. Windows
The C/C++ samples use POSIX sockets. On Windows prefer Go / Java / Python / Rust, or adapt the TCP code to Winsock.