Netty实现服务器和客户端WebSocket长连接
要求:
实现基于WebSocket的长连接的全双工交互
改变http协议多次请求的约束实现长连接,服务器可以发送消息给浏览器
客户端和浏览器端会相互感知对方是否关闭连接
代码
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
public class HttpLongServer {
public static void main(String[] args) {
EventLoopGroup boss = new NioEventLoopGroup(1);
EventLoopGroup work = new NioEventLoopGroup();
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(boss,work)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,128)
.childOption(ChannelOption.SO_KEEPALIVE,true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
//基于http协议所以需要http协议解码器
pipeline.addLast(new HttpServerCodec());
//以块的方式读写需要增加chunkedWriteHandler
pipeline.addLast(new ChunkedWriteHandler());
//http在传输中是分段的需要使用httpObjectAggregator来进行聚合
pipeline.addLast(new HttpObjectAggregator(8192));
//websocket使用帧形式传递需要使用websocketServerProtoHandler把http请求转为ws请求
pipeline.addLast(new WebSocketServerProtocolHandler("/hello"));
pipeline.addLast(new MyHttpServerHandler());
}
});
ChannelFuture future = serverBootstrap.bind(8899).sync();
future.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
boss.shutdownGracefully();
work.shutdownGracefully();
}
}
}
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import java.time.LocalDate;
public class MyHttpServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception {
//输出
System.out.println("服务器接收到消息:"+textWebSocketFrame.text());
channelHandlerContext.channel().writeAndFlush(new TextWebSocketFrame("["+LocalDate.now()+"服务器已接收到消息]"+ textWebSocketFrame.text()));
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
System.out.println("已连接到服务器"+ LocalDate.now());
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
System.out.println("已断开服务器"+ LocalDate.now());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.channel().closeFuture();
}
}
前端页面`
<textarea name="message" style="height: 200px;width: 400px"></textarea>
<!-- 这个send的value其实也可以通过值得到-->
<input type="button" value="发送数据" onclick="send(this.form.message.value)">
<textarea id="responseText" style="width: 400px;height: 200px"></textarea>
<input type="button" onclick="document.getElementById('responseText').value=''" value="清空内容">
`