1.检测模块:预检测框架提交

2.报文通用装置查询编写
This commit is contained in:
wr
2024-12-12 18:40:58 +08:00
parent e432501e99
commit bc0e93b522
20 changed files with 277 additions and 98 deletions

View File

@@ -36,6 +36,11 @@
<version>4.1.68.Final</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,26 @@
package com.njcn.gather.detection.handler;
import com.njcn.gather.detection.util.socket.SocketManager;
import com.njcn.gather.detection.util.socket.web.WebSocketHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class SocketSourceResponseService {
private final WebSocketHandler webSocketHandler;
public void deal(String userId,String msg){
System.out.println("进入deal+++++++++++++++++++");
webSocketHandler.senMsgToUser(userId,msg);
SocketManager.getChannelByUserId(userId).close();
}
}

View File

@@ -4,7 +4,7 @@ import lombok.Data;
/**
* @author wr
* @description
* @description socket 通用发送报文请求
* @date 2024/12/11 15:57
*/
@Data

View File

@@ -8,7 +8,11 @@ package com.njcn.gather.detection.service;
public interface PreDetectionService {
/**
* 源通讯校验
* 源通讯校验socket入参拼接
* @param
* @return: java.lang.String
* @Author: wr
* @Date: 2024/12/11 13:26
*/
void sourceCommunicationCheck();

View File

@@ -1,7 +1,7 @@
package com.njcn.gather.detection.service.impl;
import com.njcn.gather.detection.handler.SocketResponseService;
import com.njcn.gather.detection.handler.SocketSourceResponseService;
import com.njcn.gather.detection.service.PreDetectionService;
import com.njcn.gather.detection.util.socket.cilent.NettyClient;
@@ -21,7 +21,7 @@ public class PreDetectionServiceImpl implements PreDetectionService {
@Value("${socket.source.port}")
private static Integer port;
private final SocketResponseService socketResponseService;
private final SocketSourceResponseService sourceResponseService;
@Override
@@ -48,7 +48,7 @@ public class PreDetectionServiceImpl implements PreDetectionService {
public void test() {
NettyClient.socketClient(ip,port,"test------------>",new NettySourceClientHandler("aa",socketResponseService));
NettyClient.socketClient(ip,port,"test------------>",new NettySourceClientHandler("aaa",sourceResponseService));
}
}

View File

@@ -1,5 +1,8 @@
package com.njcn.gather.detection.util.socket;
import com.alibaba.fastjson.JSON;
import com.njcn.gather.detection.pojo.vo.SocketMsg;
/**
* @author wr
* @description
@@ -9,4 +12,7 @@ package com.njcn.gather.detection.util.socket;
public class MsgUtil {
public SocketMsg a(String textMsg){
return JSON.parseObject(textMsg,SocketMsg.class);
}
}

View File

@@ -42,8 +42,8 @@ public class WebServiceManager {
public static void sendMsg(String userId,String msg) {
Channel channel = userSessions.get(userId);
TextWebSocketFrame wd1 = new TextWebSocketFrame(msg);
channel.writeAndFlush(wd1);
TextWebSocketFrame wd = new TextWebSocketFrame(msg);
channel.writeAndFlush(wd);
}
}

View File

@@ -8,8 +8,6 @@ import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Value;
import java.util.concurrent.TimeUnit;
@@ -45,7 +43,6 @@ public class NettyClient {
e.printStackTrace();
} finally {
group.shutdownGracefully();
System.out.println("程序结束");
}
}

View File

@@ -1,18 +1,15 @@
package com.njcn.gather.detection.util.socket.cilent;
import com.njcn.gather.detection.handler.SocketResponseService;
import com.njcn.gather.detection.handler.SocketSourceResponseService;
import com.njcn.gather.detection.util.socket.SocketManager;
import com.njcn.gather.detection.util.socket.WebServiceManager;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.Scanner;
/**
* @Description: 客户端业务处理(示例)
* @Description: 客户端业务处理(示例)
* @Author: wr
* @Date: 2024/12/10 14:16
*/
@@ -22,14 +19,7 @@ public class NettySourceClientHandler extends SimpleChannelInboundHandler<Strin
private final String webUser;
private final SocketResponseService socketResponseService;
private final SocketSourceResponseService sourceResponseService;
/**
@@ -46,17 +36,7 @@ public class NettySourceClientHandler extends SimpleChannelInboundHandler<Strin
*/
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws InterruptedException {
Channel userId = SocketManager.getChannelByUserId("userId");
if(msg.endsWith("结束")){
WebServiceManager.sendMsg("","第一流程结束");
System.out.println("第一流程结束");
userId.close();
}else{
System.out.println("服务端消息 == " + msg);
socketResponseService.deal(webUser,msg);
}
sourceResponseService.deal(webUser,msg);
}
/**

View File

@@ -21,7 +21,7 @@ import io.netty.channel.socket.nio.NioServerSocketChannel;
*/
public class NettyServer {
public static final int port = 8787;
public static final int port = 61000;
public static void main(String[] args) {
NettyServer nettyServer = new NettyServer();

View File

@@ -1,9 +1,12 @@
package com.njcn.gather.detection.util.socket.web;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.njcn.gather.detection.pojo.vo.SocketMsg;
import com.njcn.gather.detection.pojo.vo.aa;
import com.njcn.gather.detection.util.socket.WebServiceManager;
import com.njcn.gather.detection.util.socket.cilent.NettyClient;
import com.njcn.gather.detection.util.socket.cilent.NettySourceClientHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
@@ -14,8 +17,8 @@ import org.springframework.stereotype.Component;
/**
* @Description: 泛型 代表的是处理数据的单位
* TextWebSocketFrame : 文本信息帧
* @Description: 泛型 代表的是处理数据的单位
* TextWebSocketFrame : 文本信息帧
* @Author: wr
* @Date: 2024/12/10 13:56
*/
@@ -25,6 +28,8 @@ import org.springframework.stereotype.Component;
@Slf4j
public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
private final static String QUESTION_MARK = "?";
private final static String EQUAL_TO = "=";
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
@@ -41,10 +46,9 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketF
String userId = getUrlParams(uri);
WebServiceManager.addUser(userId, ctx.channel());
log.info("登录的用户id是{}", userId);
//如果url包含参数需要处理
if (uri.contains("?")) {
String newUri = uri.substring(0, uri.indexOf("?"));
if (uri.contains(QUESTION_MARK)) {
String newUri = uri.substring(0, uri.indexOf(QUESTION_MARK));
request.setUri(newUri);
}
@@ -52,36 +56,35 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketF
//正常的TEXT消息类型
TextWebSocketFrame frame = (TextWebSocketFrame) msg;
log.info("客户端收到服务器数据:{}", frame.text());
}
super.channelRead(ctx, msg);
}
/**
* 根据用户地址获取用户名 ws://127.0.0.1:7777/hello?name=aa
* @param url
* @return
*/
private static String getUrlParams(String url) {
if (!url.contains("=")) {
if (!url.contains(EQUAL_TO)) {
return null;
}
String userId = url.substring(url.indexOf("=") + 1);
String userId = url.substring(url.indexOf(EQUAL_TO) + 1);
return userId;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) {
/* System.out.println("服务端消息 == " + msg.text());
if(msg.text().equals("下发指令")){
*//**
* 处理对应消息
* 1.先下发所要操作的流程信息
* 2.组装对应的入参信息
* 3.再用socket信息返回结束
*//*
* 处理对应消息
* 1.先下发所要操作的流程信息
* 2.组装对应的入参信息
* 3.再用socket信息返回结束
*//*
//NettyClient.socketClient(msg.text(),new NettySourceClientHandler());
}*/
@@ -95,7 +98,7 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketF
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
//WebServiceManager.addUser(userId, ctx.channel());
System.out.println("有新的连接接入:"+ctx);
System.out.println("有新的连接接入:" + ctx);
}
@Override
@@ -125,8 +128,8 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketF
/**
* 推送数据至前台
*/
public void senMsgToUser(String userId,String msg){
WebServiceManager.sendMsg(userId,msg);
public void senMsgToUser(String userId, String msg) {
WebServiceManager.sendMsg(userId, msg);
}
}

View File

@@ -30,7 +30,7 @@ public class WebSocketService {
/**
* 端口号
*/
@Value("${webSocket.netty.port:7777}")
@Value("${webSocket.port}")
int port;
@@ -40,7 +40,7 @@ public class WebSocketService {
@PostConstruct
public void start() throws InterruptedException {
public void start() {
new Thread(() -> {
//可以自定义线程的数量
bossGroup = new NioEventLoopGroup();
@@ -94,41 +94,4 @@ public class WebSocketService {
System.out.println("webSocket销毁---------------");
}
/* public static void main(String[] args) {
//可以自定义线程的数量
EventLoopGroup bossGroup = new NioEventLoopGroup();
// 默认创建的线程数量 = CPU 处理器数量 *2
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler())
//当前连接被阻塞的时候BACKLOG代表的事 阻塞队列的长度
.option(ChannelOption.SO_BACKLOG, 128)
//设置连接保持为活动状态
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new WebSocketInitializer());
ChannelFuture future = serverBootstrap.bind(7777).sync();
future.addListener(f -> {
if (future.isSuccess()) {
System.out.println("webSocket服务启动成功");
} else {
System.out.println("webSocket服务启动失败");
}
});
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}*/
}