Spaces:
Sleeping
Sleeping
Commit
·
603b9e3
1
Parent(s):
fb1ed7f
changes
Browse files
src/main/java/ai/giskard/learnspringwebsockets/WebSocketBrokerConfig.java
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
7 |
+
// @Configuration
|
8 |
+
// @EnableWebSocketMessageBroker
|
9 |
+
public class WebSocketBrokerConfig implements WebSocketMessageBrokerConfigurer {
|
10 |
+
|
11 |
+
@Override
|
12 |
+
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
13 |
+
registry.addEndpoint("/ws").setAllowedOrigins("*");
|
14 |
+
}
|
15 |
+
|
16 |
+
@Override
|
17 |
+
public void configureMessageBroker(MessageBrokerRegistry config) {
|
18 |
+
config.setApplicationDestinationPrefixes("/app");
|
19 |
+
config.enableSimpleBroker("/topic", "/queue");
|
20 |
+
}
|
21 |
+
|
22 |
+
}
|
src/main/java/ai/giskard/learnspringwebsockets/WebSocketConfig.java
CHANGED
@@ -1,24 +1,52 @@
|
|
1 |
package ai.giskard.learnspringwebsockets;
|
2 |
|
|
|
3 |
import org.springframework.context.annotation.Configuration;
|
4 |
-
import org.springframework.
|
5 |
-
import org.springframework.web.socket.
|
6 |
-
import org.springframework.web.socket.
|
7 |
-
import org.springframework.web.socket.
|
|
|
|
|
|
|
8 |
|
9 |
@Configuration
|
10 |
-
@
|
11 |
-
public class WebSocketConfig implements
|
12 |
|
13 |
@Override
|
14 |
-
public void
|
15 |
-
registry.
|
16 |
}
|
17 |
|
18 |
-
@
|
19 |
-
public
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
}
|
23 |
|
24 |
}
|
|
|
1 |
package ai.giskard.learnspringwebsockets;
|
2 |
|
3 |
+
import org.springframework.context.annotation.Bean;
|
4 |
import org.springframework.context.annotation.Configuration;
|
5 |
+
import org.springframework.web.socket.CloseStatus;
|
6 |
+
import org.springframework.web.socket.WebSocketHandler;
|
7 |
+
import org.springframework.web.socket.WebSocketMessage;
|
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
|
15 |
+
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");
|
28 |
+
}
|
29 |
+
|
30 |
+
@Override
|
31 |
+
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
|
32 |
+
System.out.println("handleMessage");
|
33 |
+
}
|
34 |
+
|
35 |
+
@Override
|
36 |
+
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
37 |
+
System.out.println("handleTransportError");
|
38 |
+
}
|
39 |
+
|
40 |
+
@Override
|
41 |
+
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
|
42 |
+
System.out.println("afterConnectionClosed");
|
43 |
+
}
|
44 |
+
|
45 |
+
@Override
|
46 |
+
public boolean supportsPartialMessages() {
|
47 |
+
return false;
|
48 |
+
}
|
49 |
+
};
|
50 |
}
|
51 |
|
52 |
}
|