首先服務端代碼:
package com.nettytest;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
import java.util.Scanner;
public class NettyServer {
public static void main(String[] args) {
ServerBootstrap serverBootstrap = new ServerBootstrap();
NioEventLoopGroup boos = new NioEventLoopGroup();
NioEventLoopGroup worker = new NioEventLoopGroup();
serverBootstrap
.group(boos, worker)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
protected void initChannel(final NioSocketChannel ch) {
ch.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
ch.pipeline().addLast(new SimpleChannelInboundHandler<TextWebSocketFrame>() {
@Override
public void channelActive(ChannelHandlerContext ctx) {
System.out.println("我執行樂樂樂");
System.out.println("1"+ctx.channel());
Singleton.getSingleton().setChannel(ctx.channel());
}
protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception {
}
});
ch.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
System.out.println("11"+ctx.channel());
System.out.println(msg);
}
});
}
})
.bind(8000);
Scanner sc = new Scanner(System.in);
while (true) {
String next = sc.next();
Singleton.getSingleton().getChannel().writeAndFlush(next);
}
}
}
其次是用戶端代碼:
package com.nettytest;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.ReferenceCountUtil;
import java.util.Scanner;
public class NettyClient {
public static void main(String[] args) throws InterruptedException {
Bootstrap bootstrap = new Bootstrap();
NioEventLoopGroup group = new NioEventLoopGroup();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) {
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
try {
System.out.println("用戶端接收到消息了:");
while (in.isReadable()) { // (1)
System.out.print((char) in.readByte());
System.out.flush();
}
} finally {
ReferenceCountUtil.release(msg); // (2)
}
}
});
}
});
Channel channel = bootstrap.connect("127.0.0.1", 8000).channel();
Scanner sc = new Scanner(System.in);
while (true) {
String next = sc.next();
channel.writeAndFlush(next);
}
}
}
用來擷取用戶端Channel的單例:
package com.nettytest;
import io.netty.channel.Channel;
public class Singleton {
private Channel channel;
public void setChannel(Channel channel){
this.channel = channel;
}
public Channel getChannel(){
return this.channel;
}
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getSingleton() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
其中一點,服務端給用戶端發消息,拿到用戶端的Channel就可以發送了,是以為了擷取用戶端的Channel,我使用了單例進行儲存。
這些代碼可以直接複制,是可以運作的 哦對 得導入netty的依賴
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.50.Final</version>
</dependency>
這個代碼不能發送中文,不然它會出現亂碼的情況,目前還沒有解決。
這裡邊的有很多匿名内部類,開發中,這些一般也都是單獨拿出來進行實作的 然後再将執行個體寫到目前寫的匿名内部類的位置。