Upgrade:表示将http协议升级到WebSocket协议
Sec-WebSocket-Key:随机字符串
正常情况下,应该由服务器先关闭。异常情况下(一个合理时间周期后没有收到服务器的TCP close),客户端可以发起TCP Close。当服务器被指示关闭时,会立即发起关闭WebSocket TCP close操作;客户端应该等待服务器的TCP Close。WebSocket的握手关闭消息带有一个状态码核可选的关闭愿意,必须按照协议要求发送一个Close控制帧,对端收到后需要主动关闭WebSocket连接。
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();
}
}
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);
}
}
}
/**
* 处理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);
}
}
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()));
}
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);
}
}
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>