diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..82904c4 --- /dev/null +++ b/.env.development @@ -0,0 +1,2 @@ +NODE_ENV="preview" +VUE_APP_BASE_URL="http://localhost" \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..28cc3da --- /dev/null +++ b/.env.production @@ -0,0 +1,2 @@ +NODE_ENV="production" +VUE_APP_BASE_URL="" \ No newline at end of file diff --git a/java/application.properties b/java/application.properties index dd27652..41a6bdf 100644 --- a/java/application.properties +++ b/java/application.properties @@ -1,7 +1,3 @@ -# WEB服务端口配置 -server.port=80 -# WEB服务根路径配置 -server.servlet.context-path=/ # 程序自身数据源配置 spring.datasource.url=jdbc:mysql://10.100.0.108:3306/idle_game?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true spring.datasource.username=root diff --git a/java/pom.xml b/java/pom.xml index 24cab05..aead04f 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -95,6 +95,12 @@ pagehelper-spring-boot-starter ${mybatis.pagehelper.version} + + + + org.springframework.boot + spring-boot-starter-websocket + diff --git a/java/src/main/java/vip/xumy/idle/server/SimpleApplocation.java b/java/src/main/java/vip/xumy/idle/server/IdleServerApplocation.java similarity index 51% rename from java/src/main/java/vip/xumy/idle/server/SimpleApplocation.java rename to java/src/main/java/vip/xumy/idle/server/IdleServerApplocation.java index 5b0f331..b27b423 100644 --- a/java/src/main/java/vip/xumy/idle/server/SimpleApplocation.java +++ b/java/src/main/java/vip/xumy/idle/server/IdleServerApplocation.java @@ -2,12 +2,12 @@ package vip.xumy.idle.server; import org.springframework.boot.SpringApplication; -import vip.xumy.idle.server.conf.SimpleInitializer; +import vip.xumy.idle.server.conf.IdleServerInitializer; -public class SimpleApplocation { +public class IdleServerApplocation { public static void main(String[] args) { - Class[] arr = new Class[] { SimpleInitializer.class }; + Class[] arr = new Class[] { IdleServerInitializer.class }; SpringApplication.run(arr, args); } diff --git a/java/src/main/java/vip/xumy/idle/server/conf/SimpleInitializer.java b/java/src/main/java/vip/xumy/idle/server/conf/IdleServerInitializer.java similarity index 63% rename from java/src/main/java/vip/xumy/idle/server/conf/SimpleInitializer.java rename to java/src/main/java/vip/xumy/idle/server/conf/IdleServerInitializer.java index 74f5d87..a205198 100644 --- a/java/src/main/java/vip/xumy/idle/server/conf/SimpleInitializer.java +++ b/java/src/main/java/vip/xumy/idle/server/conf/IdleServerInitializer.java @@ -4,15 +4,22 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.socket.server.standard.ServerEndpointExporter; @EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class) @ComponentScan(value = "vip.xumy.idle.server, vip.xumy.core") -public class SimpleInitializer extends SpringBootServletInitializer { +public class IdleServerInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { - return builder.sources(SimpleInitializer.class); + return builder.sources(IdleServerInitializer.class); + } + + @Bean + public ServerEndpointExporter serverEndpointExporter() { + return new ServerEndpointExporter(); } } diff --git a/java/src/main/java/vip/xumy/idle/server/ctrl/PublicController.java b/java/src/main/java/vip/xumy/idle/server/ctrl/PublicController.java new file mode 100644 index 0000000..feb7f8e --- /dev/null +++ b/java/src/main/java/vip/xumy/idle/server/ctrl/PublicController.java @@ -0,0 +1,35 @@ +package vip.xumy.idle.server.ctrl; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import vip.xumy.core.pojo.com.BaseResponse; +import vip.xumy.idle.server.pojo.SocketMsg; +import vip.xumy.idle.server.service.WebSocketService; + +/** + * Ownership belongs to the company + * + * @author:mengyxu + * @date:2025年4月21日 + */ + +@RestController +@RequestMapping("public") +public class PublicController { + + @GetMapping("socket/num") + public BaseResponse getSocketNum() { + return new BaseResponse(WebSocketService.CLIENT_MAP.keySet().size()); + } + + @PostMapping("update/notify") + public BaseResponse updateNotify(@RequestBody String notify) { + WebSocketService.sendMessage(new SocketMsg("update", notify)); + return new BaseResponse(); + } + +} diff --git a/java/src/main/java/vip/xumy/idle/server/pojo/SocketMsg.java b/java/src/main/java/vip/xumy/idle/server/pojo/SocketMsg.java new file mode 100644 index 0000000..601ad76 --- /dev/null +++ b/java/src/main/java/vip/xumy/idle/server/pojo/SocketMsg.java @@ -0,0 +1,25 @@ +package vip.xumy.idle.server.pojo; + +import lombok.Getter; + +/** Ownership belongs to the company + * + * @author:mengyxu + * @date:2025年4月9日 + */ + +@Getter +public class SocketMsg { + + private String type; + private T data; + + public SocketMsg(String type){ + this.type = type; + } + public SocketMsg(String type, T data){ + this.type = type; + this.data = data; + } + +} diff --git a/java/src/main/java/vip/xumy/idle/server/service/WebSocketService.java b/java/src/main/java/vip/xumy/idle/server/service/WebSocketService.java new file mode 100644 index 0000000..bc4459b --- /dev/null +++ b/java/src/main/java/vip/xumy/idle/server/service/WebSocketService.java @@ -0,0 +1,96 @@ +package vip.xumy.idle.server.service; + +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.websocket.OnClose; +import javax.websocket.OnError; +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.Session; +import javax.websocket.server.PathParam; +import javax.websocket.server.ServerEndpoint; + +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.stereotype.Component; + +import com.alibaba.fastjson.JSON; + +import lombok.Getter; +import lombok.extern.log4j.Log4j2; +import vip.xumy.core.exception.CoreException; +import vip.xumy.idle.server.pojo.SocketMsg; + +/** + * Ownershid belongs to the company + * + * @author:mengyxu + * @date:2024年1月26日 + */ + +@Log4j2 +@Component +@EnableScheduling +@ServerEndpoint(value = "/websocket/{id}") +public class WebSocketService { + public static final Map CLIENT_MAP = new HashMap<>(); + private Session session; + @Getter + private String id; + + @OnOpen + public void onOpen(Session session, @PathParam("id") String id) { + this.session = session; + this.id = id; + CLIENT_MAP.put(id, this); + log.debug(id + "连接websocket"); + } + + @OnClose + public void onClose() { + CLIENT_MAP.remove(id); + } + + @OnMessage + public void onMessage(String message, Session session) { + } + + @OnError + public void onError(Session session, Throwable error) { + log.error(id + "连接错误错误", error); + } + + public static Collection clients() { + return CLIENT_MAP.values(); + } + + public void send(String message) { + try { + session.getBasicRemote().sendText(message); + } catch (IOException e) { + throw new CoreException("下发消息失败", e); + } + } + + public static void sendMessage(List ids, SocketMsg message) { + String json = JSON.toJSONString(message); + for (String id : ids) { + WebSocketService client = CLIENT_MAP.get(id); + if (client == null) { + continue; + } + client.send(json); + } + } + + public static void sendMessage(SocketMsg message) { + String json = JSON.toJSONString(message); + for (WebSocketService client : CLIENT_MAP.values()) { + client.send(json); + } + } + +} diff --git a/java/src/main/resources/logback.xml b/java/src/main/resources/logback.xml index ea24e69..a8ae6aa 100644 --- a/java/src/main/resources/logback.xml +++ b/java/src/main/resources/logback.xml @@ -57,12 +57,12 @@ - + - + diff --git a/src/App.vue b/src/App.vue index c5058c0..b761fb6 100644 --- a/src/App.vue +++ b/src/App.vue @@ -9,7 +9,7 @@ -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; - color: #2c3e50; + color: #FFF; width: 100%; height: 100%; overflow: hidden; diff --git a/src/components/dialog.vue b/src/components/dialog.vue index 2ec1861..b59bcad 100644 --- a/src/components/dialog.vue +++ b/src/components/dialog.vue @@ -1,6 +1,6 @@ + \ No newline at end of file