-
Notifications
You must be signed in to change notification settings - Fork 42
/
ReactiveChatApplication.java
70 lines (58 loc) · 2.46 KB
/
ReactiveChatApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package codes.monkey.reactivechat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
import reactor.core.publisher.Flux;
import reactor.core.publisher.UnicastProcessor;
import java.util.HashMap;
import java.util.Map;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
@SpringBootApplication
public class ReactiveChatApplication {
public static void main(String[] args) {
SpringApplication.run(ReactiveChatApplication.class, args);
}
@Bean
public UnicastProcessor<Event> eventPublisher(){
return UnicastProcessor.create();
}
@Bean
public Flux<Event> events(UnicastProcessor<Event> eventPublisher) {
return eventPublisher
.replay(25)
.autoConnect();
}
@Bean
public RouterFunction<ServerResponse> routes(){
return RouterFunctions.route(
GET("/"),
request -> ServerResponse.ok().body(BodyInserters.fromResource(new ClassPathResource("public/index.html")))
);
}
@Bean
public HandlerMapping webSocketMapping(UnicastProcessor<Event> eventPublisher, Flux<Event> events) {
Map<String, Object> map = new HashMap<>();
map.put("/websocket/chat", new ChatSocketHandler(eventPublisher, events));
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
simpleUrlHandlerMapping.setUrlMap(map);
//Without the order things break :-/
simpleUrlHandlerMapping.setOrder(10);
return simpleUrlHandlerMapping;
}
@Bean
public WebSocketHandlerAdapter handlerAdapter() {
return new WebSocketHandlerAdapter();
}
@Bean
public UserStats userStats(Flux<Event> events, UnicastProcessor eventPublisher){
return new UserStats(events, eventPublisher);
}
}