大家好,欢迎来到IT知识分享网。
WebSocket协议开发
1. Http协议弊端
- 半双工协议:同一时刻,只有一个方向上的数据传送(客户端 –> 服务端 或者 服务端 –> 客户端)
- 消息冗长繁琐
- 针对服务器推送的黑客攻击。例如长时间轮询,比较新的技术Comet,使用了Ajax,这种技术会大量消耗服务器带宽和资源
2. WebSocket入门
2.1 特点
- 单一的TCP连接,采用全双工模式通信
- 对代理、防火墙和路由器透明
- 无头部信息、Cookie和身份验证
- 无安全开销
- 通过“ping/pong”帧保持链路激活
- 服务器可以主动传递消息给客户端,不在需要客户端轮询
2.2 请求消息
-
Upgrade:表示将http协议升级到WebSocket协议
-
Sec-WebSocket-Key:随机字符串
2.3 应答消息
- Sec-WebSocket-Accept: 将WebSocket-Key加上一个魔幻字符串,然后使用SHA-1加密,进行BASE-64编码
2.4 请求关闭
正常情况下,应该由服务器先关闭。异常情况下(一个合理时间周期后没有收到服务器的TCP close),客户端可以发起TCP Close。当服务器被指示关闭时,会立即发起关闭WebSocket TCP close操作;客户端应该等待服务器的TCP Close。WebSocket的握手关闭消息带有一个状态码核可选的关闭愿意,必须按照协议要求发送一个Close控制帧,对端收到后需要主动关闭WebSocket连接。
3. 时间服务器
- 绑定端口,创建netty服务端
- http-codec:添加Http请求和响应的编码和解码器
- aggregator:合并http请求
- http-chunked:添加大文件传输器
- handler:自定义处理器
public void bind(int port) {
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
NioEventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap
.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
//将请求和应答消息解码或者编码成http消息
.addLast("http-codec", new HttpServerCodec())
.addLast("aggregator", new HttpObjectAggregator(65536))
.addLast("http-chunked", new ChunkedWriteHandler())
.addLast("handler", new TimeWebSocketServerHandler());
}
});
Channel channelFuture = serverBootstrap.bind(port).sync().channel();
System.out.println("Web Socket Server started at port :" + port + ".");
System.out.println("open you browser and navigate to http://localhost:" + port + "/");
channelFuture.closeFuture().sync();
} catch (InterruptedException e) {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
3.1 自定义处理器
public static class TimeWebSocketServerHandler extends SimpleChannelInboundHandler<Object> {
private WebSocketServerHandshaker webSocketServerHandshaker;
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof FullHttpRequest) {
this.handleHttpRequest(ctx, (FullHttpRequest) msg);
} else {
this.handleWebSocketRequest(ctx, (WebSocketFrame) msg);
}
}
}
handleHttpRequest()
- 判断http请求有没有解码成功,或者协议中没有 Upgrade = websocket 值
- websocket握手工厂创建 握手协议,如果不支持返回错误码
- 握手时,就会把 websocket相关的编解码器动态的添加到管道中,后续使用websocket相关对象可以直接获取
/** * 处理http请求 * @param ctx * @param fullHttpRequest */
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest fullHttpRequest) {
//http解码失败,并且协议并不是升级为websocket
if (!fullHttpRequest.decoderResult().isSuccess() ||
(!"websocket".equals(fullHttpRequest.headers().get("Upgrade")))) {
sendHttpResponse(ctx, fullHttpRequest, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.BAD_REQUEST));
return;
}
WebSocketServerHandshakerFactory webSocketServerHandshakerFactory
= new WebSocketServerHandshakerFactory("ws://localhost:8080/websocket", null, false);
webSocketServerHandshaker = webSocketServerHandshakerFactory.newHandshaker(fullHttpRequest);
if (Objects.isNull(webSocketServerHandshaker)) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
//创建握手时,就会将websocket的编码解码添加到管道中
webSocketServerHandshaker.handshake(ctx.channel(), fullHttpRequest);
}
}
handleWebSocketRequest()
- 判断各种类型的WebSocketFrame请求,根据类型操作
private void handleWebSocketRequest(ChannelHandlerContext ctx, WebSocketFrame webSocketFrame) {
//判断链路是否是关闭的指令
if (webSocketFrame instanceof CloseWebSocketFrame) {
webSocketServerHandshaker.close(ctx.channel(), (CloseWebSocketFrame) webSocketFrame.retain());
return;
}
if (webSocketFrame instanceof PingWebSocketFrame) {
ctx.channel().write(new PongWebSocketFrame(webSocketFrame.content().retain()));
return;
}
//只支持文本消息,不支持二进制消息
if (!(webSocketFrame instanceof TextWebSocketFrame)) {
throw new UnsupportedOperationException(String.format("%s frame types not supported", webSocketFrame.getClass().getName()));
}
//返回应答消息
String text = ((TextWebSocketFrame) webSocketFrame).text();
ctx.channel().writeAndFlush(new TextWebSocketFrame(text + ", 欢迎使用netty websocket服务,现在是北京时间:"+ new Date().toString()));
}
sendHttpResponse()
- 判断状态,写入返回值数据
- 判断请求是否是长连接,如果不是,添加关闭的监听器
private static void sendHttpResponse(ChannelHandlerContext ctx,
FullHttpRequest request,
FullHttpResponse response) {
if (response.status().code() != 200) {
ByteBuf byteBuf = Unpooled.copiedBuffer(response.status().toString(), CharsetUtil.UTF_8);
response.content().writeBytes(byteBuf);
byteBuf.release();
HttpUtil.setContentLength(response, response.content().readableBytes());
}
ChannelFuture future = ctx.channel().writeAndFlush(response);
if (!HttpUtil.isKeepAlive(request) || response.status().code() != 200) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
4. JS页面代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Netty WebSocket 时间服务器</title>
</head>
<body>
<script> var socket; if (!window.WebSocket) {
window.WebSocket = window.MozWebSocket; } if (window.WebSocket) {
socket = new WebSocket("ws://localhost:8080/websocket"); socket.onmessage = function (event) {
var ta = document.getElementById('responseText'); ta.value = ""; ta.value = event.data; } socket.onopen = function (event) {
var ta = document.getElementById('responseText'); ta.value = "打开WebSocket服务正常,浏览器支持WebSocket" } socket.onclose = function (event) {
var ta = document.getElementById('responseText'); ta.value = "" ta.value = "WebSocket关闭" } } else {
alert("抱歉您的浏览器不支持WebSocket协议"); } function send(message) {
if (!window.WebSocket) {
return; } if (socket.readyState == WebSocket.OPEN) {
socket.send(message) } else {
alert("WebSocket连接没有建立成功"); } } </script>
<form onsubmit="return false;">
<input type="text" name="message" value="Netty 最佳实践">
<br><br>
<input type="button" value="发送 WebSocket请求消息" onclick="send(this.form.message.value)"/>
<hr color="blue"/>
<h3>服务端返回应答消息</h3>
<textarea id="responseText" style="width: 500px;height: 300px">
</textarea>
</form>
</body>
</html>
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/21169.html