Skip to content

Commit

Permalink
feat: session connect&disconnect Handler 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yunuo46 committed May 10, 2024
1 parent 0161414 commit 8192173
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/main/java/getaguitar/site/demo/Config/StompConfig.java
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 src/main/java/getaguitar/site/demo/Controller/StompController.java
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();
}
}
9 changes: 2 additions & 7 deletions src/main/java/getaguitar/site/demo/Entity/UserEntity.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package getaguitar.site.demo.Entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.*;
import lombok.*;
import org.antlr.v4.runtime.misc.NotNull;

Expand All @@ -14,7 +9,7 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "user")
@Table(name = "user", indexes = @Index(columnList = "username"))
@Entity
public class UserEntity {
@Id
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/getaguitar/site/demo/Interceptor/StompHandler.java
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;
}

}
}
6 changes: 3 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ spring.jpa.properties.hibernate.format_sql=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# LocalTest
# spring.datasource.url=jdbc:mysql://localhost:3306/getaguitar?useSSL=false&allowPublicKeyRetrieval=true
# spring.datasource.username=root
# spring.datasource.password=zx8320
spring.datasource.url=jdbc:mysql://localhost:3306/getaguitar?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=zx8320

0 comments on commit 8192173

Please sign in to comment.