当前位置: 首页 > 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.mmbaike.com/news/24348.html

相关文章:

  • 建设部标准定额网站营销型网站建设公司价格
  • 赣县网站建设软件开发工资一般多少
  • 做市场分析的网站seo搜索引擎优化方式
  • 建筑公司企业愿景文案珠海优化seo
  • 网购哪个平台最靠谱百度手机seo
  • 做网站需要备案舆情危机公关公司
  • 神木网站建设最基本的网站设计
  • 公司怎么在百度做网站网站关键词优化排名技巧
  • 网站免费建站美国网站在线检测网站安全
  • 做网站技术宁波百度关键词推广
  • 手机网站样式代码百度快照seo
  • 毕业设计做网站有哪些需求网络广告案例
  • 做网站投入刷粉网站推广免费
  • 德阳手机网站建设百度数据研究中心
  • 让别人做网站如何防止后门竞价推广托管开户
  • 网站开发和网站建设站长统计幸福宝下载
  • 做空压机网站搜索引擎推广的方法有
  • 网站建设企业模板下载微信公众号软文怎么写
  • 企业网站如何建立网站流量统计分析报告
  • 甘肃商城网站建设数据分析培训班
  • 网站建设语录web网址
  • b2b网站网络推广seo快速上排名
  • 北航刘禹导师做网站收录网站查询
  • 畅言 wordpress插件武汉网站seo公司
  • 1232网址之家seo海外
  • 怎样做seo网站推广短视频推广平台有哪些
  • wordpress的seo作用win7系统优化工具
  • wordpress多用户插件网站搜索优化找哪家
  • 铁路网站建设论文百度上怎么免费开店
  • 微信小程序怎么关闭防沉迷seo建站公司推荐