新聞中心
圖片來自 包圖網(wǎng)

建華網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。創(chuàng)新互聯(lián)公司于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
今天筆者就抽空做了一個實時視頻彈幕交互的小功能,不得不說這樣的形式為看視頻看直播,講義 PPT,抽獎等形式增加了許多樂趣。
技術(shù)選型
①Netty
官方對于 Netty 的描述:
- https://netty.io/
主要關(guān)鍵詞描述:Netty 是異步事件驅(qū)動網(wǎng)絡(luò)框架,可做各種協(xié)議服務(wù)端,并且支持了 FTP,SMTP,HTTP 等很多協(xié)議,并且性能,穩(wěn)定性,靈活性都很棒。
可以看到 Netty 整體架構(gòu)上分了三個部分:
- 以零拷貝,一致性接口,擴(kuò)展事件模型的底層核心。
- Socket,Datagram,Pipe,Http Tunnel 作為傳輸媒介。
- 傳輸支持的各種協(xié)議,HTTP&WebSocket,SSL,大文件,zlib/gzip 壓縮,文本,二進(jìn)制,Google Protobuf 等各種各種的傳輸形式。
②WebSocket
WebSocket 是一種在單個 TCP 連接上進(jìn)行全雙工通信的協(xié)議。WebSocket 通信協(xié)議于 2011 年被 IETF 定為標(biāo)準(zhǔn) RFC 6455,并由 RFC7936 補充規(guī)范。
WebSocket API 也被 W3C 定為標(biāo)準(zhǔn)。WebSocket 使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡單,允許服務(wù)端主動向客戶端推送數(shù)據(jù)。
在 WebSocket API 中,瀏覽器和服務(wù)器只需要完成一次握手,兩者之間就直接可以創(chuàng)建持久性的連接,并進(jìn)行雙向數(shù)據(jù)傳輸。
為什么做這樣的技術(shù)選型:
- 由上述可知,實時直播交互作為互動式是一個雙向數(shù)據(jù)傳輸過程。所以使用 WebSocket。
- Netty 本身支持了 WebSocket 協(xié)議的實現(xiàn),讓實現(xiàn)更加簡單方便。
實現(xiàn)思路
①服務(wù)架構(gòu)
整體架構(gòu)是所有客戶端都和我的服務(wù)端開啟一個雙向通道的架構(gòu)。
②傳輸流程
如下圖:
實現(xiàn)效果
先看看效果吧,是不是 perfect,接下來就來看具體代碼是怎么實現(xiàn)的吧。
視頻直播彈幕示例
代碼實現(xiàn)
①項目結(jié)構(gòu)
一個 maven 項目,將代碼放一個包下就行。
②Java 服務(wù)端
Java 服務(wù)端代碼,總共三個類,Server,Initailizer 和 Handler。
先做一個 netty nio 的服務(wù)端:一個 nio 的服務(wù),開啟一個 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(c)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) {
- }
- }
服務(wù)端的具體處理邏輯:
- 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(c)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(c)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();
- // 發(fā)生異常之后關(guān)閉連接(關(guān)閉channel),隨后從ChannelGroup中移除
- ctx.channel().close();
- channels.remove(ctx.channel());
- }
- }
③網(wǎng)頁客戶端實現(xiàn)
代碼如下:
Netty視頻彈幕實現(xiàn) Author:Binhao Liu 發(fā)送
這樣一個實時的視頻彈幕功能就完成啦,是不是很簡單,各位小伙伴快來試試吧。
小結(jié)
這個還是很簡單,筆者寫這個的時候一會兒就寫完了。不過這也得益于筆者很久以前就寫過 Netty 的服務(wù),對于 HTTP,TCP 之類協(xié)議也比較熟悉。
只有前端會有些難度,問下度娘,也很快能做完,在此分享出來與諸君分享,有問題可找筆者交流。
作者:興趣使然的程序猿
編輯:陶家龍
出處:http://adkx.net/w71wf
分享文章:老板怒吼:今晚整一個B站彈幕交互功能
鏈接地址:http://www.dlmjj.cn/article/dhcoggc.html


咨詢
建站咨詢
