当前位置: 首页 > news >正文

专业外包网站建设公司排名企业门户网站建设案例

专业外包网站建设公司排名,企业门户网站建设案例,在欣欣网上做网站效果如何,龙岩平面设计上次通信的时候用的是自带的编解码器&#xff0c;今天自己实现一下自定义的。 1、自定义一下协议 //协议类 Data public class Protocol<T> implements Serializable {private Long id System.currentTimeMillis();private short msgType;// 假设1为请求 2为响应privat…

上次通信的时候用的是自带的编解码器,今天自己实现一下自定义的。
1、自定义一下协议

//协议类
@Data
public class Protocol<T> implements Serializable {private Long id = System.currentTimeMillis();private short msgType;// 假设1为请求 2为响应private T body;}//消息请求体
@Data
public class RequestMsg implements Serializable {private String msg;private String other;}//消息响应体
@Data
public class ResponseMsg implements Serializable {private String result;private String error;}

2、定义编解码器import io.netty.buffer.ByteBuf;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;//编码器
public class EnCodeMsg extends MessageToByteEncoder<Protocol<Object>> {@Overrideprotected void encode(ChannelHandlerContext channelHandlerContext, Protocol<Object> msg, ByteBuf byteBuf) throws Exception {Serialization serialization = new JdkSerialization();byte[] body = serialization.serialize(msg.getBody());int length = body.length;Long id = msg.getId();short msgType = msg.getMsgType();byteBuf.writeLong(id);byteBuf.writeShort(msgType);byteBuf.writeInt(length);byteBuf.writeBytes(body);}
}import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;import java.util.List;//解码器
public class DeCodeMsg extends ByteToMessageDecoder {@Overrideprotected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list) throws Exception {Serialization serialization = new JdkSerialization();long id = in.readLong();short msgType = in.readShort();int bodyLength = in.readInt();int i = in.readableBytes();if(bodyLength!=i){in.resetReaderIndex();return;}byte[] bytes = new byte[bodyLength];in.readBytes(bytes);if(msgType==(short)1){Protocol<RequestMsg> requestMsgProtocol = new Protocol<>();RequestMsg requestMsg = serialization.deserialize(bytes, RequestMsg.class);requestMsgProtocol.setBody(requestMsg);requestMsgProtocol.setId(id);requestMsgProtocol.setMsgType(msgType);list.add(requestMsgProtocol);}else if(msgType==(short)2){Protocol<ResponseMsg> responseMsgProtocol = new Protocol<>();ResponseMsg responseMsg = serialization.deserialize(bytes,ResponseMsg.class);responseMsgProtocol.setId(id);responseMsgProtocol.setMsgType(msgType);responseMsgProtocol.setBody(responseMsg);list.add(responseMsgProtocol);}else {return;}}
}

3、修改消息处理器


public class NettyClientHandler extends SimpleChannelInboundHandler<Protocol<ResponseMsg>> {private static final Logger logger = LoggerFactory.getLogger(NettyClientHandler.class);private volatile Channel channel;private SocketAddress remotePeer;public Channel getChannel() {return channel;}public SocketAddress getRemotePeer() {return remotePeer;}/*** 注册* @param ctx* @throws Exception*/@Overridepublic void channelRegistered(ChannelHandlerContext ctx) throws Exception {logger.info("channelRegistered--------------");super.channelRegistered(ctx);this.channel = ctx.channel();}/*** 激活* @param ctx* @throws Exception*/@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {super.channelActive(ctx);this.remotePeer = this.channel.remoteAddress();logger.info("channelActive--------------");}@Overrideprotected void channelRead0(ChannelHandlerContext channelHandlerContext,Protocol<ResponseMsg> o) throws Exception {logger.info("channelRead0--------------"+Thread.currentThread().getName());logger.info("消费者接收到的消息为{}", JSONObject.toJSONString(o));}public void sendMsg(Protocol<RequestMsg> message){channel.writeAndFlush(message);}public void close(){channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);}}
public class NettyServerHandler extends SimpleChannelInboundHandler<Protocol<RequestMsg>> {private static final Logger logger = LoggerFactory.getLogger(NettyServerHandler.class);@Overrideprotected void channelRead0(ChannelHandlerContext channelHandlerContext, Protocol<RequestMsg> o) throws Exception {logger.info("服务端收到的消息为================{}", JSONObject.toJSONString(o));Protocol<ResponseMsg> protocol = new Protocol<>();ResponseMsg responseMsg = new ResponseMsg();responseMsg.setResult("SUCCESS");responseMsg.setError("NO ERROR");protocol.setBody(responseMsg);protocol.setMsgType((short) 2);protocol.setId(o.getId());channelHandlerContext.channel().writeAndFlush(protocol);}
}

4、测试

public class NettyTest {public static void main(String[] args) {new Thread(()->{NettyServer.startNettyServer();}).start();new Thread(()->{NettyClient instance = NettyClient.getInstance();try {while (true){Thread.sleep(2000);Protocol<RequestMsg> protocol = new Protocol<>();protocol.setMsgType((short)1);RequestMsg requestMsg = new RequestMsg();requestMsg.setMsg("hello:"+System.currentTimeMillis());requestMsg.setOther("你好啊");protocol.setBody(requestMsg);instance.sendMsg(protocol);}} catch (Exception e) {e.printStackTrace();}}).start();}
}

5、效果截图

在这里插入图片描述

http://www.yayakq.cn/news/106456/

相关文章:

  • 两个网站放在同一个服务器 备案建设部网站电子政务
  • 网站域名解析步骤视频网站如何做引流
  • 给非法公司做网站维护网站空间内存
  • 好网站建设公司服务公司简介模板下载
  • 品牌宣传型网站建设方案河北省建设项目环境官网网站
  • 营销网站建设优化网站制作多少钱一年
  • 电子商务网站建设与管理实训东营市建设信息网站
  • 网站建设试题 jsp免费拓客软件排行榜
  • wordpress推广机制廊坊seo关键字排名
  • 建设银行网站ie11打不开做关于什么样的网站好
  • 可以做外国网站文章网站仿造
  • 学校网站建设答辩莱芜二手房网
  • 岳西县住房和城乡建设局网站福建响应式网站制作
  • 做旅行社网站多少钱宣传片拍摄注意事项
  • 做网站犯法吗婚纱网站开发
  • 湖州住房建设部网站代码网
  • 箱包商城网站建设深圳品牌网站制作公司哪家好
  • 如何制作app网站怎样找出那些没有做友链的网站
  • 建设银行积分兑换商城网站wordpress 主题 love
  • 如何新做的网站让百度快速收录一流的江苏网站建设
  • 建设银行的网站用户名是什么Wordpress做APP后端
  • 重庆锅炉网站建设公司wordpress绑定百家号
  • 做企业网站安装什么系统好开发网站的软件
  • 漳诈网站建设网站设计与制作成品作品
  • 网站的好坏centos7 wordpress 安装
  • 徐水网站建设公司电商网站活动推广
  • 佳木斯网站建设哪家好如何给一个网站做优化
  • 山东商祺网站建设优化公司网站模板侵权案例
  • 手机h5免费模板网站中国建设机械教育协会网站
  • 深圳自己做网站 服务器深圳网站制作必推祥奔科技