使用netty建立websocket进行群聊
来源:互联网 发布:ubuntu怎么读音标 编辑:程序博客网 时间:2024/05/18 12:03
WebSocket 简介:
WebSocket 规范定义了一种 API,可在网络浏览器和服务器之间建立“套接字”连接。简单地说:客户端和服务器之间存在持久的连接,而且双方都可以随时开始发送数据。
第一次请求客户端发送的是http请求,请求头中包含websocket相关的信息,服务器端对请求进行验证,所以要添加一些http 的解码编码handler,同时也要添加WebSocket 的handler处理器,如下:
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("decoder", new HttpRequestDecoder()); //用于解析http报文的handler ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536)); //用于将解析出来的数据封装成http对象,httprequest什么的 ch.pipeline().addLast("encoder", new HttpResponseEncoder()); //用于将response编码成httpresponse报文发送 ch.pipeline().addLast("handshake", new WebSocketServerProtocolHandler("", "", true)); //websocket的handler部分定义的,它会自己处理握手等操作 ch.pipeline().addLast("handler", new MyWebSocketServerHandler()); } }
第一次处理的事http 请求,等到建立WebSocket 之后就需要处理WebSocket的请求,如下代码:
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof FullHttpRequest) { handleHttpRequest(ctx, ((FullHttpRequest) msg)); } else if (msg instanceof WebSocketFrame) { handlerWebSocketFrame(ctx, (WebSocketFrame) msg); } }
接下来告诉客户端,接下来的数据按照websocket协议来发送内容了,以及启动websocket服务器在制定的端口上
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) { if (!req.getDecoderResult().isSuccess() || (!"websocket".equals(req.headers().get("Upgrade")))) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST)); return; } WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( "ws://localhost:8888/websocket", null, false); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory .sendUnsupportedVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), req); } }
最后,在读取到消息时websocket类型过后,将消息进行群发
private void handlerWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { // 返回应答消息 String request = ((TextWebSocketFrame) frame).text(); System.out.println("recv:" + request); TextWebSocketFrame tws = new TextWebSocketFrame(new Date().toString() + ctx.channel().localAddress() + ":" + request); // 群发 MyChannelGroup.group.writeAndFlush(tws); }
代码地址
0 0
- 使用netty建立websocket进行群聊
- Netty+WebSocket实现简单群聊
- Websocket Netty protobuf协作使用
- 使用Netty进行文件传输
- 使用Netty进行文件传输
- netty websocket
- netty websocket
- netty websocket
- 使用Netty框架开发websocket即时通讯
- [C#] 使用WebSocket进行通讯
- Netty笔记:使用WebSocket协议开发聊天系统
- Netty使用websocket协议实现汽车行驶轨迹追踪demo
- Netty笔记:使用WebSocket协议开发聊天系统
- WebSocket+Netty实现聊天室
- netty websocket chatroom
- Netty之WebSocket开发
- Netty的websocket Demo
- WebSocket初体验(Netty)
- 轻量级iOS安全框架:SSKeyChain
- 深入理解Linux内核 Chapter1知识点
- 自定义大头针
- zabbix使用自带模板监控mysql
- 安卓应用内重启
- 使用netty建立websocket进行群聊
- HDU5610——暴力枚举(可减少遍历次数)
- ASP.NET MVC4使用Html.DropDownList和Html.BeginForm
- mysql start slave 的until用法
- the usage(head file and other) of sprintf(), fprintf()
- SQL DISTINCT 多字段查询用法
- QT tableview用法
- 不同场景下 MySQL 的迁移方案
- 欢迎使用CSDN-markdown编辑器