Netty进阶 -- WebSocket长连接开发

炒沙作縻终不饱,缕冰文章费工巧。这篇文章主要讲述Netty进阶 -- WebSocket长连接开发相关的知识,希望能为你提供帮助。
一、WebSocket简介百度百科WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket通信协议于2011年被IETF定为标准RFC 6455,并由RFC7936补充规范。WebSocket API也被W3C定为标准。
WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。
二、有了HTTP为什么还需要WebSocket?因为HTTP有一个缺陷,就是只能从客户端发起请求,无法从服务器发起请求,所以诞生了WebSocket请求

Netty进阶 -- WebSocket长连接开发

文章图片

?
以上为WebSocket请求URI
二、需求说明Http协议是无状态的,浏览器和服务器间的请求响应一次,下一次会重新创建连接
  1. 要求:实现基于WebSocket的长连接的全双工的交互
  2. 改变Http协议多次请求的约束,实现长连接,服务器可以发送消息给浏览器
  3. 客户端浏览器和服务器会相互感知,比如服务器关闭了,浏览器会感知,同样浏览器关闭了,服务器也可感知
三、需求分析?服务器与浏览器相互感知当客户端浏览器上线后,服务器可以感知到并提示用户上线,继承SimpleChannelInboundHandler类并重写handlerAdded
即可感知用户上线
?服务器发送消息给客户端当连接成功后,服务器重写channelRead0方法并通过全局上下文ctx.channel().writeAndFlush方法发送消息
注意:这里不可以直接写字符串发送,要封装成 TextWebSocketFrame
?客户端发送消息给服务器客户端发送消息给服务器通过WebSocket的send方法发送即可
四、效果图
Netty进阶 -- WebSocket长连接开发

文章图片

五、核心源码??前端源码WebSocket.html
< !DOCTYPE html>
< html lang="en">
< head>
< meta charset="UTF-8">
< meta http-equiv="X-UA-Compatible" content="IE=edge">
< meta name="viewport" content="width=device-width, initial-scale=1.0">
< title> Document< /title>
< /head>
< script>
var socket;
if (window.WebSocket)
socket = new WebSocket("ws://localhost:7000/hello2");
socket.onmessage = function(ev)
var rt = document.getElementById("responseText");
rt.value = https://www.songbingjia.com/android/rt.value +"\\n" + ev.data;
document.getElementById("msg").value = https://www.songbingjia.com/android/;


//连接开启
socket.onopen = function(ev)
var rt = document.getElementById("responseText");
rt.value = https://www.songbingjia.com/android/rt.value +"已开启连接...";

//连接关闭
socket.onclose = function(ev)
var rt = document.getElementById("responseText");
rt.value = https://www.songbingjia.com/android/rt.value +"已关闭连接...";

else
alert("您的浏览器不支持WebSocket!");


//发送消息到浏览器
function send(msg)
//判断websocket是否创建好
if (!socket)
return;


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


< /script>
< body>
< form onsubmit="return false">
< div class="msgDiv">
< h2> 客户端< /h2>
< textarea name="msg" id="msg" style="width: 400px; height: 300px; "> < /textarea> & emsp;
< input type="button" value="https://www.songbingjia.com/android/发送消息" onclick="send(this.form.msg.value)">
< /div>
< div class="msgDiv">
< h2> 服务器内容< /h2>
< textarea id="responseText" style="width: 400px; height: 300px; "> < /textarea> & emsp;
< input type="button" value="https://www.songbingjia.com/android/清空内容" onclick="document.getElementById(responseText).value = "https://www.songbingjia.com/android/>
< /div>
< /form>
< /body>
< /html>

< style>
.msgDiv
float: left;
margin: 20px;

< /style>

??后端源码【Netty进阶 -- WebSocket长连接开发】MyWebSocketServer
package com.wanshi.netty.websocket;

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;

public

    推荐阅读