From e169a3539e6b5bf9356cc7ce6b5c4f9399f2fd0e Mon Sep 17 00:00:00 2001 From: mrliudeveloper Date: Wed, 29 Dec 2021 09:34:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90service-simulation=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E7=9A=84=E6=95=B0=E6=8D=AE=E5=88=86=E5=8F=91=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hollykunge/simu/config/Constant.java | 5 ++++ .../simu/simulation/SimulationService.java | 24 +++++++++++++++---- .../github/hollykunge/simu/vo/SimuMsg.java | 16 +++++++++++++ src/main/resources/application.yml | 4 ++-- 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/github/hollykunge/simu/vo/SimuMsg.java diff --git a/src/main/java/com/github/hollykunge/simu/config/Constant.java b/src/main/java/com/github/hollykunge/simu/config/Constant.java index fd6c655..e9d543b 100644 --- a/src/main/java/com/github/hollykunge/simu/config/Constant.java +++ b/src/main/java/com/github/hollykunge/simu/config/Constant.java @@ -21,6 +21,11 @@ public class Constant { * 用户信息主键 */ public static final String USER_KEY = "userId"; + + /** + * 用户信息PId主键 + */ + public static final String USER_PID = "userPId"; /** * 打印处理 */ diff --git a/src/main/java/com/github/hollykunge/simu/simulation/SimulationService.java b/src/main/java/com/github/hollykunge/simu/simulation/SimulationService.java index 7bdbcf8..d565754 100644 --- a/src/main/java/com/github/hollykunge/simu/simulation/SimulationService.java +++ b/src/main/java/com/github/hollykunge/simu/simulation/SimulationService.java @@ -1,6 +1,9 @@ package com.github.hollykunge.simu.simulation; import com.alibaba.fastjson.JSONObject; +import com.github.hollykunge.simu.vo.SimuMsg; +import io.netty.channel.Channel; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.stream.annotation.EnableBinding; import org.springframework.cloud.stream.annotation.StreamListener; @@ -8,6 +11,7 @@ import org.springframework.messaging.Message; import org.springframework.stereotype.Component; import static com.github.hollykunge.simu.config.Constant.PRINT_DESCRIBE; +import static com.github.hollykunge.simu.websocket.NettyConfig.getUserChannel; /** * @author Mr.Liu @@ -19,13 +23,23 @@ public class SimulationService { @StreamListener(SimulationSink.SIMULATION_INPUT) public void input(Message message) { - log.info(SimulationSink.SIMULATION_INPUT + ":" + message.getPayload()); + if (message == null) { + return; + } final String payload = message.getPayload(); + log.debug(SimulationSink.SIMULATION_INPUT + ":" + payload); + try { - final JSONObject parse = (JSONObject) JSONObject.parse(payload); - log.warn(parse.getString("code")); - log.warn(parse.getString("address")); - log.warn(parse.getString("data")); + final SimuMsg simuMsg = JSONObject.parseObject(payload, SimuMsg.class); + final String[] userList = simuMsg.getAddress().split(","); + for (String user : userList) { + Channel userChannel = getUserChannel(user); + if (null == userChannel) { + log.warn(user + " User offline..."); + continue; + } + userChannel.writeAndFlush(new TextWebSocketFrame(simuMsg.getData())); + } } catch (Exception e) { log.error(PRINT_DESCRIBE + "\n" + payload); } diff --git a/src/main/java/com/github/hollykunge/simu/vo/SimuMsg.java b/src/main/java/com/github/hollykunge/simu/vo/SimuMsg.java new file mode 100644 index 0000000..adc2194 --- /dev/null +++ b/src/main/java/com/github/hollykunge/simu/vo/SimuMsg.java @@ -0,0 +1,16 @@ +package com.github.hollykunge.simu.vo; + +import lombok.Data; + +/** + * @author Mr.Liu + */ +@Data +public class SimuMsg { + + private String code; + + private String address; + + private String data; +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index eb5784b..ebedbc5 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,7 +4,7 @@ spring: rabbitmq: virtual-host: / # virtual-host: /simuMsg - host: 10.11.24.134 + host: 10.11.24.136 # host: 10.11.24.136 username: hollykunge password: 123456 @@ -15,7 +15,7 @@ spring: rabbitmq: virtual-host: / # virtual-host: /simuMsg - host: 10.11.24.134 + host: 10.11.24.136 # host: 10.11.24.136 port: 5672 username: hollykunge -- Gitee