Spaces:
Sleeping
Sleeping
Commit
·
8cf3e85
1
Parent(s):
603b9e3
changes
Browse files
src/main/java/ai/giskard/learnspringwebsockets/WebSocketBrokerConfig.java
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
package ai.giskard.learnspringwebsockets;
|
2 |
|
|
|
3 |
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
|
|
4 |
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
5 |
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
6 |
|
|
|
1 |
package ai.giskard.learnspringwebsockets;
|
2 |
|
3 |
+
import org.springframework.context.annotation.Configuration;
|
4 |
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
5 |
+
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
6 |
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
7 |
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
8 |
|
src/main/java/ai/giskard/learnspringwebsockets/WebSocketConfig.java
CHANGED
@@ -2,13 +2,19 @@ package ai.giskard.learnspringwebsockets;
|
|
2 |
|
3 |
import org.springframework.context.annotation.Bean;
|
4 |
import org.springframework.context.annotation.Configuration;
|
5 |
-
import org.springframework.
|
6 |
-
import org.springframework.
|
7 |
-
import org.springframework.web.socket
|
8 |
-
import org.springframework.web.socket.WebSocketSession;
|
9 |
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
10 |
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
11 |
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
@Configuration
|
14 |
@EnableWebSocket
|
@@ -16,12 +22,114 @@ public class WebSocketConfig implements WebSocketConfigurer {
|
|
16 |
|
17 |
@Override
|
18 |
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
19 |
-
registry.addHandler(myHandler(), "/ws")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
}
|
21 |
|
22 |
-
@Bean
|
23 |
public WebSocketHandler myHandler() {
|
24 |
return new WebSocketHandler() {
|
|
|
25 |
@Override
|
26 |
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
27 |
System.out.println("afterConnectionEstablished");
|
|
|
2 |
|
3 |
import org.springframework.context.annotation.Bean;
|
4 |
import org.springframework.context.annotation.Configuration;
|
5 |
+
import org.springframework.http.server.ServerHttpRequest;
|
6 |
+
import org.springframework.http.server.ServerHttpResponse;
|
7 |
+
import org.springframework.web.socket.*;
|
|
|
8 |
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
9 |
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
10 |
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
|
11 |
+
import org.springframework.web.socket.server.RequestUpgradeStrategy;
|
12 |
+
import org.springframework.web.socket.server.support.AbstractHandshakeHandler;
|
13 |
+
|
14 |
+
import java.io.IOException;
|
15 |
+
import java.security.Principal;
|
16 |
+
import java.util.List;
|
17 |
+
import java.util.Map;
|
18 |
|
19 |
@Configuration
|
20 |
@EnableWebSocket
|
|
|
22 |
|
23 |
@Override
|
24 |
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
25 |
+
registry.addHandler(myHandler(), "/ws").setHandshakeHandler(new AbstractHandshakeHandler() {
|
26 |
+
@Override
|
27 |
+
public RequestUpgradeStrategy getRequestUpgradeStrategy() {
|
28 |
+
System.out.println("public RequestUpgradeStrategy getRequestUpgradeStrategy() {");
|
29 |
+
return super.getRequestUpgradeStrategy();
|
30 |
+
}
|
31 |
+
|
32 |
+
@Override
|
33 |
+
public void setSupportedProtocols(String... protocols) {
|
34 |
+
System.out.println("public void setSupportedProtocols(String... protocols) {");
|
35 |
+
super.setSupportedProtocols(protocols);
|
36 |
+
}
|
37 |
+
|
38 |
+
@Override
|
39 |
+
public String[] getSupportedProtocols() {
|
40 |
+
System.out.println("public String[] getSupportedProtocols() {");
|
41 |
+
return super.getSupportedProtocols();
|
42 |
+
}
|
43 |
+
|
44 |
+
@Override
|
45 |
+
public void start() {
|
46 |
+
System.out.println("public void start() {");
|
47 |
+
super.start();
|
48 |
+
}
|
49 |
+
|
50 |
+
@Override
|
51 |
+
protected void doStart() {
|
52 |
+
System.out.println("protected void doStart() {");
|
53 |
+
super.doStart();
|
54 |
+
}
|
55 |
+
|
56 |
+
@Override
|
57 |
+
public void stop() {
|
58 |
+
System.out.println("public void stop() {");
|
59 |
+
super.stop();
|
60 |
+
}
|
61 |
+
|
62 |
+
@Override
|
63 |
+
protected void doStop() {
|
64 |
+
System.out.println("protected void doStop() {");
|
65 |
+
super.doStop();
|
66 |
+
}
|
67 |
+
|
68 |
+
@Override
|
69 |
+
public boolean isRunning() {
|
70 |
+
System.out.println("public boolean isRunning() {");
|
71 |
+
return super.isRunning();
|
72 |
+
}
|
73 |
+
|
74 |
+
@Override
|
75 |
+
protected void handleInvalidUpgradeHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
|
76 |
+
System.out.println("protected void handleInvalidUpgradeHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {");
|
77 |
+
super.handleInvalidUpgradeHeader(request, response);
|
78 |
+
}
|
79 |
+
|
80 |
+
@Override
|
81 |
+
protected void handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
|
82 |
+
System.out.println("protected void handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {");
|
83 |
+
super.handleInvalidConnectHeader(request, response);
|
84 |
+
}
|
85 |
+
|
86 |
+
@Override
|
87 |
+
protected boolean isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders) {
|
88 |
+
System.out.println("protected boolean isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders) {");
|
89 |
+
return super.isWebSocketVersionSupported(httpHeaders);
|
90 |
+
}
|
91 |
+
|
92 |
+
@Override
|
93 |
+
protected String[] getSupportedVersions() {
|
94 |
+
System.out.println("protected String[] getSupportedVersions() {");
|
95 |
+
return super.getSupportedVersions();
|
96 |
+
}
|
97 |
+
|
98 |
+
@Override
|
99 |
+
protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) {
|
100 |
+
System.out.println("protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) {");
|
101 |
+
super.handleWebSocketVersionNotSupported(request, response);
|
102 |
+
}
|
103 |
+
|
104 |
+
@Override
|
105 |
+
protected boolean isValidOrigin(ServerHttpRequest request) {
|
106 |
+
System.out.println("protected boolean isValidOrigin(ServerHttpRequest request) {");
|
107 |
+
return super.isValidOrigin(request);
|
108 |
+
}
|
109 |
+
|
110 |
+
@Override
|
111 |
+
protected String selectProtocol(List<String> requestedProtocols, WebSocketHandler webSocketHandler) {
|
112 |
+
System.out.println("protected String selectProtocol(List<String> requestedProtocols, WebSocketHandler webSocketHandler) {");
|
113 |
+
return super.selectProtocol(requestedProtocols, webSocketHandler);
|
114 |
+
}
|
115 |
+
|
116 |
+
@Override
|
117 |
+
protected List<WebSocketExtension> filterRequestedExtensions(ServerHttpRequest request, List<WebSocketExtension> requestedExtensions, List<WebSocketExtension> supportedExtensions) {
|
118 |
+
System.out.println("protected List<WebSocketExtension> filterRequestedExtensions(ServerHttpRequest request, List<WebSocketExtension> requestedExtensions, List<WebSocketExtension> supportedExtensions) {");
|
119 |
+
return super.filterRequestedExtensions(request, requestedExtensions, supportedExtensions);
|
120 |
+
}
|
121 |
+
|
122 |
+
@Override
|
123 |
+
protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
|
124 |
+
System.out.println("protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {");
|
125 |
+
return super.determineUser(request, wsHandler, attributes);
|
126 |
+
}
|
127 |
+
});
|
128 |
}
|
129 |
|
|
|
130 |
public WebSocketHandler myHandler() {
|
131 |
return new WebSocketHandler() {
|
132 |
+
|
133 |
@Override
|
134 |
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
135 |
System.out.println("afterConnectionEstablished");
|