Skip to content

Commit

Permalink
feat : get attachment list by socket
Browse files Browse the repository at this point in the history
  • Loading branch information
NameIsUser06 committed Dec 27, 2023
1 parent 58888ab commit 5cb2ac4
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.bssm.attachit.domain.attachment.exception;

import org.bssm.attachit.global.error.exception.AttachItException;
import org.bssm.attachit.global.error.exception.ErrorCode;

public class SocketIOException extends AttachItException {
public static final SocketIOException EXCEPTION = new SocketIOException(ErrorCode.SOCKET_OUTPUT);
public SocketIOException(ErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.bssm.attachit.domain.attachment.service;

import lombok.RequiredArgsConstructor;
import org.bssm.attachit.domain.attachment.domain.Attachment;
import org.bssm.attachit.domain.attachment.repository.AttachmentRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class GetAttachmentListService {

private final AttachmentRepository attachmentRepository;

public List<Attachment> execute() {
return attachmentRepository.findAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public enum ErrorCode {
FILE_ERROR(500, "파일 오류"),

UNAUTHORIZED(403, "권한이 올바르지 않습니다"),
BAD_REQUEST(400, "잘못된 요청입니다");
BAD_REQUEST(400, "잘못된 요청입니다"),

SOCKET_OUTPUT(500, "소켓 데이터 전송 에러");

private final int status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bssm.attachit.global.jwt.util.JwtUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -20,6 +21,7 @@
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
@EnableScheduling
public class SecurityConfig {

private final JwtUtil jwtUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.bssm.attachit.global.security.socket;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.*;

@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {

private final WebSocketHandler webSocketHandler;

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(webSocketHandler, "/attachments").setAllowedOrigins("*");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.bssm.attachit.global.security.socket;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.bssm.attachit.domain.attachment.domain.Attachment;
import org.bssm.attachit.domain.attachment.exception.SocketIOException;
import org.bssm.attachit.domain.attachment.service.GetAttachmentListService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Component
@RequiredArgsConstructor
public class WebSocketHandler extends TextWebSocketHandler {

private final GetAttachmentListService getAttachmentListService;
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final ConcurrentHashMap<String, WebSocketSession> CLIENTS = new ConcurrentHashMap<>();

@Override
public void afterConnectionEstablished(WebSocketSession session) throws RuntimeException {
CLIENTS.put(session.getId(), session);
}

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws RuntimeException {
CLIENTS.remove(session.getId());
}

@Scheduled(fixedRate = 1000, initialDelay = 5000)
public void sendAttachmentListToAllClients() throws JsonProcessingException {
List<Attachment> attachmentList = getAttachmentListService.execute();
String jsonAttachments = objectMapper.writeValueAsString(attachmentList);
TextMessage message = new TextMessage(jsonAttachments);
CLIENTS.forEach((key, value) -> {
try {
value.sendMessage(message);
} catch (IOException e) {
throw SocketIOException.EXCEPTION;
}
});
}
}

0 comments on commit 5cb2ac4

Please sign in to comment.