Giriş
WebSocket çift yönlüdür. Aynı TCP bağlantısı gibidir. Açıklaması şöyle
registerWebSocketHandlers metodu
Handler TextWebSocketHandler'dan kalıtan bir sınıf olabilir.
Handler AbstractWebSocketHandler'dan kalıtan bir sınıf olabilir.
Örnek
Şöyle yaparız
Şöyle yaparız.
Şöyle yaparız.
WebSocket çift yönlüdür. Aynı TCP bağlantısı gibidir. Açıklaması şöyle
WebSocket establishes a connection using a handshake request, after which the communication becomes bidirectional where there is no concept of response to a request.Bağlantı bir kere kurulduktan sonra artık maliyeti yok gibidir. Açıklaması şöyle.
The crucial distinction between WebSockets and typical REST-based APIs is that clients based on the former protocol always establish long-running TCP connections and, once connected, the overhead they incur is practically negligible.WebSockets streaming için tek yöntem değil. Açıklaması şöyle
Websockets, SSE (Server-sent Events) or XHR streaming are all good protocol choices for streaming updates in a browser.
Diğer 
Bu sınıfı kullanmak istemiyorsak şöyle yaparız. Burada direkt bir HandlerMapping  yani SimpleUrlHandlerMapping yaratılıyor.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.reactive.socket.WebSocketHandler;
@Configuration
public class ReactiveServerWebSocketConfig {
  @Autowired
  private WebSocketHandler webSocketHandler;
  @Bean
  public HandlerMapping webSocketHandlerMapping() {
    Map<String, WebSocketHandler> map = new HashMap<>();
    map.put("/webflux", webSocketHandler);
    SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
    handlerMapping.setOrder(-1);
    handlerMapping.setUrlMap(map);
    return handlerMapping;
  }
}Handler TextWebSocketHandler'dan kalıtan bir sınıf olabilir.
Handler AbstractWebSocketHandler'dan kalıtan bir sınıf olabilir.
Örnek
Şöyle yaparız
public class WebSocketConfiguration implements WebSocketConfigurer {
  
  @Override
  public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(new WebSocketHandler(), "/presentation").setAllowedOrigins("*");
  }
}Şöyle yaparız.
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
  @Override
  public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(myWebSocketController(), "/chat")
      .addInterceptors(new HttpSessionHandshakeInterceptor())
      .setAllowedOrigins("*");
  }
  @Bean
  public MyWebSocketController myWebSocketController() {
    return new MyWebSocketController();
  }
}Şöyle yaparız.
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer
{
  @Override
  public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(myHandler(), "/websocket").withSockJS();
  }
  @Bean
  public ServerHandler myHandler() {
    return new ServerHandler();
  }
} 
Hiç yorum yok:
Yorum Gönder