netty-确定客户端和微信端的沟通方式

This commit is contained in:
2023-08-22 21:35:57 +08:00
parent 86fd226c84
commit 6c0a07331b
24 changed files with 109 additions and 490 deletions

View File

@@ -1,13 +1,18 @@
package com.sv.netty;
import com.sv.netty.message.HeartBeat;
import com.sv.netty.config.HeartBeat;
import com.sv.netty.config.VenueMessage;
import com.sv.netty.utils.EncodeMsg;
import com.sv.netty.utils.JsonUtils;
import com.sv.service.MessageService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.UnknownHostException;
@@ -19,6 +24,7 @@ import java.net.UnknownHostException;
*/
@ChannelHandler.Sharable
public class ClientHandler extends SimpleChannelInboundHandler<String> {
private final Logger logger = LoggerFactory.getLogger(ClientHandler.class);
/**
* 当通道就绪就会触发
@@ -48,9 +54,9 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
*/
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("接收服务器响应msg:[" + msg + "]");
// MessageDTO message = JsonMapper.fromJson(msg, MessageDTO.class);
// MessageService.getInstance().execute(message);
logger.info("接收服务器响应msg:[" + msg + "]");
VenueMessage message = JsonUtils.decode(msg, VenueMessage.class);
MessageService.getInstance().execute(message);
}
/**
@@ -88,7 +94,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
System.out.println("ClientHandler exceptionCaught");
logger.info("ClientHandler exceptionCaught");
cause.printStackTrace();
Channel channel = ctx.channel();
if(channel.isActive()) {

View File

@@ -5,6 +5,8 @@ import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LoggingHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
@@ -17,6 +19,7 @@ import java.util.concurrent.TimeUnit;
* @date 07/12/2017 10:12 PM
*/
public class ClientThread extends Thread{
private final Logger logger = LoggerFactory.getLogger(ClientThread.class);
private static ClientThread instance;
@@ -69,11 +72,11 @@ public class ClientThread extends Thread{
}
public void doConnect() {
System.out.println("现在开始链接了");
logger.info("现在开始链接了");
if (closed) {
return;
}
System.out.println("连接 = " + remoteHost + " " + remotePort);
logger.info("连接 = " + remoteHost + " " + remotePort);
future = bootstrap.connect(new InetSocketAddress(remoteHost, remotePort));
future.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture f) throws Exception {
@@ -82,9 +85,9 @@ public class ClientThread extends Thread{
public void run() {
if (!f.isSuccess()) {
doConnect();
System.out.println("等待连接");
logger.info("等待连接");
} else {
System.out.println("已连接");
logger.info("已连接");
}
}
}, 2, TimeUnit.SECONDS);
@@ -110,7 +113,7 @@ public class ClientThread extends Thread{
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
System.out.println(ctx.toString() + "======inactive");
logger.info(ctx.toString() + "======inactive");
ctx.channel().eventLoop().schedule(new Runnable() {
@Override
public void run() {

View File

@@ -1,33 +0,0 @@
package com.sv.netty.message;
import java.io.Serializable;
/**
* 客户端心跳数据包
* HeartBeat.java
*
* @author peakren
* @date 07/12/2017 10:23 PM
*/
public class HeartBeat implements Serializable {
private Integer venueId; //场馆号
private String deviceName; //设备号
public Integer getVenueId() {
return venueId;
}
public void setVenueId(Integer venueId) {
this.venueId = venueId;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
}

View File

@@ -1,20 +0,0 @@
package com.sv.netty.message;
public enum MessageType {
HB("连接"),
OPEN_DOOR("开门");
private String message;
MessageType(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@@ -1,25 +0,0 @@
package com.sv.netty.message;
import java.io.Serializable;
public class VenueMessage implements Serializable {
private MessageType messageType;
private String message;
public MessageType getMessageType() {
return messageType;
}
public void setMessageType(MessageType messageType) {
this.messageType = messageType;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@@ -1,6 +1,8 @@
package com.sv.service;
import com.sv.netty.message.VenueMessage;
import com.sv.netty.config.VenueMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 消息服务
@@ -11,6 +13,8 @@ import com.sv.netty.message.VenueMessage;
*/
public class MessageService {
private final Logger logger = LoggerFactory.getLogger(MessageService.class);
static private MessageService sInstance;
static public MessageService getInstance() {
@@ -34,7 +38,7 @@ public class MessageService {
openDoor();
break;
default:
System.out.println( "default");
logger.info( "default");
}
}
@@ -43,7 +47,7 @@ public class MessageService {
*/
public void openDoor() {
// 开门
System.out.println("开门成功!!!");
logger.info("开门成功!!!");
}
}

View File

@@ -1,15 +0,0 @@
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/smart_venue?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8&useAffectedRows=true&useSSL=false
username: root
password: 123456
jpa:
show-sql: true
sv:
file:
store:
image: imagetest/
video: videotest/
health: health-docstest/

View File

@@ -1,15 +0,0 @@
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/smart_venue?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8&useAffectedRows=true
username: root
password: hyty1234
jpa:
show-sql: true
sv:
file:
store:
image: image/
video: video/
health: health-docs/

View File

@@ -1,111 +0,0 @@
server:
port: 8023
context-path: /netty
tomcat:
uri-encoding: utf-8
spring:
profiles:
include:
-dev
-prod
active: dev
# 数据库连接池配置
druid:
filters: stat
initialSize: 1
minIdle: 1
maxActive: 40
maxWait: 600000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
WebStatFilter:
enabled: false
urlPattern:
exclusions:
sessionStatMaxCount:
sessionStatEnable:
principalSessionName:
principalCookieName:
profileEnable:
StatViewServlet:
enabled: true
urlPattern: /druid/*
resetEnable: true
loginUsername: druid
loginPassword: druid
allow:
deny:
aop:
auto: true
http:
encoding:
force: true
charset: utf-8
enabled: true
jackson:
date-format: yyyy-MM-dd HH:mm:ss
property-naming-strategy: CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES
redis:
pool:
max-active: 1000
max-wait: -1
max-idle: 8
min-idle: 8
timeout: 60000
# MyBatis Configuration
mybatis:
type-aliases-package: com.ydd.oms.entity
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*/*.xml
oss:
accessKeyId: LTAIlbtS4W2Xe4OV
accessKeySecret: qWMYkSfmXFtRoIv9q9OCbszcF9U7dX
protocol: http
name: smartvenue
endPoint: http://oss-cn-beijing.aliyuncs.com
url: https://smartvenue.oss-cn-beijing.aliyuncs.com/
face:
url: 192.168.1.111
account: test@test.com
pwd: 123456
nettym:
url: http://127.0.0.1:8021/netty/message/send
#netty服务器配置
netty:
port: 56791
boss:
thread:
count: 1
worker:
thread:
count: 2
so:
keepalive: true
backlog: 128
reuseaddr: true
tcp_nodelay: true
logging:
level:
com:
sv:
mapper: DEBUG

View File

@@ -1,41 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true" />
<!-- 打印sql日志 -->
<setting name="logImpl" value="SLF4J" />
</settings>
<!--<settings>-->
<!--<setting name="cacheEnabled" value="true"/>-->
<!--<setting name="lazyLoadingEnabled" value="true"/>-->
<!--<setting name="multipleResultSetsEnabled" value="true"/>-->
<!--<setting name="useColumnLabel" value="true"/>-->
<!--<setting name="useGeneratedKeys" value="false"/>-->
<!--<setting name="autoMappingBehavior" value="PARTIAL"/>-->
<!--<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>-->
<!--<setting name="defaultExecutorType" value="SIMPLE"/>-->
<!--<setting name="defaultStatementTimeout" value="25"/>-->
<!--<setting name="defaultFetchSize" value="100"/>-->
<!--<setting name="safeRowBoundsEnabled" value="false"/>-->
<!--<setting name="mapUnderscoreToCamelCase" value="false"/>-->
<!--<setting name="localCacheScope" value="SESSION"/>-->
<!--<setting name="jdbcTypeForNull" value="OTHER"/>-->
<!--<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>-->
<!--</settings>-->
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Byte" type="java.lang.Byte"/>
<typeAlias alias="String" type="java.lang.String" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>