From 9b614251ca9e99b026ea4e91656c733a6077f194 Mon Sep 17 00:00:00 2001 From: limqhz <540344226@qq.com> Date: Tue, 22 Aug 2023 22:26:56 +0800 Subject: [PATCH] =?UTF-8?q?netty-=E7=A1=AE=E5=AE=9A=E4=BA=92=E8=81=94?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/AppMessageHandlerAdapter.java | 28 +++++++---------- .../main/java/com/sv/netty/ClientHandler.java | 2 +- .../java/com/sv/netty/ClientInitializer.java | 2 +- netty-model/pom.xml | 4 +++ .../com/sv/netty/config/NettyConstant.java | 4 +-- .../com/sv/netty/config/VenueMessage.java | 9 ++++-- .../sv/netty/utils/ServerMessageUtils.java | 4 ++- .../mybatis/mapper/sv/DeviceMapper.xml | 31 +------------------ 8 files changed, 30 insertions(+), 54 deletions(-) diff --git a/api/src/main/java/com/sv/netty/netty/service/impl/AppMessageHandlerAdapter.java b/api/src/main/java/com/sv/netty/netty/service/impl/AppMessageHandlerAdapter.java index 24f15a3..52e195b 100644 --- a/api/src/main/java/com/sv/netty/netty/service/impl/AppMessageHandlerAdapter.java +++ b/api/src/main/java/com/sv/netty/netty/service/impl/AppMessageHandlerAdapter.java @@ -18,7 +18,6 @@ import io.netty.util.internal.PlatformDependent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.text.MessageFormat; @@ -46,27 +45,24 @@ public class AppMessageHandlerAdapter implements MessageService { */ private ConcurrentMap links = PlatformDependent.newConcurrentHashMap(); -// @Resource + @Resource private MemberService memberService; @Resource private VenueService venueService; -// @Resource + @Resource private DeviceService deviceService; -// @Resource + @Resource private ConfigService configService; -// @Resource(name = "scheduledExecutorService") + @Resource(name = "scheduledExecutorService") private ScheduledExecutorService scheduledExecutorService; -// @Resource + @Resource private MemberEnterVenueLogService memberEnterVenueLogService; -// @Resource - private RestTemplate restTemplate; - /** * 处理心跳信息,存储心跳信息 * @param clientId @@ -84,9 +80,9 @@ public class AppMessageHandlerAdapter implements MessageService { logger.error("this client choose venue Error! venueId == " + heartBeat.getVenueId()); } else { deviceService.online(heartBeat.getDeviceName(),heartBeat.getVenueId(),thisVenue.getType(),clientId); - putChannelType(heartBeat.getDeviceName(),heartBeat.getVenueId(),channel); - VenueMessage VenueMessage = new VenueMessage(MessageType.LINK,"欢迎扫码进场!"); - channel.writeAndFlush(VenueMessage); + addLinks(heartBeat.getDeviceName(),heartBeat.getVenueId(),channel); + VenueMessage venueMessage = new VenueMessage(MessageType.LINK,"欢迎扫码进场!"); + ServerMessageUtils.INSTANCE.sendMsg(channel,venueMessage); } } } @@ -129,7 +125,7 @@ public class AppMessageHandlerAdapter implements MessageService { @Override public void testLoad(String deviceName, Integer venueId) { Channel currentChannel = getCurrentChannel(deviceName, venueId); - currentChannel.writeAndFlush("Test Links" + deviceName + venueId); + ServerMessageUtils.INSTANCE.sendMsg(currentChannel,new VenueMessage(MessageType.LINK,"测试链接")); } @@ -160,7 +156,7 @@ public class AppMessageHandlerAdapter implements MessageService { // VenueMessage.setCmdId(Cmd.OPEN_DOOR.id); // VenueMessage.setDoor(2); // sendOpenMessage(VenueMessage, device.getId()); - // 校验玩就可以出场了 + // 校验完就可以出场了 // sendMessage(memberMessageDto, device.getId()); // venueService.addNumber(venue.getId(), -1, member.getId()); // venue = venueService.findById(venue.getId()); @@ -245,7 +241,7 @@ public class AppMessageHandlerAdapter implements MessageService { /** * 缓存通道 */ - public void putChannelType(String deviceName, Integer venueId, Channel channel) { + public void addLinks(String deviceName, Integer venueId, Channel channel) { String clientId = deviceName + NettyConstant.SPIT_WORD + venueId; links.put(clientId, channel); } @@ -254,7 +250,7 @@ public class AppMessageHandlerAdapter implements MessageService { * 获取当前通道 */ public Channel getCurrentChannel(String deviceName, Integer venueId){ - String clientId = deviceName + NettyConstant.SPIT_WORD + venueId + NettyConstant.SPIT_WORD; + String clientId = deviceName + NettyConstant.SPIT_WORD + venueId; return links.get(clientId); } diff --git a/netty-client/src/main/java/com/sv/netty/ClientHandler.java b/netty-client/src/main/java/com/sv/netty/ClientHandler.java index 02530a0..6121a2e 100644 --- a/netty-client/src/main/java/com/sv/netty/ClientHandler.java +++ b/netty-client/src/main/java/com/sv/netty/ClientHandler.java @@ -82,7 +82,7 @@ public class ClientHandler extends SimpleChannelInboundHandler { private HeartBeat getHbMessage() { HeartBeat hb = new HeartBeat(); hb.setDeviceName("dsadasdasd"); - hb.setVenueId(123); + hb.setVenueId(32); return hb; } diff --git a/netty-client/src/main/java/com/sv/netty/ClientInitializer.java b/netty-client/src/main/java/com/sv/netty/ClientInitializer.java index b1d9ba1..111026b 100644 --- a/netty-client/src/main/java/com/sv/netty/ClientInitializer.java +++ b/netty-client/src/main/java/com/sv/netty/ClientInitializer.java @@ -12,7 +12,7 @@ import io.netty.handler.timeout.IdleStateHandler; public class ClientInitializer extends ChannelInitializer { - private final static int TIME_HEART_BEAT = 5; + private final static int TIME_HEART_BEAT = 60; public ClientThread.ReConnectHandler reConnectHandler; public ClientHandler dmClientHandler; diff --git a/netty-model/pom.xml b/netty-model/pom.xml index acd160d..fd78eae 100644 --- a/netty-model/pom.xml +++ b/netty-model/pom.xml @@ -23,6 +23,10 @@ slf4j-api 1.7.33 + + ch.qos.logback + logback-classic + com.fasterxml.jackson.core jackson-core diff --git a/netty-model/src/main/java/com/sv/netty/config/NettyConstant.java b/netty-model/src/main/java/com/sv/netty/config/NettyConstant.java index af1de4a..ccde7eb 100644 --- a/netty-model/src/main/java/com/sv/netty/config/NettyConstant.java +++ b/netty-model/src/main/java/com/sv/netty/config/NettyConstant.java @@ -9,8 +9,8 @@ public interface NettyConstant { /** * session中存储终端发送的额外参数 */ - public static AttributeKey CHANNEL_PARAM = AttributeKey.newInstance("CHANNEL_PARAM"); + AttributeKey CHANNEL_PARAM = AttributeKey.newInstance("CHANNEL_PARAM"); - public final static String SPIT_WORD = "#"; + String SPIT_WORD = "#"; } diff --git a/netty-model/src/main/java/com/sv/netty/config/VenueMessage.java b/netty-model/src/main/java/com/sv/netty/config/VenueMessage.java index bb3d503..c4ab8ef 100644 --- a/netty-model/src/main/java/com/sv/netty/config/VenueMessage.java +++ b/netty-model/src/main/java/com/sv/netty/config/VenueMessage.java @@ -7,9 +7,12 @@ public class VenueMessage implements Serializable { private MessageType messageType; private String message; - public VenueMessage(MessageType type, String msg) { - this.messageType = type; - this.message = msg; + public VenueMessage() { + } + + public VenueMessage(MessageType messageType, String message) { + this.messageType = messageType; + this.message = message; } public MessageType getMessageType() { diff --git a/netty-model/src/main/java/com/sv/netty/utils/ServerMessageUtils.java b/netty-model/src/main/java/com/sv/netty/utils/ServerMessageUtils.java index c9ddf14..505b3a0 100644 --- a/netty-model/src/main/java/com/sv/netty/utils/ServerMessageUtils.java +++ b/netty-model/src/main/java/com/sv/netty/utils/ServerMessageUtils.java @@ -8,6 +8,8 @@ public enum ServerMessageUtils { INSTANCE; public void sendMsg(Channel channel, VenueMessage message) { - channel.writeAndFlush(EncodeMsg.INSTANCE.encode(message)); + if (channel != null) { + channel.writeAndFlush(EncodeMsg.INSTANCE.encode(message)); + } } } diff --git a/service/src/main/resources/mybatis/mapper/sv/DeviceMapper.xml b/service/src/main/resources/mybatis/mapper/sv/DeviceMapper.xml index 037cb27..76db3c0 100644 --- a/service/src/main/resources/mybatis/mapper/sv/DeviceMapper.xml +++ b/service/src/main/resources/mybatis/mapper/sv/DeviceMapper.xml @@ -8,7 +8,6 @@ - @@ -53,7 +52,6 @@ #{stream, jdbcType=VARCHAR}, #{venue_id, jdbcType=INTEGER}, #{venue_type, jdbcType=INTEGER}, - #{device_type, jdbcType=VARCHAR}, #{bind_member, jdbcType=INTEGER}, #{bind_time, jdbcType=TIMESTAMP}, #{createdId, jdbcType=INTEGER}, @@ -94,9 +92,6 @@ venue_type, - - device_type, - bind_member, @@ -138,9 +133,6 @@ #{venueType}, - - #{deviceType}, - #{bindMember}, @@ -211,9 +203,6 @@ venue_type = #{venueType}, - - device_type = #{deviceType}, - bind_member = #{bindMember}, @@ -271,49 +260,31 @@ UPDATE sv_device set status = 0,modified_time = now() WHERE name = #{deviceName} and venue_id=#{venueId} - - and device_type = #{deviceType} - UPDATE sv_device set status = 2,modified_time = now() WHERE name = #{deviceName} and venue_id=#{venueId} - - and device_type = #{deviceType} - UPDATE sv_device set bind_member = #{bindMember},bind_time = now() WHERE name = #{deviceName} and venue_id=#{venueId} - - and device_type = #{deviceType} - UPDATE sv_device set bind_member = null,bind_time = null WHERE name = #{deviceName} and venue_id=#{venueId} - - and device_type = #{deviceType} - - \ No newline at end of file +