Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private channel broadcasting #40

Merged
merged 3 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.swiftgathering_server.controller;

import com.example.swiftgathering_server.dto.CreateSessionRequestDto;
import com.example.swiftgathering_server.dto.CreatedSessionIdDto;
import com.example.swiftgathering_server.dto.ParticipateSessionRequestDto;
import com.example.swiftgathering_server.service.GatheringSessionService;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,9 +16,9 @@ public class GatheringSessionController {
private final GatheringSessionService gatheringSessionService;

@PostMapping
public ResponseEntity<Void> createSession(@RequestBody CreateSessionRequestDto requestDto) {
gatheringSessionService.createSession(requestDto);
return ResponseEntity.ok().build();
public ResponseEntity<CreatedSessionIdDto> createSession(@RequestBody CreateSessionRequestDto requestDto) {
CreatedSessionIdDto createdSessionIdDto = gatheringSessionService.createSession(requestDto);
return ResponseEntity.ok(createdSessionIdDto);
}

@PatchMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GatheringSession {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private UUID sessionId = UUID.randomUUID();
private final UUID sessionId = UUID.randomUUID();
private LocalDateTime createdTime;

@ElementCollection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.swiftgathering_server.dto;

import lombok.Getter;

@Getter
public class CreatedSessionIdDto {
private final String sessionId;

public CreatedSessionIdDto(String sessionId) {
this.sessionId = sessionId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

@Getter
public class GatheringSessionNotificationDto {
final private Long sessionId;
final private String sessionId;
final private List<Long> participantIds;

public GatheringSessionNotificationDto(Long sessionId, List<Long> participantIds) {
public GatheringSessionNotificationDto(String sessionId, List<Long> participantIds) {
this.sessionId = sessionId;
this.participantIds = participantIds;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import com.example.swiftgathering_server.domain.*;
import com.example.swiftgathering_server.dto.CreateSessionRequestDto;
import com.example.swiftgathering_server.dto.CreatedSessionIdDto;
import com.example.swiftgathering_server.dto.GatheringSessionNotificationDto;
import com.example.swiftgathering_server.dto.ParticipateSessionRequestDto;
import com.example.swiftgathering_server.exception.ResourceNotFoundException;
import com.example.swiftgathering_server.repository.GatheringSessionRepository;
import com.example.swiftgathering_server.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -28,9 +25,9 @@ public class GatheringSessionService {
private final MemberRepository memberRepository;
private final GatheringSessionRepository gatheringSessionRepository;
// private final FlagLocationRepository flagLocationRepository;
private final AmqpTemplate amqpTemplate;
private final SimpMessageSendingOperations messagingTemplate;

public void createSession(CreateSessionRequestDto requestDto) {
public CreatedSessionIdDto createSession(CreateSessionRequestDto requestDto) {
List<GatheringSessionMember> sessionMembers = memberRepository
.findAllByIds(requestDto.getGuestIds()).stream()
.map(member -> GatheringSessionMember.builder()
Expand All @@ -47,6 +44,8 @@ public void createSession(CreateSessionRequestDto requestDto) {

GatheringSession savedSession = gatheringSessionRepository.save(session);
notifyClients(savedSession);

return new CreatedSessionIdDto(savedSession.getSessionId().toString());
}

public void participateSession(ParticipateSessionRequestDto requestDto) {
Expand All @@ -71,11 +70,8 @@ private void notifyClients(GatheringSession session) {
.collect(Collectors.toList());

for (Long memberId : memberIds) {
Queue queue = new Queue("swift-gathering.queue." + memberId, false);
DirectExchange exchange = new DirectExchange("swift-gathering.exchange." + memberId);
Binding binding = BindingBuilder.bind(queue).to(exchange).with("swift-gathering.routing." + memberId);
GatheringSessionNotificationDto notification = new GatheringSessionNotificationDto(session.getId(), memberIds);
amqpTemplate.convertAndSend(exchange.getName(), binding.getRoutingKey(), notification);
GatheringSessionNotificationDto notification = new GatheringSessionNotificationDto(session.getSessionId().toString(), memberIds);
messagingTemplate.convertAndSend("/topic/private." + memberId, notification);
}
}
}
Loading