Java wrappers for libdatachannel, a WebRTC Data Channels standalone implementation in C++.
implementation("tel.schich.libdatachannel:libdatachannel-java:0.21.1")
<dependency>
<groupId>tel.schich.libdatachannel</groupId>
<artifactId>libdatachannel-java</artifactId>
<version>0.21.1</version>
</dependency>
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