-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
huaran
committed
Jan 16, 2019
1 parent
824cb40
commit 82aade1
Showing
5 changed files
with
153 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
java/netty/heartbeatv2/src/main/java/com/fhr/netty/hearbeatv2/DelimiterEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.fhr.netty.hearbeatv2; | ||
|
||
import io.netty.buffer.ByteBufUtil; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.MessageToMessageEncoder; | ||
|
||
import java.nio.CharBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Fan Huaran | ||
* created on 2018/12/29 | ||
* @description | ||
*/ | ||
public class DelimiterEncoder extends MessageToMessageEncoder<String> { | ||
|
||
@Override | ||
protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception { | ||
// out.add(msg + "\r\n"); | ||
if (msg.length() == 0) { | ||
return; | ||
} | ||
|
||
out.add(ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(msg + "\r\n"), StandardCharsets.UTF_8)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
java/netty/heartbeatv2/src/main/java/com/fhr/netty/hearbeatv2/client/ClientBoot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.fhr.netty.hearbeatv2.client; | ||
|
||
import com.fhr.netty.hearbeatv2.DelimiterEncoder; | ||
import com.fhr.netty.hearbeatv2.HeartbeatHandler; | ||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelPipeline; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
import io.netty.handler.codec.DelimiterBasedFrameDecoder; | ||
import io.netty.handler.codec.string.StringDecoder; | ||
import io.netty.handler.timeout.IdleStateHandler; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
/** | ||
* @author Fan Huaran | ||
* created on 2018/12/29 | ||
* @description | ||
*/ | ||
public class ClientBoot { | ||
public static void main(String[] args) throws ExecutionException, InterruptedException { | ||
NioEventLoopGroup workerGroup = new NioEventLoopGroup(1); | ||
|
||
Bootstrap bootstrap = new Bootstrap() | ||
.group(workerGroup) | ||
.channel(NioSocketChannel.class) | ||
.handler(new ChannelInitializer() { | ||
@Override | ||
protected void initChannel(Channel ch) throws Exception { | ||
ChannelPipeline p = ch.pipeline(); | ||
// 添加空闲状态处理器 | ||
p.addLast(new IdleStateHandler(0, 0, 10)); | ||
// 添加行分隔符解码器 | ||
p.addLast(new DelimiterBasedFrameDecoder(1024, true, Unpooled.copiedBuffer("&&".getBytes(StandardCharsets.UTF_8)))); | ||
// 添加字符串解码器 | ||
p.addLast(new StringDecoder(StandardCharsets.UTF_8)); | ||
// 添加行分隔符编码器 | ||
p.addLast(new DelimiterEncoder()); | ||
// p.addLast(new StringEncoder(StandardCharsets.UTF_8)); | ||
// 添加心跳处理器 | ||
p.addLast(new HeartbeatHandler()); | ||
// 添加请求处理器 | ||
} | ||
}); | ||
|
||
ChannelFuture future = bootstrap.connect("127.0.0.1", 12345).sync(); | ||
Channel channel = future.channel(); | ||
channel.writeAndFlush("TEST"); | ||
|
||
// TODO | ||
// future.get(); | ||
// workerGroup.shutdownGracefully(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
java/netty/heartbeatv2/src/main/java/com/fhr/netty/hearbeatv2/server/ServerBoot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.fhr.netty.hearbeatv2.server; | ||
|
||
import com.fhr.netty.hearbeatv2.DelimiterEncoder; | ||
import com.fhr.netty.hearbeatv2.HeartbeatHandler; | ||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelPipeline; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import io.netty.handler.codec.LineBasedFrameDecoder; | ||
import io.netty.handler.codec.string.StringDecoder; | ||
import io.netty.handler.timeout.IdleStateHandler; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* @author Fan Huaran | ||
* created on 2018/12/29 | ||
* @description | ||
*/ | ||
public class ServerBoot { | ||
public static void main(String[] args) { | ||
NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); | ||
NioEventLoopGroup workGroup = new NioEventLoopGroup(4); | ||
try { | ||
|
||
|
||
ServerBootstrap bootstrap = new ServerBootstrap(); | ||
bootstrap.group(bossGroup, workGroup) | ||
.channel(NioServerSocketChannel.class) | ||
.childHandler(new ChannelInitializer<SocketChannel>() { | ||
protected void initChannel(SocketChannel socketChannel) throws Exception { | ||
ChannelPipeline p = socketChannel.pipeline(); | ||
// 添加空闲状态处理器 | ||
p.addLast(new IdleStateHandler(0, 0, 10)); | ||
// 添加行分隔符解码器 | ||
// p.addLast(new DelimiterBasedFrameDecoder(1024,true,Unpooled.copiedBuffer("&&".getBytes(StandardCharsets.UTF_8)))); | ||
p.addLast(new LineBasedFrameDecoder(1024)); | ||
// 添加字符串解码器 | ||
p.addLast(new StringDecoder(StandardCharsets.UTF_8)); | ||
// 添加行分隔符编码器 | ||
p.addLast(new DelimiterEncoder()); | ||
// p.addLast(new StringEncoder(StandardCharsets.UTF_8)); | ||
// 添加心跳处理器 | ||
p.addLast(new HeartbeatHandler()); | ||
} | ||
}); | ||
|
||
Channel ch = bootstrap.bind(12345).sync().channel(); | ||
ch.closeFuture().sync(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
bossGroup.shutdownGracefully(); | ||
workGroup.shutdownGracefully(); | ||
} | ||
|
||
} | ||
} |