Springboot中使用netty 实现 WebSocket 服务

依赖


    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.77.Final</version>
    </dependency>
创建启动类
package com.message.after;

import com.message.websocket.WebSocketServer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @author kuaiting
 */
@Component
public class AfterExecuteMethods implements CommandLineRunner {
	/**
	 * 项目启动之后立即执行的方法,可以做些初始化项目的操作以及需要启动项目立即执行的任务
	 * @param args
	 * @throws Exception
	 */
	@Override
	public void run(String... args) throws Exception {
		/**
		 * 启动WebSocketServer 服务使用netty实现
		 */
		new WebSocketServer().start();
	}
}

创建WebSocket 服务
package com.message.websocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author kuaiting
 * WebSocket
 */

public class WebSocketServer {


	public void start() {
		// 一个主线程组
		NioEventLoopGroup mainGroup = new NioEventLoopGroup();
		//一个工作线程组
		NioEventLoopGroup subGroup = new NioEventLoopGroup();
		try {
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			serverBootstrap.group(mainGroup, subGroup)
					//设置队列大小
					.option(ChannelOption.SO_BACKLOG, 1024)
					.channel(NioServerSocketChannel.class)
					// 两小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文
					.childOption(ChannelOption.SO_KEEPALIVE, true)
					//添加自定义初始化处理器
					.childHandler(new WsServerInitialzer());
			ChannelFuture channelFuture = serverBootstrap.bind(8082).sync();
			channelFuture.channel().closeFuture().sync();

		}catch (Exception e){
			e.printStackTrace();
		}finally {
			//关闭主线程组
			mainGroup.shutdownGracefully();
			//关闭工作线程组
			subGroup.shutdownGracefully();
		}


	}
}

WsServerInitialzer 初始化
package com.message.websocket;


import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
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;

/**
 * @author kuaiting
 */
public class WsServerInitialzer extends ChannelInitializer<SocketChannel> {
	@Override
	protected void initChannel(SocketChannel ch) throws Exception {

		ChannelPipeline pipeline = ch.pipeline();
		//websocket基于http协议,所以需要http编解码器
		pipeline.addLast(new HttpServerCodec());
		//添加对于读写大数据流的支持
		pipeline.addLast(new ChunkedWriteHandler());
		//对httpMessage进行聚合
		pipeline.addLast(new HttpObjectAggregator(1024*64));

		// ================= 上述是用于支持http协议的 ==============

		//websocket 服务器处理的协议,用于给指定的客户端进行连接访问的路由地址
		//比如处理一些握手动作(ping,pong)
		pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
		//自定义handler
		pipeline.addLast(new ChatHandler());

	}
}

创建信息ChatHandler 处理类
package com.message.websocket;

import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author kuaiting
 */
@Slf4j
public class ChatHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
	private static  ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		log.info("已创建WebSocket链接:{}", ctx.channel().remoteAddress());
	}

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
		String text = msg.text();
		sendAllMessages(ctx,text);
	}

	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
		channels.add(ctx.channel());
	}

	@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
		log.info("断开链接的ID", ctx.channel().id().asLongText());

	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.channel().closeFuture();
	}


	//给每个人发送消息,除发消息人外
	private void sendAllMessages(ChannelHandlerContext ctx, String msg) {
		for (Channel channel : channels) {
			if (!channel.id().asLongText().equals(ctx.channel().id().asLongText())) {
				channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(msg)));
			}
		}
	}

	//给每个人发送消息,除发消息人外
	private void sendMessages(String msg) {
		for (Channel channel : channels) {
			channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(msg)));
		}
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

缘不易

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

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

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

打赏作者

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

抵扣说明:

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

余额充值