This is a STOMP protocol implementation.
The following example is taken from Spring's "Using WebSocket to build an interactive web application" guide but with SockJS disabled.
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket");
}
}
@Controller
public class GreetingController {
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(1000); // simulated delay
return new Greeting("Hello, " + message.getName() + "!");
}
}
See the included client example module.
final StompClient stompSocket = new StompClient(URI.create("ws:https://localhost:8080/gs-guide-websocket"));
// Wait for a connection to establish
boolean connected;
try {
connected = stompSocket.connectBlocking();
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
if (!connected) {
System.out.println("Failed to connect to the socket");
return;
}
// Subscribing to a topic
stompSocket.subscribe("/topic/greetings", new StompMessageListener() {
@Override
public void onMessage(String message) {
System.out.println("Server message: " + message);
// Disconnect
stompSocket.close();
}
});
// Sending JSON message to a server
String message = "{\"name\": \"Jack\"}";
stompSocket.send("/app/hello", message);