diff --git a/pom.xml b/pom.xml index 162c136..ae7ce81 100644 --- a/pom.xml +++ b/pom.xml @@ -110,6 +110,11 @@ 6.9.1 + + org.springframework.boot + spring-boot-starter-websocket + + org.lionsoul ip2region diff --git a/src/main/java/com/lion/lionwebsite/Configuration/WebsocketConfiguration.java b/src/main/java/com/lion/lionwebsite/Configuration/WebsocketConfiguration.java new file mode 100644 index 0000000..fc35d5c --- /dev/null +++ b/src/main/java/com/lion/lionwebsite/Configuration/WebsocketConfiguration.java @@ -0,0 +1,22 @@ +package com.lion.lionwebsite.Configuration; + +import com.lion.lionwebsite.Service.WebSocketService; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.config.annotation.EnableWebSocket; +import org.springframework.web.socket.config.annotation.WebSocketConfigurer; +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; + +@Configuration +@EnableWebSocket +public class WebsocketConfiguration implements WebSocketConfigurer { + + WebSocketService webSocketService; + public WebsocketConfiguration(WebSocketService webSocketService) { + this.webSocketService = webSocketService; + } + + @Override + public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { + registry.addHandler(webSocketService, "/ws/").setAllowedOriginPatterns("*"); + } +} diff --git a/src/main/java/com/lion/lionwebsite/Controller/GalleryManageController.java b/src/main/java/com/lion/lionwebsite/Controller/GalleryManageController.java index c799af5..443cdd6 100644 --- a/src/main/java/com/lion/lionwebsite/Controller/GalleryManageController.java +++ b/src/main/java/com/lion/lionwebsite/Controller/GalleryManageController.java @@ -43,7 +43,6 @@ public class GalleryManageController { case "gid" -> galleryManageService.selectTaskByGid(Integer.parseInt(param)); case "all" -> galleryManageService.selectAllGallery(userId); case "name" -> galleryManageService.selectGalleryByName(param); - case "undone" -> galleryManageService.selectUnDoneGallery(); case "downloader" -> galleryManageService.selectGalleryByDownloader(AuthCode); default -> Response._failure("参数错误"); }; diff --git a/src/main/java/com/lion/lionwebsite/Domain/GalleryTask.java b/src/main/java/com/lion/lionwebsite/Domain/GalleryTask.java index 99b4df4..1571a1f 100644 --- a/src/main/java/com/lion/lionwebsite/Domain/GalleryTask.java +++ b/src/main/java/com/lion/lionwebsite/Domain/GalleryTask.java @@ -6,17 +6,11 @@ import lombok.Data; @Data public class GalleryTask { - public static byte DOWNLOADING = 1; - public static byte DOWNLOAD_COMPLETE = 2; - - public static byte DOWNLOAD_QUEUED = 3; - + public static byte COMPRESSING = 3; public static byte COMPRESS_COMPLETE = 4; - public static byte COMPRESSING = 5; - @JsonInclude(JsonInclude.Include.NON_NULL) private String name; diff --git a/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java b/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java index f7e44d0..584685e 100644 --- a/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java +++ b/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java @@ -123,6 +123,7 @@ public class GalleryManageService { if (gallery.getStatus().equals("已提交")) { response.success(gallery.toString()); gallery.setDownloader(user.getId()); + gallery.set_download(true); galleryMapper.insertGallery(gallery); long usedAmount = Long.parseLong(configurationMapper.selectConfiguration(CustomConfiguration.WEEK_USED_AMOUNT).getValue()); @@ -230,23 +231,6 @@ public class GalleryManageService { return response.toJSONString(); } - /** - * 查询未完成的本子 - * - * @return 查询结果 - */ - public String selectUnDoneGallery() { - Response response = Response.generateResponse(); - Gallery[] galleries = galleryMapper.selectUnDoneGalleries(); - - if (galleries.length > 0) - response.success(new ObjectMapper().valueToTree(galleries).toString()); - else - response.failure(); - - return response.toJSONString(); - } - /** * 通过本子名查询本子 * diff --git a/src/main/java/com/lion/lionwebsite/Service/RemoteService.java b/src/main/java/com/lion/lionwebsite/Service/RemoteService.java index 3a8af80..54aecd4 100644 --- a/src/main/java/com/lion/lionwebsite/Service/RemoteService.java +++ b/src/main/java/com/lion/lionwebsite/Service/RemoteService.java @@ -19,6 +19,7 @@ import org.springframework.stereotype.Service; import java.io.IOException; import java.net.*; +import java.util.ArrayList; import java.util.HashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -52,13 +53,16 @@ public class RemoteService { AtomicInteger atomicInteger; - public RemoteService(GalleryMapper galleryMapper, PushService pushService){ + WebSocketService webSocketService; + + public RemoteService(GalleryMapper galleryMapper, PushService pushService, WebSocketService webSocketService){ this.galleryMapper = galleryMapper; this.pushService = pushService; atomicInteger = new AtomicInteger(0); eventLoopGroup = new DefaultEventLoop(); downloadThread = Executors.newCachedThreadPool(); promiseHashMap = new HashMap<>(); + this.webSocketService = webSocketService; if(!initChannel()){ //如果远程服务器连接失败,则开启本地监听 monitor = new Thread(this::monitorFunc); @@ -190,6 +194,7 @@ public class RemoteService { log.info(gallery.getName() + "下载进度:" + gallery.getProceeding() + "/" + gallery.getPages()); galleryMapper.updateGallery(gallery); } + webSocketService.updateTaskProcessing(galleryTasks); } else if(msg instanceof ResponseMessage rsm) promiseHashMap.get(rsm.messageId).setSuccess(rsm); diff --git a/src/main/java/com/lion/lionwebsite/Service/WebSocketService.java b/src/main/java/com/lion/lionwebsite/Service/WebSocketService.java new file mode 100644 index 0000000..9335f85 --- /dev/null +++ b/src/main/java/com/lion/lionwebsite/Service/WebSocketService.java @@ -0,0 +1,73 @@ +package com.lion.lionwebsite.Service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.lion.lionwebsite.Domain.GalleryTask; +import com.lion.lionwebsite.Util.CustomUtil; +import org.jetbrains.annotations.NotNull; +import org.springframework.stereotype.Service; +import org.springframework.web.socket.*; + +import java.util.ArrayList; + +@Service +public class WebSocketService implements WebSocketHandler { + + ArrayList sessions; + + ObjectMapper objectMapper; + + public WebSocketService() { + sessions = new ArrayList<>(); + objectMapper = CustomUtil.objectMapper; + } + + public void updateTaskProcessing(GalleryTask[] galleryTasks){ + if(sessions.isEmpty()) + return; + for (GalleryTask galleryTask : galleryTasks) + if(galleryTask.getStatus() == GalleryTask.COMPRESS_COMPLETE) { + sessions.forEach(s -> { + try { + s.sendMessage(new TextMessage("{\"type\": \"fullUpdate\"}")); + } catch (Exception ignore) { + } + }); + return; + } + + ObjectNode objectNode = objectMapper.createObjectNode(); + objectNode.put("type", "updateTasks"); + objectNode.set("data", objectMapper.valueToTree(galleryTasks)); + System.out.println(objectNode); + sessions.forEach(s -> { + try { + s.sendMessage(new TextMessage(objectNode.toString())); + }catch (Exception ignore){} + }); + } + + @Override + public void afterConnectionEstablished(@NotNull WebSocketSession session) throws Exception {} + + @Override + public void handleMessage(@NotNull WebSocketSession session, @NotNull WebSocketMessage message) throws Exception { + if(message.getPayload().toString().equals("DownloaderWebsocket")) + sessions.add(session); + else + session.close(); + } + + @Override + public void handleTransportError(@NotNull WebSocketSession session, @NotNull Throwable exception) throws Exception {} + + @Override + public void afterConnectionClosed(@NotNull WebSocketSession session, @NotNull CloseStatus closeStatus) throws Exception { + sessions.remove(session); + } + + @Override + public boolean supportsPartialMessages() { + return false; + } +} diff --git a/src/main/resources/LionWebsite.db b/src/main/resources/LionWebsite.db index b7efde1..24bc397 100644 Binary files a/src/main/resources/LionWebsite.db and b/src/main/resources/LionWebsite.db differ