netty实现websocket

导包

            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>4.1.6.Final</version>
            </dependency>

服务端

package com.zhk.routine.nio;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
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.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;

public class Server {
    public static void main(String[] args) {
        //来接收客户端传进来的连接
        NioEventLoopGroup bossGroup  = new NioEventLoopGroup();
        //来处理已被接收的连接
        NioEventLoopGroup workerGroup  = new NioEventLoopGroup();
        // 创建 netty 服务
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        serverBootstrap.group(bossGroup, workerGroup)
                // 设置 NIO 模式
                .channel(NioServerSocketChannel.class)
                // 设置 tcp 缓冲区
                .option(ChannelOption.SO_BACKLOG,1024)
                // 设置发送缓冲区数据大小
                .childOption(ChannelOption.SO_SNDBUF,64 * 1024)
                // 设置接收缓冲区数据大小
                .option(ChannelOption.SO_RCVBUF,64 * 1024)
                //保持长连接
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        // HttpClient编解码器
                        pipeline.addLast(new HttpServerCodec());
                        // 设置最大内容长度
                        pipeline.addLast(new HttpObjectAggregator(65536));
                        // WebSocket 数据压缩扩展
                        pipeline.addLast(new WebSocketServerCompressionHandler());
                        // WebSocket 握手、控制帧处理   /hello是路径
                        pipeline.addLast(new WebSocketServerProtocolHandler("/hello", null, true));
                        // 通道的初始化,数据传输过来进行拦截及执行
                        pipeline.addLast(new ConnectHandler());
                    }
                });
                // 绑定端口启动服务
        ChannelFuture channelFuture = null;
        try {
        //8081端口号
            channelFuture = serverBootstrap.bind(8081).sync();
            channelFuture.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}

接收消息

package com.zhk.routine.nio;

import com.google.common.collect.Lists;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.ArrayList;

public class ConnectHandler extends ChannelInboundHandlerAdapter {
    private static ArrayList<ChannelHandlerContext> list = Lists.newArrayList();
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("通道激活(回调)");
        if (!list.contains(ctx)) {
            System.out.println("添加");
            list.add(ctx);
        }

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        // 仅处理 TextWebSocketFrame
        if (msg instanceof TextWebSocketFrame) {
            String request = ((TextWebSocketFrame) msg).text();
            System.out.println("收到请求:" + request);

            list.forEach(channelHandlerContext ->
            {
                System.out.println("来了");
                channelHandlerContext.writeAndFlush(new TextWebSocketFrame("我回复消息"));
            });
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("数据读取完成");
    }
}

测试

http://www.easyswoole.com/wstool.html

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张航柯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值