netty 简单应用

java 后端服务

启动类

package netty;

import io.netty.bootstrap.ServerBootstrap;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.*;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.nio.NioServerSocketChannel;

import io.netty.handler.codec.http.*;

import io.netty.handler.codec.http.websocketx.*;

import io.netty.handler.stream.ChunkedWriteHandler;

import io.netty.util.CharsetUtil;

import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

/**

* Created by yehan on 2018/4/27.

*/

public class WebSocketServer {

private final EventLoopGroupworkerGroup =new NioEventLoopGroup();

    private final EventLoopGroupbossGroup =new NioEventLoopGroup();

    private WebSocketServerHandshakerhandshaker;

    private MapchannelMap =new ConcurrentHashMap();

    public void run() {

ServerBootstrap boot =new ServerBootstrap();

        boot.group(bossGroup, workerGroup)

.channel(NioServerSocketChannel.class)

.childHandler(new ChannelInitializer() {

@Override

                    protected void initChannel(Channel ch)throws Exception {

ChannelPipeline pipeline = ch.pipeline();

                        channelMap.put(ch.id(),ch);

                        pipeline.addLast("http-codec", new HttpServerCodec());

                        pipeline.addLast("aggregator", new HttpObjectAggregator(65536));

                        pipeline.addLast("http-chunked", new ChunkedWriteHandler());

                        pipeline.addLast("handler", new WebSocketServerHandler(WebSocketServer.this));

                    }

});

        try {

Channel ch = boot.bind(2048).sync().channel();

            System.out.println("websocket server start at port:2048");

            ch.closeFuture().sync();

        }catch (InterruptedException e) {

e.printStackTrace();

        }finally {

bossGroup.shutdownGracefully();

            workerGroup.shutdownGracefully();

        }

}

public void handlerWebSocketFrame(ChannelHandlerContext ctx,

                                      WebSocketFrame frame) {

/**

* 判断是否关闭链路的指令

*/

        if (frameinstanceof CloseWebSocketFrame) {

handshaker.close(ctx.channel(),

                    (CloseWebSocketFrame) frame.retain());

return;

        }

/**

* 判断是否ping消息

*/

        if (frameinstanceof PingWebSocketFrame) {

ctx.channel().write(

new PongWebSocketFrame(frame.content().retain()));

return;

        }

/**

* 本例程仅支持文本消息,不支持二进制消息

*/

        if (frameinstanceof BinaryWebSocketFrame) {

throw new UnsupportedOperationException(String.format(

"%s frame types not supported", frame.getClass().getName()));

        }

if(frameinstanceof TextWebSocketFrame){

// 返回应答消息

            String request = ((TextWebSocketFrame) frame).text();

            System.out.println("服务端收到:" + request);

//            ctx.channel().writeAndFlush(new TextWebSocketFrame("服务器收到并返回:"+request));

            for (Channel ch :channelMap.values()) {

if (ctx.channel().equals(ch)) {

ch.writeAndFlush(new TextWebSocketFrame("我发送的:"+request));

continue;

                }

ch.writeAndFlush(new TextWebSocketFrame("收到:"+request));

            }

}

}

public void handleHttpRequest(ChannelHandlerContext ctx,FullHttpRequest req){

if (!req.getDecoderResult().isSuccess()

|| (!"websocket".equals(req.headers().get("Upgrade")))) {

sendHttpResponse(ctx, req, new DefaultFullHttpResponse(

HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));

return;

        }

WebSocketServerHandshakerFactory wsFactory =new WebSocketServerHandshakerFactory(

"ws://localhost:2048/ws", null, false);

        handshaker = wsFactory.newHandshaker(req);

        if (handshaker ==null) {

WebSocketServerHandshakerFactory

.sendUnsupportedWebSocketVersionResponse(ctx.channel());

        }else {

handshaker.handshake(ctx.channel(), req);

        }

}

private static void sendHttpResponse(ChannelHandlerContext ctx,

                                        FullHttpRequest req, DefaultFullHttpResponse res) {

// 返回应答给客户端

        if (res.getStatus().code() !=200) {

ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(),

                    CharsetUtil.UTF_8);

            res.content().writeBytes(buf);

            buf.release();

        }

// 如果是非Keep-Alive,关闭连接

        ChannelFuture f = ctx.channel().writeAndFlush(res);

        if (!isKeepAlive(req) || res.getStatus().code() !=200) {

f.addListener(ChannelFutureListener.CLOSE);

        }

}

private static boolean isKeepAlive(FullHttpRequest req) {

return false;

    }

public static void main(String[] args) {

new WebSocketServer().run();

    }

}


处理类

package netty;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.SimpleChannelInboundHandler;

import io.netty.handler.codec.http.FullHttpRequest;

import io.netty.handler.codec.http.websocketx.WebSocketFrame;

/**

* Created by yehan on 2018/4/27.

*/

public class WebSocketServerHandlerextends SimpleChannelInboundHandler {

private  WebSocketServerwebSocketServer;

    public WebSocketServerHandler(WebSocketServer webSocketServer) {

super();

        this.webSocketServer = webSocketServer;

    }

@Override

    protected void channelRead0(ChannelHandlerContext ctx, Object msg)

throws Exception {

/**

* HTTP接入,WebSocket第一次连接使用HTTP连接,用于握手

*/

        if(msginstanceof FullHttpRequest){

webSocketServer.handleHttpRequest(ctx, (FullHttpRequest)msg);

            System.out.println("http请求");

        }

/**

* Websocket 接入

*/

        else if(msginstanceof WebSocketFrame){

webSocketServer.handlerWebSocketFrame(ctx, (WebSocketFrame)msg);

        }

}

@Override

    public void channelReadComplete(ChannelHandlerContext ctx)throws Exception {

ctx.flush();

    }

}

客户端


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • netty常用API学习 netty简介 Netty是基于Java NIO的网络应用框架. Netty是一个NIO...
    花丶小伟阅读 6,059评论 0 20
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,001评论 19 139
  • 2018年4月1日 周日 天气晴 今天依旧是明媚的一天。儿子为了今天上午能去图书馆,一早起来完成了剩下的一点儿作业...
    阳光明媚_bf41阅读 226评论 0 3
  • 我不要你的祝福 甜言蜜语只会让人中毒 如果你对我真的在乎 就请好好保重自己别让我为你而哭 我不要你的呵护 风雨再大...
    狼神2019阅读 565评论 21 24
  • 曾经在准备一场重要考试的数月中,我体会到了书中或文献中所不曾具体描述过的真理:把压力化为动力。它似乎很抽象,...
    离默青雪阅读 250评论 0 0