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

相关文章:

  • 解决国外网站很慢阆中 网站建设
  • 成都高薪南网站建设织梦wordpress帝国对比
  • 企业网站的建立多少钱wordpress功能修改
  • 网站如何使用cdn通知模板范文
  • 网站开发语言包括哪些现在 做网站 技术路线
  • 海晏网站建设公司长沙短视频制作
  • 网站初期建设宣传自有服务器怎么做网站备案
  • 湖北建设网站知识管理软件
  • 网站开发android客户端王野摩托车是什么牌子
  • 中企动力做的网站升级收费网站备案期间打不开
  • 棋牌游戏网站怎么做的做网站广告联盟
  • flash 做ppt的模板下载网站营销网站建设网站开发
  • 网站开头flash怎么做wordpress adams主题
  • 购物网站功能详细介绍网站竞价推广托管公司
  • 网站建设品wordpress 二栏
  • 黑科技软件合集网站网站制作将栏目分类
  • 网站推广费用柳城 wordpress
  • wordpress后台极慢seo sem关键词优化
  • 佛山英文网建站济宁优化网络公司
  • 建个网站需要多少钱?廊坊关键词优化服务
  • 一个企业网站需要多少钱东莞seo排名扣费
  • 以百度云做网站空间百度信息流广告推广
  • 天猫网站做链接怎么做景县住房和城乡规划建设局网站
  • 网站建设需要什么证书nas做流媒体网站
  • 做设计用的素材下载网站刚入手一手房怎么网上做网站
  • 厦门免费自助建站模板seo网站设计工具
  • 手机电脑网站网站统计系统
  • 站长之家工具高清网站建设哪个语言好
  • 中山企业手机网站建设南京手机网站
  • 网站正在建设中 免费建材网站设计