An experimental WebSocket implementation. Not recommended for production use as this currently doesn't strictly adhere to the spec.
WebSocketClient client = new WebSocketClient(URI.create("wss://echo.casterlabs.co"));
client.setListener(new WebSocketListener() {
@Override
public void onOpen(WebSocketClient client, Map<String, String> headers, @Nullable String acceptedProtocol) {
System.out.println("Connected. Headers: " + headers);
}
@Override
public void onText(WebSocketClient client, String string) {
System.out.println("Got text message: '" + string + "'");
}
@Override
public void onBinary(WebSocketClient client, byte[] bytes) {
System.out.println("Got binary message: len=" + bytes.length);
}
@Override
public void onClosed(WebSocketClient client) {
System.out.println("Closed");
}
});
client.connect(10_000, 5_000);
Scanner in = new Scanner(System.in);
while (true) {
client.send(in.nextLine());
}
// Type into the console to send messages to the server. It will echo them back :^)Replace VERSION_OR_HASH with the latest version or commit in this repo and make sure to add the Repository to your build system.
Maven
<dependency>
<groupId>co.casterlabs.commons</groupId>
<artifactId>websocket</artifactId>
<version>VERSION_OR_HASH</version>
</dependency>Gradle
dependencies {
implementation 'co.casterlabs.commons:websocket:VERSION_OR_HASH'
}SBT
libraryDependencies += "co.casterlabs.commons" % "websocket" % "VERSION_OR_HASH"
Leiningen
:dependencies [[co.casterlabs.commons/websocket "VERSION_OR_HASH"]]