https://juejin.im/book/5b4bc28bf265da0f60130116
https://item.jd.com/13611960.html
master 为项目全量代码,建议读者根据章节顺序,切换到每个章节对应的代码分支,循序渐进学习。
更多源码阅读技巧相关的文章,欢迎关注 《闪电侠的博客》
掘金小册及实体书源码
https://juejin.im/book/5b4bc28bf265da0f60130116
https://item.jd.com/13611960.html
master 为项目全量代码,建议读者根据章节顺序,切换到每个章节对应的代码分支,循序渐进学习。
更多源码阅读技巧相关的文章,欢迎关注 《闪电侠的博客》
购买了您掘金和慕课网的课程,很优质的内容,很棒,非常感谢。
最近在做一个项目,使用到了zlib压缩,大致的流程是这样:
78 9c ca 48 cd c9 c9 d7 51 c8 4b 2d 29 a9 54 04 00 00 00 ff ff eb 90 eb 90 eb 90
字符串 -> zlib压缩 -> 添加包尾 -> 发送
读取一帧 -> 去包尾 -> zlib解压 -> 字符串
其中包尾定义为EB 90 EB 90 EB 90
protected void initChannel(NioSocketChannel ch) {
log.info("init channel !");
// 处理读数据的逻辑链
ByteBuf delimiter = Unpooled.copiedBuffer(Constants.TAIL);
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(2048, delimiter));
ch.pipeline().addLast(new JdkZlibDecoder());
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new InBoundHandler());
// 写逻辑处理
ch.pipeline().addLast(new DelimiterEncoder());
ch.pipeline().addLast(new JdkZlibEncoder());
ch.pipeline().addLast(new StringEncoder());
}
调试中,我发现io.netty.handler.codec.compression
包下的JdkZlibDecoder
和JdkZlibEncoder
都有一个finish
标记,这就导致我只能编解码一次,不清楚为何这样设计,最后我只能用了笨办法:
@Slf4j
public class InBoundHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
log.info("MyInBoundHandler: " + msg);
ctx.pipeline().replace(JdkZlibDecoder.class, "zlibDecoder", new JdkZlibDecoder());
String content = "hello, netty!";
ctx.channel().writeAndFlush(content);
}
}
@Slf4j
public class DelimiterEncoder extends MessageToByteEncoder<ByteBuf> {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
log.info("writeBytes");
System.err.println(ByteBufUtil.prettyHexDump(msg));
out.writeBytes(msg);
out.writeBytes(Constants.TAIL);
ctx.pipeline().replace(JdkZlibEncoder.class, "zlibEncoder", new JdkZlibEncoder());
}
}
19:49:03.127 [nioEventLoopGroup-3-3] WARN io.netty.channel.DefaultChannelPipeline - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: index: 222, length: 4 (expected: range(0, 224))
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:480)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:279)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IndexOutOfBoundsException: index: 222, length: 4 (expected: range(0, 224))
at io.netty.buffer.AbstractByteBuf.checkRangeBounds(AbstractByteBuf.java:1390)
at io.netty.buffer.AbstractByteBuf.checkIndex0(AbstractByteBuf.java:1397)
at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1384)
at io.netty.buffer.AbstractByteBuf.getInt(AbstractByteBuf.java:433)
at com.example.demo.im.Spilter.decode(Spilter.java:21)
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:332)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:510)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:449)
... 17 common frames omitted
Hi,
We are developing a tool to check whether the dependencies' scopes in maven are set rightly and reasonably.
Our tools have found some issues in your project about scope setting in maven' pom.xml file.
pom.xml
, junit:junit
scope should be test
instead of compile
.Please help us confirm whether the above reports are right and explain why it should set like these.
Thanks a lot.
你好;
代码中存在一个问题;
the.flash.protocol.Packet 在被传递的过程中;
使用 ctx.writeAndFlush(msg)调用链路
会走到 AbstractChannel#write(Object msg, ChannelPromise promise)
其主要代码:
...
// 依据msg的类型对其msg进行过滤
msg = filterOutboundMessage(msg);
...
//发送msg
outboundBuffer.addMessage(msg, size, promise);
而在我们代码中 msg 全部为 自定义的Packet类型,而非ByteBuf
这会导致数据无法发送;
应替换为
ctx.channel().writeAndFlush(msg)
其调用链路会走encode,将msg通过我们定义的encoder转变为ByteBuf
怎么加好友呢
js 里面 都是 WebSocket进行通信.如果使用课程里面的自定义协议,是不是只有用 js 写 Socket 才可以进行连接呢? 问了好多群都没有找到如何使用这里的自定义协议...
不然只有修改 netty 使用WebSocket协议进行通信了. 求大佬指点迷津呀..
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.