Giriş
AbstractSecurityWebSocketMessageBrokerConfigurer sınıfı ile kardeştir.
Broker ve Controller'ı Birleştirmek
3 tane anotasyon var
AbstractSecurityWebSocketMessageBrokerConfigurer sınıfı ile kardeştir.
Broker ve Controller'ı Birleştirmek
3 tane anotasyon var
- @SubscribeMapping
- @MessageMapping - Stomp istemcisi tarafından gönderilen mesajı okur
- @MessageMapping + @SendTo
- @MessageExceptionHandler
- Broker'dan mesaj göndermek için MessageSendingOperations arayüzü kullanılır. Bu arayüzü gerçekleştiren sınıf SimpMessagingTemplate 
Örnek
Controller 'a gelen mesajı broker 'a göndermek için şöyle yaparız.
configureClientInboundChannel metodu
Örnek
@Controller
public class GreetingController {
  @MessageMapping("/hello")
  @SendTo("/topic/greetings")
  public TestResponse greeting(HelloMessage message) throws Exception {
    Thread.sleep(1000); // simulated delay
    return new TestResponse("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
  }
}Kullanım
Örnek
En basit haliyle kullanmak için şöyle yaparız. 
- Burada Stomp istemcilerinin bağlanacağı adres "/websocket-sockjs-stomp". 
- Stomp mesajlarının yayınlanacağı (publish) adres ise "/queue" ve "/topic". Stomp istemcileri bu adreslere abone olurlar.
- Eğer Stomp istemcisi mesaj dinlemek yanında, mesaj da yayınlıyorsa, Stomp istemcilerinin mesaj gönderebilecekler adres ise "/app" ile başlıyor. 
Örneğin 
- "/app/request"
- "/app/subscribe"
@Configuration@EnableWebSocketMessageBrokerpublic class StompWebSocketConfig implements WebSocketMessageBrokerConfigurer {@Overridepublic void registerStompEndpoints(StompEndpointRegistry registry) {registry.addEndpoint("/websocket-sockjs-stomp").withSockJS();}@Overridepublic void configureMessageBroker(MessageBrokerRegistry registry) {registry.enableSimpleBroker("/queue", "/topic");registry.setApplicationDestinationPrefixes("/app");}}
Örnek
Şöyle yaparız
- enableSimpleBroker() çağrısına genellikle "/topic" ve "/queue" verilir. Topic ile spring kodundan herkese mesaj gönderilir. "/queue" ile spring kodundan sadece belli bir kullanıcıya mesaj gönderilir.
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
  @Override
  public void configureClientInboundChannel(ChannelRegistration channelRegistration) {
    channelRegistration.taskExecutor().corePoolSize(4).maxPoolSize(8);
  }
  ...
}- enableSimpleBroker() çağrısına genellikle "/topic" ve "/queue" verilir. Topic ile spring kodundan herkese mesaj gönderilir. "/queue" ile spring kodundan sadece belli bir kullanıcıya mesaj gönderilir.
- setApplicationDestinationPrefixes() çağrısı ile verilen string, aynı string'e sahip @MessageMapping anotasyonuna sahip kodların tetiklenmesini sağlar. Yani mesaj gönderilecek endpoint'lerin ismidir. Bu sınıflar aynı zamanda @SendTo anotasyonuna da sahiptirler. Böylece kendisine gelen mesajı işleyerek bir başka topic'e yayınlarlar.
Örnek
Şöyle yaparız.
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
  @Override
  public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setApplicationDestinationPrefixes("/app");
  }
  ...
}websocket istemcisi bu adrese bağlanır. WebSocket, Stomp, SockJS İlişkisi yazısına bakabilirsiniz.
Örnek
Elimizde şöyle bir kod olsun
@Overridepublic void registerStompEndpoints(StompEndpointRegistry registry) {registry.addEndpoint("/greeting");}
websocket istemcisi bu adrese bağlanır. Şöyle yaparız
var socket = new WebSocket('ws://localhost:8080/greeting');ws = Stomp.over(socket);
Örnek - SockJS ile
Şöyle yaparız.
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
  registry.addEndpoint("/gs-guide-websocket").withSockJS();
} 
Hiç yorum yok:
Yorum Gönder