Forked from MeiyappanKannappa/SimpleWebSocketHandler.java
Created
September 9, 2026 04:53
-
-
Save chinhvo/174c7acce648fb19b68c04877500148c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Configuration | |
| @EnableWebFlux | |
| @Slf4j | |
| public class SimpleWebSocketHandler implements WebSocketHandler { | |
| @Bean | |
| public SimpleUrlHandlerMapping simpleUrlHandlerMapping() { | |
| Map<String, WebSocketHandler> urlMap = new HashMap<>(); | |
| urlMap.put("/ws", this::handle); | |
| SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); | |
| handlerMapping.setUrlMap(urlMap); | |
| handlerMapping.setOrder(1); | |
| return handlerMapping; | |
| } | |
| @Override | |
| public Mono<Void> handle(WebSocketSession session) { | |
| UriComponentsBuilder builder = UriComponentsBuilder.fromUri(session.getHandshakeInfo().getUri()); | |
| Map<String,String> queryParams = builder.build().getQueryParams().toSingleValueMap(); | |
| String uniqueId = queryParams.get("email"); | |
| log.info("Websocket connected for id "+uniqueId); | |
| Flux<WebSocketMessage> messageFlux = session.receive() | |
| .map(msg -> "Server received: " + msg.getPayloadAsText()) | |
| .map(session::textMessage); | |
| return session.send(messageFlux); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment