- Understand WebSocket.
- ... MessageBroker
- ... SockJS
- ... STOMP
- Spring Boot(Maven)
- WebSocket
- MessageBroker
- SockJS
- STOMP
HttpRequest -> Controller -> View
Main Method in Spring Boot.
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
WebSocketMessageBroker.
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/receive");
config.setApplicationDestinationPrefixes("/send");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/websocket").withSockJS();
}
}
Talk Controller
@Controller
public class TalkController {
// Member
static ArrayList<String> members = new ArrayList<>();
@RequestMapping("/")
public String index() {
return "index";
}
@MessageMapping("/in")
@SendTo("/receive/member")
public ArrayList<String> in (Talk talk) {
members.add(talk.getName());
return members;
}
@MessageMapping("/out")
@SendTo("/receive/member")
public ArrayList<String> out (Talk talk) {
members.remove(talk.getName());
return members;
}
@MessageMapping("/chats")
@SendTo("/receive/chats")
public String talk(Talk talk) {
return talk.getName() + " : " + talk.getContent();
}
}
2018.09.18. ~ 2018.09.19. WebSocket MessageBroker