程序员Java 杂谈

netty 对websocket 支持

2017-08-26  本文已影响285人  持续进步者

WebSocket

WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工通信————允许服务器主动发送信息给客户端。

WebSocket解决了那些问题

netty 服务器端

public class MyServer {
    public static void main(String ...arg) throws Exception{

        // 负责接收客户端连接
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        // 负责处理连接
        NioEventLoopGroup wokerGroup = new NioEventLoopGroup();

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

            ChannelFuture channelFuture = serverBootstrap.bind(9999).sync();
            channelFuture.channel().closeFuture().sync();


        } finally {
            bossGroup.shutdownGracefully();
            wokerGroup.shutdownGracefully();
        }

    }
}

public class WebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(new ChunkedWriteHandler());
        pipeline.addLast(new HttpObjectAggregator(8192));
        //websocket 请求路径
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));

        pipeline.addLast(new TextWebSocketFrameHandler());

    }
}


public class TextWebSocketFrameHandler 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()));

    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        //每个channel 都有一个唯一的id
        System.out.println("handlerAdded: "+ctx.channel().id().asLongText());
    }

    @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 {
        cause.printStackTrace();
        ctx.close();
    }
}



WebSocket js 使用

var ws = new WebSocket(url,name);

ws.send(msg);

ws.onmessage = (function(){...})();

ws.onerror = (function(){...})();

ws.close();

客户端端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>websoket 客户端</title>
</head>
<body>
<form onsubmit="return false">
    <textarea name="message" style="width:400px;height: 200px"></textarea>
    <input type="button" value="发送数据" onclick="send(this.form.message.value)">

    <h3>服务器输出:</h3>
    <textarea id="responseText" style="width: 400px; height: 300px"></textarea>

    <input type="button" onclick="javascript:document.getElementById('responseText').value=''" value="清空内容">
</form>

</body>
<script type="text/javascript">
    var socket;
    if (window.WebSocket) {
        socket = new WebSocket("ws://127.0.0.1:9999/ws")
        var ta = document.getElementById("responseText");

        socket.onmessage = function (event) {
            ta.value = ta.value + "\n" + event.data;
        }

        socket.onopen = function (event) {
            ta.value = "连接开启"
        }

        socket.onclose = function (event) {
            ta.value = ta.value + "\n" + "连接关闭";
        }

    } else {
        alert("浏览器不支持websocket")
    }

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

        if(socket.readyState == WebSocket.OPEN){
            socket.send(message)
        }else{
            alert("连接尚未成功")
        }

    }

</script>
</html>
上一篇下一篇

猜你喜欢

热点阅读