The purpose of this project is to provide a tools to test notification/messaging service using WebSockets (Angular WebSocket client example).
- Clone the repository
git clone https://github.com/batiwo/websocket-server-utils.git - Run the distributed JAR
distribution/websocket-server-utils-x.x.x-SNAPSHOT.jarwithjava -jar websocket-server-utils-x.x.x-SNAPSHOT.jar - OR build the
spring-websocketproject withmvn install - Use a websocket client like https://github.com/batiwo/spring-websocket-angular6.
The endpoint and the streamUrl (/topic by default) are both configurable from application.properties or by JVM arguments (Using -Dwebsocket.endpoint=/socket1 and -Dwebsocket.handler=/topic1).
The spring server is a basic spring-boot api with the spring-websocket dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>The WebSocketConfiguration is minimalist.
We just configure the MessageBroker with an Endpoint and setAllowedOrigins to anyone.
The Endpoint /socket means that you will connect to the ws://server-url/socket with your clients.
@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {
@Value("${websocket.endpoint}")
private String _websocketEndpoint;
@Value("${websocket.handler}")
private String _stompHandler;
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
System.out.println("Using endpoint : " + _websocketEndpoint);
registry.addEndpoint(_websocketEndpoint).setAllowedOrigins("*");
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker(_stompHandler);
}
}