-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: session connect&disconnect Handler 추가
- Loading branch information
Showing
5 changed files
with
84 additions
and
12 deletions.
There are no files selected for viewing
12 changes: 10 additions & 2 deletions
12
src/main/java/getaguitar/site/demo/Config/StompConfig.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,33 @@ | ||
package getaguitar.site.demo.Config; | ||
|
||
import getaguitar.site.demo.Interceptor.StompHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.simp.config.ChannelRegistration; | ||
import org.springframework.messaging.simp.config.MessageBrokerRegistry; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; | ||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
@RequiredArgsConstructor | ||
public class StompConfig implements WebSocketMessageBrokerConfigurer { | ||
|
||
final StompHandler stompHandler; | ||
|
||
@Override | ||
public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
registry.addEndpoint("/ws").setAllowedOriginPatterns("*"); | ||
} | ||
|
||
@Override | ||
public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
// send로 시작되는 메시지는 message-handling methods로 라우팅된다. | ||
registry.setApplicationDestinationPrefixes("/app"); | ||
} | ||
|
||
//registry.enableSimpleBroker("/topic"); | ||
@Override | ||
public void configureClientInboundChannel(ChannelRegistration registration) { | ||
registration.interceptors(stompHandler); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/getaguitar/site/demo/Controller/StompController.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package getaguitar.site.demo.Controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.messaging.handler.annotation.SendTo; | ||
import org.springframework.messaging.simp.SimpMessageSendingOperations; | ||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.socket.messaging.SessionConnectEvent; | ||
import org.springframework.web.socket.messaging.SessionDisconnectEvent; | ||
|
||
import java.util.Map; | ||
|
||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class StompController { | ||
private static final Logger LOGGER = LoggerFactory.getLogger( StompController.class ); | ||
|
||
private final SimpMessageSendingOperations simpleMessageSendingOperations; | ||
|
||
@EventListener | ||
public void handleWebSocketConnectListener(SessionConnectEvent event) { | ||
StompHeaderAccessor headerAccesor = StompHeaderAccessor.wrap(event.getMessage()); | ||
String sessionId = headerAccesor.getSessionId(); | ||
} | ||
|
||
@EventListener | ||
public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) { | ||
StompHeaderAccessor headerAccesor = StompHeaderAccessor.wrap(event.getMessage()); | ||
String sessionId = headerAccesor.getSessionId(); | ||
} | ||
} |
This file contains 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
33 changes: 33 additions & 0 deletions
33
src/main/java/getaguitar/site/demo/Interceptor/StompHandler.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package getaguitar.site.demo.Interceptor; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; | ||
import org.springframework.messaging.support.ChannelInterceptor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class StompHandler implements ChannelInterceptor { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger( StompHandler.class ); | ||
|
||
@Override | ||
public void postSend(Message message, MessageChannel channel, boolean sent) { | ||
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message); | ||
String sessionId = accessor.getSessionId(); | ||
|
||
switch ((accessor.getCommand())) { | ||
case CONNECT: | ||
LOGGER.info("Received a new web socket connection {}", sessionId); | ||
break; | ||
case DISCONNECT: | ||
LOGGER.info("sessionId Disconnected {}", sessionId); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
} | ||
} |
This file contains 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