【138期】手撸 websocket + netty 实时视频弹幕交互功能(Java版附源码)

1.2 WebSocket


WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket通信协议于2011年被IETF定为标准RFC 6455,并由RFC7936补充规范。WebSocket API也被W3C定为标准。

WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。2

1.3 为什么做这样的技术选型。


a. 由上述可知,实时直播交互作为互动式是一个双向数据传输过程。所以使用webSocket。

b. netty本身支持了webSocket协议的实现,让实现更加简单方便。

2 、实现思路


2.1 服务架构

整体架构是所有客户端都和我的服务端开启一个双向通道的架构。

82ea01e9f8557bc4c81461f822446f27.png

2.2 传输流程

77de6fbf138974acf3edc153387d9ed2.png

3 、实现效果


3.1 视频展示

先看看效果吧,是不是perfect,接下来就来看具体代码是怎么实现的吧。

视频直播弹幕示例

3ef6d703e922ccb88de2d591908d354c.png

4、 代码实现


4.1 项目结构

一个maven项目,将代码放一个包下就行。522d9101efb1386144119fd8bfc9c8f9.png

4.2 Java服务端

Java服务端代码,总共三个类,Server,Initailizer和 Handler。

4.2.1 先做一个netty nio的服务端:

一个nio的服务,开启一个tcp端口。

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.ChannelFuture;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

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

/**

* Copyright©lbhbinhao@163.com

* @author liubinhao

* @date 2021/1/14

* ++++ ______                           ______             ______

* +++/     /|                         /     /|           /     /|

* +//  |                       //  |         /_____/  |

* |     |   |                      |     |   |        |     |   |

* |     |   |                      |     |   |________|     |   |

* |     |   |                      |     |  /         |     |   |

* |     |   |                      |     |/___________|     |   |

* |     |   |___________________   |     |____________|     |   |

* |     |  /                  / |  |     |   |        |     |   |

* |     |/ _________________/  /   |     |  /         |     |  /

* |_________________________|/b    ||/           ||/

*/

public enum BulletChatServer {

/**

* Server instance

*/

SERVER;

private BulletChatServer(){

EventLoopGroup mainGroup = new NioEventLoopGroup();

EventLoopGroup subGroup  = new NioEventLoopGroup();

ServerBootstrap server = new ServerBootstrap();

server.group(mainGroup,subGroup)

.channel(NioServerSocketChannel.class)

.childHandler(new BulletChatInitializer());

ChannelFuture future = server.bind(9123);

}

public static void main(String[] args) {

}

}

4.2.2 服务端的具体处理逻辑

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;

import io.netty.handler.timeout.IdleStateHandler;

/**

* Copyright©lbhbinhao@163.com

* @author liubinhao

* @date 2021/1/14

* ++++ ______                           ______             ______

* +++/     /|                         /     /|           /     /|

* +//  |                       //  |         /_____/  |

* |     |   |                      |     |   |        |     |   |

* |     |   |                      |     |   |________|     |   |

* |     |   |                      |     |  /         |     |   |

* |     |   |                      |     |/___________|     |   |

* |     |   |___________________   |     |____________|     |   |

* |     |  /                  / |  |     |   |        |     |   |

* |     |/ _________________/  /   |     |  /         |     |  /

* |_________________________|/b    ||/           ||/

*/

public class BulletChatInitializer extends ChannelInitializer {

@Override

protected void initChannel(SocketChannel ch) throws Exception {

ChannelPipeline pipeline = ch.pipeline();

pipeline.addLast(new HttpServerCodec());

pipeline.addLast(new ChunkedWriteHandler());

pipeline.addLast(new HttpObjectAggregator(1024*64));

pipeline.addLast(new IdleStateHandler(8, 10, 12));

pipeline.addLast(new WebSocketServerProtocolHandler(“/lbh”));

pipeline.addLast(new BulletChatHandler());

}

}

[^

后台处理逻辑,接受到消息,写出到所有的客户端:

import io.netty.channel.Channel;

import io.netty.channel.ChannelHandler;

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.EventExecutorGroup;

import io.netty.util.concurrent.GlobalEventExecutor;

/**

* Copyright©lbhbinhao@163.com

* @author liubinhao

* @date 2021/1/14

* ++++ ______                           ______             ______

* +++/     /|                         /     /|           /     /|

* +//  |                       //  |         /_____/  |

* |     |   |                      |     |   |        |     |   |

* |     |   |                      |     |   |________|     |   |

* |     |   |                      |     |  /         |     |   |

* |     |   |                      |     |/___________|     |   |

* |     |   |___________________   |     |____________|     |   |

* |     |  /                  / |  |     |   |        |     |   |

* |     |/ _________________/  /   |     |  /         |     |  /

* |_________________________|/b    ||/           ||/

*/

public class BulletChatHandler  extends SimpleChannelInboundHandler {

// 用于记录和管理所有客户端的channel

public static ChannelGroup channels =

new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

@Override

protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {

// 获取客户端传输过来的消息

String content = msg.text();

System.err.println(“收到消息:”+ content);

channels.writeAndFlush(new TextWebSocketFrame(content));

System.err.println(“写出消息完成:”+content);

}

@Override

public void handlerAdded(ChannelHandlerContext ctx) throws Exception {

channels.add(ctx.channel());

}

@Override

public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {

String channelId = ctx.channel().id().asShortText();

System.out.println(“客户端被移除,channelId为:” + channelId);

channels.remove(ctx.channel());

}

@Override

public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

cause.printStackTrace();

// 发生异常之后关闭连接(关闭channel),随后从ChannelGroup中移除

ctx.channel().close();

channels.remove(ctx.channel());

}

}

4.3 网页客户端实现

Netty视频弹幕实现 Author:Binhao Liu
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值