赞
踩
WebSocket 是 HTML5 的一种协议,它可以实现浏览器与服务器全双工通信。Websocket 是应用层第七层上的一个应用层协议,它必须依赖 HTTP 协议进行一次握手,握手成功后,数据就可以直接使用 TCP 通道传输,之后就与 HTTP 无关。简单地讲,开始握手需要借助 HTTP 请求完成,之后就会升级为 WebSocket 协议。
本篇开发一个基于 WebSocket 协议的长连接通信。
采用 Netty 作为长连接的服务端,使用浏览器作为客户端,二者交互图如下。
- package netty.websocket;
-
- 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;
-
- public class MyNettyServerTest {
- public static void main(String[] args) {
- EventLoopGroup bossGroup = new NioEventLoopGroup();
- EventLoopGroup workerGroup = new NioEventLoopGroup();
- try {
- // ServerBootstrap:服务端启动时的初始化操作
- ServerBootstrap serverBootstrap = new ServerBootstrap();
- // 将 bossGroup 和 workerGroup 注册到服务端的 Channel上,并注册一个服务端的初始化器 NettyServerInitializer
- // 该初始化器中的 initChannel() 方法,会在连接被注册后立刻执行;最后将端口号绑定到 8888
- ChannelFuture channelFuture = serverBootstrap
- .group(bossGroup, workerGroup)
- .channel(NioServerSocketChannel.class)
- .childHandler(new MyNettyServerInitializer())
- .bind(8888).sync();
- channelFuture.channel().closeFuture().sync();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- bossGroup.shutdownGracefully();
- workerGroup.shutdownGracefully();
- }
- }
- }
- package netty.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;
-
- public class MyNettyServerInitializer extends ChannelInitializer<SocketChannel> {
- protected void initChannel(SocketChannel sc) throws Exception {
- ChannelPipeline pipeline = sc.pipeline();
- pipeline.addLast("HttpServerCodec", new HttpServerCodec());
- /*
- HttpObjectAggregator:把多个 HttpMessage 组装成一个完整的 Http 请求(FullHttpRequest)或者响应(FullHttpResponse)。
- */
- pipeline.addLast("HttpObjectAggregator", new HttpObjectAggregator(4096));
- // 处理 websocket 的 netty 处理器,可以通过构造方法绑定 webSocket 的服务端地址
- pipeline.addLast("WebSocketServerProtocolHandler", new WebSocketServerProtocolHandler("/myWebSocket"));
- // 自定义处理器
- pipeline.addLast("MyNettyServerHandler", new MyNettyServerHandler());
- }
- }
- package netty.websocket;
-
-
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.channel.SimpleChannelInboundHandler;
- import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
-
- // 泛型 TextWebSocketFrame:WebSocket 处理的处理文本类型
- public class MyNettyServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
- // 接收 WebSocket 客户端发送来的数据(Websocket以 fram e的形式传递数据)
- @Override
- protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame frame) {
- System.out.println("Server收到消息:" + frame.text());
- // 向 WebSocket 客户端发送数据
- ctx.channel().writeAndFlush(new TextWebSocketFrame("helo client..."));
- }
-
- @Override
- public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
- System.out.println("客户端加入:id=" + ctx.channel().id());
- }
-
- @Override
- public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
- System.out.println("客户端离开:id=" + ctx.channel().id());
- }
- }
-
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- <script type="text/javascript">
- var webSocket = new WebSocket("ws://localhost:8888/myWebSocket");
- //检测WebSocket服务端是否开启
- webSocket.onopen = function (event) {
- document.getElementById("tip").innerText = "连接开启";
- }
- //检测WebSocket服务端是否关闭
- webSocket.onclose = function (event) {
- document.getElementById("tip").innerText = "连接关闭";
- }
- //接收WebSocket服务端发送来的数据(数据保存在event对象中)
- webSocket.onmessage = function (event) {
- document.getElementById("tip").innerText = "接收到的服务端消息:" + event.data;
- }
-
- function sendMessage(msg) {
- if (webSocket.readyState == WebSocket.OPEN) {
- //向WebSocket服务端发送数据
- webSocket.send(msg);
- }
- }
- </script>
- </head>
-
- <body onload="init()">
- <form>
- <textarea name="message"></textarea> <br/>
- <input type="button" onclick="sendMessage(this.form.message.value)" value="向服务端发送WebSocket数据"/>
- </form>
- <div id="tip"></div>
- </body>
- </html>
客户端加入:id=0ecbc18d
Server收到消息:hello,我是客户端
客户端离开:id=0ecbc18d
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。