websocket+netty实时视频弹幕交互功能(Java版)

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

发送
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值