-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : get attachment list by socket
- Loading branch information
1 parent
58888ab
commit 5cb2ac4
Showing
6 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/org/bssm/attachit/domain/attachment/exception/SocketIOException.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,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); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/bssm/attachit/domain/attachment/service/GetAttachmentListService.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,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(); | ||
} | ||
} |
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
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
18 changes: 18 additions & 0 deletions
18
src/main/java/org/bssm/attachit/global/security/socket/WebSocketConfig.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,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("*"); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/bssm/attachit/global/security/socket/WebSocketHandler.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,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; | ||
} | ||
}); | ||
} | ||
} |