Skip to content

Commit

Permalink
[FEAT] location socket join and share
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Oct 12, 2023
1 parent d95bede commit 2a799b2
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RabbitMQConfig {

private static final String QUEUE_NAME = "map.queue";
private static final String EXCHANGE_NAME = "map.exchange";
private static final String ROUTING_KEY = "group.*";
private static final String ROUTING_KEY = "plan.*";
private final RabbitMQProperties rabbitMQProperties;
private final ObjectMapper objectMapper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception {
"auth/refresh",
"auth/save",
"auth/login",
"member/duplicate/**")
"member/duplicate/**",
"socket/**")
.permitAll())
.authorizeHttpRequests(
x -> x.requestMatchers("/test/**").permitAll().anyRequest().authenticated())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
Expand All @@ -18,11 +19,13 @@ public class StompConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void registerStompEndpoints(final StompEndpointRegistry registry) {
registry.addEndpoint("/socket").setAllowedOrigins("*");
registry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS();
}

@Override
public void configureMessageBroker(final MessageBrokerRegistry registry) {
registry.setPathMatcher(new AntPathMatcher("."));

registry.enableStompBrokerRelay("/topic", "/queue", "/exchange", "/amq/queue")
.setRelayHost(rabbitMQProperties.getHost())
.setRelayPort(61613)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.List;

@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {
public class WebConfig extends WebMvcConfigurationSupport {

@Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.twtw.backend.domain.location.controller;

import com.twtw.backend.domain.location.dto.request.LocationRequest;
import com.twtw.backend.domain.location.service.LocationService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;

import java.util.UUID;

@Controller
@RequiredArgsConstructor
public class LocationController {

private static final String EXCHANGE_NAME = "map.exchange";
private static final String ROUTING_KEY = "plan.*";
private final RabbitTemplate rabbitTemplate;
private final LocationService locationService;

@MessageMapping("map.join.{planId}")
public void join(@DestinationVariable final UUID planId, final LocationRequest locationRequest) {
rabbitTemplate.convertAndSend(EXCHANGE_NAME, ROUTING_KEY + planId, locationService.addInfo(locationRequest));
}

@MessageMapping("map.share.{planId}")
public void share(@DestinationVariable final UUID planId, final LocationRequest locationRequest) {
rabbitTemplate.convertAndSend(EXCHANGE_NAME, ROUTING_KEY + planId, locationService.addInfo(locationRequest));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.twtw.backend.domain.location.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class LocationRequest {
private String nickname;
private Double x;
private Double y;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.twtw.backend.domain.location.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
@Builder
@AllArgsConstructor
public class LocationResponse {
private String nickname;
private Double x;
private Double y;
private LocalDateTime time;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.twtw.backend.domain.location.mapper;

import com.twtw.backend.domain.location.dto.request.LocationRequest;
import com.twtw.backend.domain.location.dto.response.LocationResponse;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class LocationMapper {
public LocationResponse toResponse(final LocationRequest locationRequest, final LocalDateTime now) {
return LocationResponse.builder()
.x(locationRequest.getX())
.y(locationRequest.getY())
.nickname(locationRequest.getNickname())
.time(now)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.twtw.backend.domain.location.service;

import com.twtw.backend.domain.location.dto.request.LocationRequest;
import com.twtw.backend.domain.location.dto.response.LocationResponse;
import com.twtw.backend.domain.location.mapper.LocationMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class LocationService {

private final LocationMapper locationMapper;

public LocationResponse addInfo(final LocationRequest locationRequest) {
return locationMapper.toResponse(locationRequest, LocalDateTime.now());
}
}

0 comments on commit 2a799b2

Please sign in to comment.