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

建一个网站需要多少时间表产品定制网站开发

建一个网站需要多少时间表,产品定制网站开发,珠海网站建设网络公司,网页设计行业上次通信的时候用的是自带的编解码器#xff0c;今天自己实现一下自定义的。 1、自定义一下协议 //协议类 Data public class ProtocolT implements Serializable {private Long id System.currentTimeMillis();private short msgType;// 假设1为请求 2为响应privat…上次通信的时候用的是自带的编解码器今天自己实现一下自定义的。 1、自定义一下协议 //协议类 Data public class ProtocolT 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 MessageToByteEncoderProtocolObject {Overrideprotected void encode(ChannelHandlerContext channelHandlerContext, ProtocolObject 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, ListObject 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){ProtocolRequestMsg 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){ProtocolResponseMsg 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 SimpleChannelInboundHandlerProtocolResponseMsg {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,ProtocolResponseMsg o) throws Exception {logger.info(channelRead0--------------Thread.currentThread().getName());logger.info(消费者接收到的消息为{}, JSONObject.toJSONString(o));}public void sendMsg(ProtocolRequestMsg message){channel.writeAndFlush(message);}public void close(){channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);}}public class NettyServerHandler extends SimpleChannelInboundHandlerProtocolRequestMsg {private static final Logger logger LoggerFactory.getLogger(NettyServerHandler.class);Overrideprotected void channelRead0(ChannelHandlerContext channelHandlerContext, ProtocolRequestMsg o) throws Exception {logger.info(服务端收到的消息为{}, JSONObject.toJSONString(o));ProtocolResponseMsg 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);ProtocolRequestMsg 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.hkea.cn/news/14501553/

相关文章:

  • 做网站放广告赚钱手机在线做ppt模板下载网站有哪些
  • 什么网站用php做的flash网站源码带后台
  • 网站制作案例招远网站建设公司报价
  • 手游网站开发企业官网营销推广
  • 简单大气网站欣赏黄骅贴吧最新消息金鼎18号
  • 阜阳学网站建设室内设计素材网站哪个最好
  • 做内部网站cms网站500错误是什么意思
  • 淘宝客网站做一种还是做好几种阿里云 域名申请
  • oracle网站开发郑州优之客网站建设
  • 洛阳网站建站法律网站建设实施方案
  • 网站开发不让搜索引擎吉林省住房建设安厅网站
  • 烟台哪个公司做网站好黄强会见韩国忠清南道知事金泰钦
  • 中文建网站微信开放平台怎么注册
  • 网站恶意点击wordpress上传到域名
  • 好网站开发公司小说网站怎么做流量吗
  • 哈尔滨学校网站建设营销qq购买
  • 做基因结构可以用哪个网站中化山东公路建设集团网站
  • 网站建设分为多少模块网页设计制作代码大全
  • 湖南seo网站策划网站开发基本要求
  • 免费1级做爰片在线网站网站不兼容360浏览器
  • 无锡网站建设专业极速信息外包公司网络安全管理制度
  • 龙岗做网站公司企业官网网页设计报价
  • 手机版网站建设报价北京网络seo经理
  • 红酒网站定位如何做解析网站
  • 网站更新中网站建设服务平台
  • 怎样申请免费网站空间家在深圳 业主论坛
  • dedecms如何做网站专业微网站电话
  • 德阳网站开发用ps怎么做短视频网站
  • wordpress网站手机端菜单栏怎样建设一个游戏网站
  • 国外seo网站嵌入式培训宣传