Java wrappers for libdatachannel, a WebRTC Data Channels standalone implementation in C++.
implementation("tel.schich:libdatachannel-java:0.22.6.1")
<dependency>
<groupId>tel.schich</groupId>
<artifactId>libdatachannel-java</artifactId>
<version>0.22.6.1</version>
</dependency>
Additionally, pull the architecture-specific native components. Alternatively, use the libdatachannel-java-arch-detect
module, which includes common architectures and has code to detect which one to apply.
var cfg = RTCConfiguration.of("stun.l.google.com:19302");
// try with resources to cleanup peer when done
try (var peer = RTCPeerConnection.createPeer(cfg)) {
// when complete send sdp to remote peer
peer.onGatheringStateChange((pc, state) -> {
if (RTC_GATHERING_COMPLETE == state) {
var sdp = pc.localDescription();
System.out.println(sdp);
}
});
// create data channel
var channel = peer.createDataChannel("test");
// wait for local sdp...
// then set answer from remote peer
peer.setAnswer(readInput());
// register message callback
channel.onMessage((c, message, size) -> System.out.println("Incoming message: " + new String(message)));
// block until channel is closed
CompletableFuture<Void> future = new CompletableFuture<>();
channel.onClose(c -> future.completeAsync(() -> null));
future.join();
}
- Web Example: https://pschichtel.github.io/libdatachannel-java/ (Source)
See tests for more examples