netty开2个端口实现socket连接和websocket连接

第一:引入netty依赖

<!--netty-->

<dependency>

  <groupId>io.netty</groupId>

  <artifactId>netty-all</artifactId>

  <version>5.0.0.Alpha1</version>

</dependency>

第二:创建netty服务端

package com.cx.netty;



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.codec.string.StringDecoder;

import io.netty.handler.codec.string.StringEncoder;

import io.netty.handler.stream.ChunkedWriteHandler;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;



@ChannelHandler.Sharable

@Component

public class NettyServer {

  @Autowired

  private ServerHandle serverHandle;

  @Resource

  private ServerHandle01 serverHandle01;



  public void run() throws InterruptedException {



    NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);

    NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    try {



      ServerBootstrap bootstrap = new ServerBootstrap();



      bootstrap.group(bossGroup, workerGroup)

          .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();

              pipeline.addLast("decoder", new StringDecoder());//加入解码器

              pipeline.addLast("encoder", new StringEncoder());//加入编码器

              pipeline.addLast(serverHandle);//自定义

            }

          });



      ServerBootstrap bootstrap01 = new ServerBootstrap();

      bootstrap01.group(bossGroup, workerGroup)

          .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();

              pipeline.addLast(new HttpServerCodec());//http协议编码器

              pipeline.addLast(new ChunkedWriteHandler());//以块的形式来写

              pipeline.addLast(new HttpObjectAggregator(1024 * 1024 * 1024));//获取请求的最大的长度

              pipeline.addLast(new WebSocketServerProtocolHandler("/chat"));//适配 ws://127.0.0.1:6001/chat

              pipeline.addLast(serverHandle01);//自定义

            }

          });



      ChannelFuture channelFuture = bootstrap.bind(8088).sync();

      ChannelFuture channelFuture01 = bootstrap01.bind(8099).sync();

      channelFuture01.channel().closeFuture().sync();

      channelFuture.channel().closeFuture().sync();



    } finally {

      bossGroup.shutdownGracefully();

      workerGroup.shutdownGracefully();

    }



  }

}


第三:创建serverHandle.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值