- The simple library is used to support the forward proxy of websocket for applications which are based in the framework of
spring boot. - The library is based on
JDK8.
spring-websocket-forward is available from Maven Central.
<dependency>
<groupId>com.github.ji4597056</groupId>
<artifactId>spring-websocket-forward</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>Spring Cloud Zuul and Spring Boot Websocket do not support the forwarding proxy of web sockets. However, we usually need the proxy of websocket.
- Enable it like so:
@EnableWsForward
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}- Then in your spring application properties(e.g
application.yml)
ws:
forward:
enabled: true
handlers:
test:
enabled: true
prefix: /test
uri: /**
serviceId: test-client
allowedOrigins: "*"
withJs: falseWith this you can accurately get websockets support from proxied back-end service.
| property | type | required | default | introduction |
|---|---|---|---|---|
| enabled | boolean | false | true | enabled or not |
| prefix | string | false | null | uri route prefix |
| uri | string | true | null | uri |
| withJs | boolean | false | false | with JS or not |
| forwardPrefix | string | false | null | forwarding uri route prefix |
| serviceId | string | false | null | find forwarding addresses from registration center by service id |
| listOfServers | string[] | false | null | find forwarding addresses from list of services if serviceId is not set,eg:localhost:8080 |
| allowedOrigins | string[] | false | "*" | allowed origins |
| handlerClass | string | false | null | name of AbstractWsServerHandler class,use global handler if there is not set |
| interceptorClasses | string[] | false | null | name of HandshakeInterceptor class,use global interceptors if there is not set |
if you want to set your own AbstractWsServerHandler or HandshakeInterceptor,config bean of WsHandlerRegistration like:
@Bean
public WsHandlerRegistration wsHandlerRegistration() {
WsHandlerRegistration registration = new DefaultWsHandlerRegistration();
registration.addHandler(new TestWsServerHandler());
registration.addInterceptor(new TestHandshakeInterceptor());
return registration;
}Then you can set the property of handlerClass or interceptorClasses.