Skip to content

Instantly share code, notes, and snippets.

@grunka
Last active January 18, 2021 10:32
Show Gist options
  • Save grunka/a0b05c0c96cabd379d6cbb3424c0323c to your computer and use it in GitHub Desktop.
Save grunka/a0b05c0c96cabd379d6cbb3424c0323c to your computer and use it in GitHub Desktop.
WebSockets in Dropwizard

Instructions for adding in websocket support to your Dropwizard service without any more dependencies than Jettys websocket implementation and the websocket api.

The pom.xml below contains the required dependency, update jetty version as needed.

The WebSocketsInDropwizardApplication.java shows what is needed in the Dropwizard java implementation in your app to configure websocket support and register server endpoints.

Finally the WebsocketImplementation.java just contains a basically empty server endpoint.

<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>javax-websocket-server-impl</artifactId>
<version>9.4.35.v20201120</version>
</dependency>
@ServerEndpoint("/socket-endpoint")
public class WebSocketImplementation {
@OnOpen
public void onOpen(Session session) throws IOException {
session.getBasicRemote().sendText("Hello World");
}
@OnMessage
public void onMessage(Session session, String message) throws IOException {
}
@OnError
public void onError(Session session, Throwable throwable) throws IOException {
}
@OnClose
public void onClose(Session session) throws IOException {
}
}
@Override
public void run(ApplicationConfiguration applicationConfiguration, Environment environment) throws IOException {
WebSocketServerContainerInitializer.configure(environment.getApplicationContext(), (servletContext, serverContainer) -> {
// All the default values
serverContainer.getPolicy().setMaxTextMessageSize(64 * 1024);
serverContainer.getPolicy().setMaxBinaryMessageSize(64 * 1024);
serverContainer.getPolicy().setAsyncWriteTimeout(Duration.ofMinutes(1).toMillis());
serverContainer.getPolicy().setIdleTimeout(Duration.ofMinutes(5).toMillis());
// Registering endpoints, the first way is the "normal" one where you get one instance per connection
serverContainer.addEndpoint(WebsocketImplementation.class);
// or this second way where a single instance is reused
serverContainer.addEndpoint(createServerEndpointConfigFromInstance(new WebsocketImplementation()));
});
}
private ServerEndpointConfig createServerEndpointConfigFromInstance(Object instance) {
Class<?> instanceClass = instance.getClass();
ServerEndpoint annotation = instanceClass.getAnnotation(ServerEndpoint.class);
if (annotation == null) {
throw new IllegalArgumentException("Instance is not annotated with @ServerEndpoint");
}
return ServerEndpointConfig.Builder.create(instanceClass, annotation.value()).configurator(new ServerEndpointConfig.Configurator() {
@SuppressWarnings("unchecked")
@Override
public <T> T getEndpointInstance(Class<T> endpointClass) {
return (T) instance;
}
}).build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment