Netty学习笔记_12(WebSocket与长连接)

15 篇文章 0 订阅

Http短连接和长连接

  • Http短连接即TCP短连接,即客户端和服务器通过“三次握手”建立连接后,进行一次Http操作以后,便断开连接。因此,浏览器每打开一个web资源,便创建了一个新的http会话;
  • Http长连接即TCP长连接,即客户端和服务器建立连接后保持一定的时间,即使用户在进行某次操作后将浏览器(或客户端)关闭,但只要在保持时间内又一次访问该服务器,则默认使用已经创建好的连接;
  • Http1.0默认支持短连接,Http1.1默认支持长连接。

Http连接无状态

  • Http协议无状态是指协议对于事务处理没有记忆性,即某一次打开一个服务器的网页和上一次打开这个服务器的网页之间没有关系。

WebSocket简介

  • WebSocket是一种可以在单个TCP连接上实现全双工通信的通信协议,http协议只能实现客户端请求,服务端响应的单向通信,而webSocket则可以实现服务端主动向客户端推送消息;
  • WebSocket复用了Http的握手通道,客户端和服务器的数据交换则遵照升级后的协议进行:WebSocket相关的业务处理器可以将http协议升级为ws协议,其核心功能之一为保持稳定的长连接

WebSocket长连接实例

要求:

  • 实现基于webSocket的长连接全双工交互
  • 改变http协议多次请求的约束,实现长连接,服务器可以发送消息给浏览器
  • 客户端和服务器会相互感知。若服务器关闭,客户端会感知;同样客户端关闭,服务器也会感知。

实现:

服务器端(MyServer.java)

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
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.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

public class MyServer {
    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try{
            ServerBootstrap serverBootstrap = new ServerBootstrap();

            serverBootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {

                            ChannelPipeline pipeline = ch.pipeline();

                            //因为基于http协议,故使用http的编解码器
                            pipeline.addLast(new HttpServerCodec());
                            //过程中以块的方式写,添加 ChunkedWriteHandler 处理器
                            pipeline.addLast(new ChunkedWriteHandler());
                            /**
                             * 说明
                             * 1、http数据在传输过程中是分段的,HttpObjectAggregator 可以将多个数据段整合起来
                             * 2、因此,当浏览器发送大量数据时,就会发出多次http请求
                             * */
                            pipeline.addLast(new HttpObjectAggregator(8192));
                            /**
                             * 说明
                             * 1、对于 WebSocket,它的数据以 帧(Frame)的形式传递
                             * 2、可以看到 WebSocketFrame 下面有6个子类
                             * 3、浏览器请求时 ws://localhost:7000/xxx 表示请求的uri
                             * 4、WebSocketServerProtocolHandler 会把 http 协议升级为ws协议
                             *      即保持长连接----------核心功能
                             * 5、如何升级——通过状态玛切换101
                             */
                            pipeline.addLast(new WebSocketServerProtocolHandler("/hello"));

                            //自定义的 handler 处理业务逻辑
                            pipeline.addLast(new MyTextWebSocketFrameHandler());
                        }
                    });
            ChannelFuture channelFuture = serverBootstrap.bind(7000).sync();
            channelFuture.channel().closeFuture().sync();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

服务器业务处理器(MyTextWebSocketFrameHandler.java)

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.time.LocalDateTime;


//TextWebSocketFrame表示一个文本帧
public class MyTextWebSocketFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        System.out.println("服务器收到消息"+msg.text());

        //回复浏览器,当前时间+消息回显
        ctx.channel().writeAndFlush(new TextWebSocketFrame("服务器时间 "+ LocalDateTime.now() + " "+msg.text()));
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        //id 表示唯一的值,asLongText是唯一的
        System.out.println("handlerAdded 被调用了"+ctx.channel().id().asLongText());
        System.out.println("handlerAdded 被调用了"+ctx.channel().id().asShortText());
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {

        System.out.println("handlerRemoved 被调用" + ctx.channel().id().asLongText());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("异常发生:"+cause.getMessage());
        ctx.close();
    }
}

页面(hello.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    //判断当前浏览器是否支持webSocket编程
    if (window.WebSocket){
        //go on
        socket = new WebSocket("ws://localhost:7000/hello");
        //相当于channelRead0,收到服务器端回送的消息
        socket.onmessage = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + ev.data;
        }
        //相当于连接开启
        socket.onopen = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = "连接开启";
        }

        socket.onclose = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n"+"连接关闭";
        }
    }else {
        alert("当前浏览器不支持webSocket")
    }
    //发送消息到服务器
    function send(message) {
        if (!window.socket){  //先判断socket是否创建好了
            return;
        }
        if (socket.readyState == WebSocket.OPEN){
            //通过socket发送消息
            socket.send(message);
        }else {
            alert("连接没有开启")
        }
    }
</script>
    <form onsubmit = "return false">
        <textarea name="message" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="发送消息" onclick="send(this.form.message.value)">
        <textarea id="responseText" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
    </form>

</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值