From 1516226192ba01e97edcebea593cbd0114227b66 Mon Sep 17 00:00:00 2001 From: chuzhongzai Date: Thu, 28 Dec 2023 16:07:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E8=8E=B7=E5=8F=96=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E8=BF=9B=E5=BA=A6=E6=96=B9=E5=BC=8F=E7=94=B1=E8=BD=AE?= =?UTF-8?q?=E8=AF=A2=E6=94=B9=E4=B8=BAWebsocket;=E5=8E=BB=E9=99=A4?= =?UTF-8?q?=E6=97=A0=E7=94=A8=E4=BB=BB=E5=8A=A1=E7=8A=B6=E6=80=81;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 ++ .../Configuration/WebsocketConfiguration.java | 22 ++++++ .../Controller/GalleryManageController.java | 1 - .../lion/lionwebsite/Domain/GalleryTask.java | 8 +- .../Service/GalleryManageService.java | 18 +---- .../lionwebsite/Service/RemoteService.java | 7 +- .../lionwebsite/Service/WebSocketService.java | 73 ++++++++++++++++++ src/main/resources/LionWebsite.db | Bin 442368 -> 442368 bytes 8 files changed, 108 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/lion/lionwebsite/Configuration/WebsocketConfiguration.java create mode 100644 src/main/java/com/lion/lionwebsite/Service/WebSocketService.java 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 b7efde1d37a82b032391ca6ac027d0be84a64bfb..24bc397205568d71cfbd253e9f0224dbb5d58c1e 100644 GIT binary patch delta 2297 zcmcgtU2Ggj9pBko`#yYM2}`uuFmELkC|DnWCz6A%&eN-#z&L>S?V@LtUCy zzV@YK;p?0bo9AMGkKK;FSB)N;i*=kh9IJByP@GI{Z}7=Hn@1+|HkqI!1)9zkY;LA{ zyYGP52x6UFth3r)+7UCnL2v#hj&sLIE7~TK&#_Zu;)#uJ0h{oBT15t+;O8L(9j^v3%x>bwLR_^d_x{zp1$?9_>cBY z&3-vUpUGuN)*kY}+UA?bk9KTqe%<5B-}vaOkxNgR{TrLrr+0bu+itgf<0GYIlUHx4>*=*CAk! z2+bh!!NDz@x1Y4|y9I%7Z3W&J1IYn5pU-$Fytxm;ajr;O*PcDZo-ytRCo>K$NKcH> z{1`3R!~JBUKyz8KJD17O^n}Y&CIbUv16O_<@`=-HsjShoqu9Dm9r1Ie$X z$6QzJW?@*&jgymZk(?xAHYd7;Nq0P#b+Z#BD?TUYiet5Z{E6QNYWI!+2zcLIZP%l0 zVl054R62UVYqjM?z8TbRKL+;tFM=CYu{W2?4vRfgC&hk}DT;gQ-i4KgOZBU#>*Wi3#XWu5(VUwd-76;9 z&#&S-EGAce{u%>z>H1}sGf!fqRIWYN*fPDEsj@){R4im;EU&-9UEWMZXz9Zb>8a-X z+~o%*g{GE3>ue}kMpRZXqGnpr)0(5FF;)%sK?^HdiWmm-y8@Ao(ps8I$W&FsluQFl za5r=CW-h#>H~;Hr;@lIf9rA-h5o3;6W0;OJiYx(q{ufu1DAF?WJ6*?#=uJN5Gy zmlhU2e&yTscV?C=r|a{t)G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