基于websocket的netty demo

前面2文

基于http的netty demo

基于socket的netty demo

讲了netty在http和socket的使用,下面讲讲netty如何使用websocket

websocket是html5提出来的一个东西,功能很强大,可以支持长连接,实现服务器向客户端的通信,这里不做过多的介绍,只说说netty如何使用websocket作为协议来通信

这里采用表单提交的时候,使用websocket的方式提交,具体请看代码:

ps:我这里的代码架构是这样的

服务器代码:

package com.bill.websocketdemo;


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;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class WebSocketServer {

    public static void main(String[] args) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try{
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).
                    handler(new LoggingHandler(LogLevel.INFO)).childHandler(new WebSocketChannelInitializer());

            ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
package com.bill.websocketdemo;


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

public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception {
        System.out.println("收到消息:" + textWebSocketFrame.text());
        channelHandlerContext.channel().writeAndFlush(new TextWebSocketFrame("服务器随机数:" + Math.random()));
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println("handlerAdded!!!");
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("handlerRemoved!!!");
    }
}
package com.bill.websocketdemo;


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;

public class WebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        ChannelPipeline pipeline = socketChannel.pipeline();

        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(new ChunkedWriteHandler());
        pipeline.addLast(new HttpObjectAggregator(8192));
        pipeline.addLast(new WebSocketServerProtocolHandler("/hello"));

        pipeline.addLast(new WebSocketHandler());

    }
}

HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>websocket</title>
</head>
<body>

<script type="text/javascript">
    var socket;
    if(window.WebSocket) {
        socket = new WebSocket("ws://localhost:8899/hello");
        socket.onmessage = function (event) {
            var ta = document.getElementById("resp");
            ta.value = ta.value + "\n" + event.data;
        }
        
        socket.onopen = function (event) {
            var ta = document.getElementById("resp");
            ta.value = "连接接开启!";
        }
        
        socket.onclose = function (event) {
            var ta = document.getElementById("resp");
            ta.value = ta.value + "\n" + "连接关闭!";
        }
    } else {
        alert("浏览器不支持websocket");
    }

    function send(msg) {
        if(!window.WebSocket) {
            return;
        }

        if(socket.readyState == WebSocket.OPEN) {
            socket.send(msg);
        } else {
            alert("连接未开启!")
        }
    }

</script>

<form onsubmit="return false;">

    <textarea id="message" style="width: 400px; height: 200px"></textarea>
    <input type="button" value="发送数据" onclick="send(this.form.message.value)">

    <h3> 服务器输出:</h3>

    <textarea id="resp" style="width: 400px; height: 300px"></textarea>
    <input type="button" onclick="javascript: document.getElementById('resp').value = ''" value="清空内容">
</form>


</body>
</html>

请注意:

运行的时候,先执行WebSocketServer的main方法,然后再run src/main/webapp/test.html ,这样 idea会自动启动一个页面

启动完服务器后,会在浏览器上看到页面:

服务器出现信息:

发送数据:

服务器:

完整代码下载:

https://download.csdn.net/download/mweibiao/10551574

猜你喜欢

转载自blog.csdn.net/mweibiao/article/details/81119539