WireSpider is a simple and compact WebSocket (RFC6455) client written in Java.
- High performance
java.nio
based implementation. - Incredibly compact binary size.
- Android compatible. (Note that Java 7 language features must be enabled.)
Download JAR from Bintray, or write Gradle dependency as follows.
buildscript {
repositories {
jcenter()
}
}
dependencies {
compile 'net.kazyx:wirespider:1.3.1'
}
cd <root>/wirespider
./gradlew wirespider:assemble
Now you can find wirespider-x.y.z.jar
at <root>/wirespider/core/build/libs
Set external Base64 conversion function at first. The following snippets are the samples for Java 8 and Android.
Java 8
Base64.setEncoder(source -> java.util.Base64.getEncoder().encodeToString(source));
Android
Base64.setEncoder(source -> android.util.Base64.encodeToString(source, android.util.Base64.DEFAULT));
WebSocketFactory factory = new WebSocketFactory();
// It is recommended to use this WebSocketFactory while your process is alive.
URI uri = URI.create("ws://host:port/path"); // ws scheme
WebSocketHandler handler = new WebSocketHandler() {
@Override
public void onTextMessage(String message) {
// Received text message.
}
@Override
public void onBinaryMessage(byte[] message) {
// Received binary message.
}
@Override
public void onClosed(int code, String reason) {
// Connection is closed.
}
};
Blocking style
SessionRequest req = new SessionRequest.Builder(uri, handler)
.setConnectionTimeout(5, TimeUnit.SECONDS)
.build();
WebSocket websocket = factory.open(req);
Async style
SessionRequest req = new SessionRequest.Builder(uri, handler)
.build();
Future<WebSocket> futureWebSocket = factory.openAsync(req);
websocket.sendTextMessageAsync("Hello");
websocket.sendBinaryMessageAsync(new byte[]{0x01, 0x02, 0x03, 0x04});
try (PartialMessageWriter writer = websocket.newPartialMessageWriter()) {
writer.sendPartialFrameAsync("H", false);
writer.sendPartialFrameAsync("e", false);
writer.sendPartialFrameAsync("llo", true/*isFinal*/);
} // Don't forget to close PartialMessageWriter
websocket.closeAsync();
// WebSocketHandler.onClosed() will be called soon.
factory.destroy();
Use URI
created with wss
scheme instead of ws
.
URI uri = URI.create("wss://host:port/path");
Use SSLContext
on which the newer version of TLS is enabled,
since JDK7 disables client side TLSv1.1
and TLSv1.2
by default.
SSLContext context = SSLContext.getInstance("TLSv1.1");
context.init(null, null, null);
WebSocketFactory.setSslContext(context);
It is recommended to use Google Play services to enable TLSv1.1
and over.
dependencies {
compile 'com.google.android.gms:play-services-basement:+'
}
ProviderInstaller.installIfNeeded(getApplicationContext());
WebSocket extensions can be implemented with net.kazyx.wirespider.extension.Extension
interface.
Per-Message Deflate extension (RFC7692) is provided by wirespider-pmdeflate
placed under wirespider/permessage-deflate
.
Download JAR
or write Gradle dependency as follows.
dependencies {
compile 'net.kazyx:wirespider-pmdeflate:1.3.1'
}
Set DeflateRequest
into the SessionRequest
.
ExtensionRequest deflate = new DeflateRequest.Builder()
.setCompressionThreshold(100)
.build();
List<ExtensionRequest> extensions = new ArrayList<>();
extensions.add(deflate);
SessionRequest req = new SessionRequest.Builder(uri, handler)
.setExtensions(extensions)
.build();
No additional prevension required.
Contribution for bugfix, performance improvement, API refinement and extension implementation are welcome.
Note that JUnit test code is required for the pull request.
This software is released under the MIT License.