{"id":30596,"date":"2022-01-11T12:36:11","date_gmt":"2022-01-11T04:36:11","guid":{"rendered":"https:\/\/aqzt.com\/30596.html"},"modified":"2022-01-11T12:36:11","modified_gmt":"2022-01-11T04:36:11","slug":"netty-websocket-6","status":"publish","type":"post","link":"https:\/\/aqzt.com\/30596.html","title":{"rendered":"Netty websocket"},"content":{"rendered":"<h2>Network protocols<\/h2>\n<p>WebSocket\u662f\u4e00\u79cd\u9ad8\u7ea7\u7f51\u7edc\u534f\u8bae\uff0c\u65e8\u5728\u63d0\u9ad8Web\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002 \u6211\u4eec\u5c06\u901a\u8fc7\u7f16\u5199\u793a\u4f8b\u5e94\u7528\u7a0b\u5e8f\u6765\u63a2\u7d22Netty\u5bf9\u5b83\u4eec\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p>\u5728\u7b2c12\u7ae0\u4e2d\uff0c\u60a8\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528WebSocket\u5b9e\u73b0\u53cc\u5411\u6570\u636e\u4f20\u8f93\uff0c\u65b9\u6cd5\u662f\u6784\u5efa\u4e00\u4e2a\u804a\u5929\u5ba4\u670d\u52a1\u5668\uff0c\u5176\u4e2d\u591a\u4e2a\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\u53ef\u4ee5\u5b9e\u65f6\u901a\u4fe1\u3002 \u60a8\u8fd8\u5c06\u770b\u5230\u5982\u4f55\u901a\u8fc7\u68c0\u6d4b\u5ba2\u6237\u7aef\u662f\u5426\u652f\u6301\u5b83\uff0c\u4ece\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684HTTP\u5207\u6362\u5230WebSocket\u534f\u8bae\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u5728\u7b2c13\u7ae0\u4e2d\u603b\u7ed3\u7b2c3\u90e8\u5206\uff0c\u7814\u7a76Netty\u5bf9\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae\uff08UDP\uff09\u7684\u652f\u6301\u3002\u5728\u8fd9\u91cc\uff0c\u60a8\u5c06\u6784\u5efa\u4e00\u4e2a\u5e7f\u64ad\u670d\u52a1\u5668\u548c\u76d1\u89c6\u5668\u5ba2\u6237\u7aef\uff0c\u53ef\u4ee5\u9002\u5e94\u8bb8\u591a\u5b9e\u9645\u7528\u9014\u3002<\/p>\n<p>\u672c\u7ae0\u8bb2\u4ecb\u7ecd\uff1a<\/p>\n<ul>\n<li>real-time web \u7684\u6982\u5ff5<\/li>\n<li>WebSocket \u534f\u8bae<\/li>\n<li>\u4f7f\u7528Netty \u521b\u5efa\u4e00\u4e2a\u57fa\u4e8e WebSocket\u3000\u7684\u804a\u5929\u5ba4\u670d\u52a1\u7aef\u7a0b\u5e8f<\/li>\n<\/ul>\n<p>\u5982\u679c\u60a8\u5173\u6ce8\u7f51\u7edc\u6280\u672f\u7684\u6700\u65b0\u53d1\u5c55\uff0c\u60a8\u5f88\u53ef\u80fd\u4f1a\u9047\u5230\u5b9e\u65f6\u7f51\u7edc\u77ed\u8bed\uff0c\u5982\u679c\u60a8\u6709\u5de5\u7a0b\u9886\u57df\u7684\u5b9e\u65f6\u5e94\u7528\u7a0b\u5e8f\u7ecf\u9a8c\uff0c\u60a8\u53ef\u80fd\u4f1a\u5bf9\u8fd9\u4e2a\u672f\u8bed\u7684\u542b\u4e49\u6301\u6000\u7591\u6001\u5ea6\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u9996\u5148\u8981\u6f84\u6e05\u7684\u662f\uff0c\u8fd9\u4e0d\u662f\u6240\u8c13\u7684\u786c\u5b9e\u65f6\u670d\u52a1\u8d28\u91cf\uff08QoS\uff09\uff0c\u5176\u4e2d\u4fdd\u8bc1\u4e86\u5728\u6307\u5b9a\u65f6\u95f4\u95f4\u9694\u5185\u4ea4\u4ed8\u8ba1\u7b97\u7ed3\u679c\u3002 \u4ec5\u4ec5HTTP\u7684\u8bf7\u6c42\/\u54cd\u5e94\u8bbe\u8ba1\u4f7f\u5f97\u8fd9\u4e2a\u95ee\u9898\u975e\u5e38\u4e25\u91cd\uff0c\u56e0\u4e3a\u8fc7\u53bb\u8bbe\u8ba1\u7684\u65b9\u6cd5\u90fd\u6ca1\u6709\u63d0\u4f9b\u4ee4\u4eba\u6ee1\u610f\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u867d\u7136\u5df2\u7ecf\u6709\u4e00\u4e9b\u5173\u4e8e\u6b63\u5f0f\u5b9a\u4e49\u5b9a\u65f6Web\u670d\u52a1\u8bed\u4e49\u7684\u5b66\u672f\u8ba8\u8bba\uff0c\u4f46\u666e\u904d\u63a5\u53d7\u7684\u5b9a\u4e49\u4f3c\u4e4e\u5e76\u672a\u51fa\u73b0\u3002 \u6240\u4ee5\u73b0\u5728\u6211\u4eec\u5c06\u63a5\u53d7\u6765\u81ea\u7ef4\u57fa\u767e\u79d1\u7684\u4ee5\u4e0b\u975e\u6743\u5a01\u6027\u63cf\u8ff0\uff1a<\/p>\n<blockquote>\n<p>The real-time web is a network web using technologies and practices that<br \/>\n<!-- -->enable users to receive information as soon as it is published by its<br \/>\n<!-- -->authors, rather than requiring that they or their software check a source<br \/>\n<!-- -->periodically for updates.<br \/>\n<!-- -->\u5b9e\u65f6\u7f51\u7edc\u662f\u4e00\u79cd\u4f7f\u7528\u6280\u672f\u548c\u5b9e\u8df5\u7684\u7f51\u7edc\u7f51\u7edc\uff0c\u4f7f\u7528\u6237\u80fd\u591f\u5728\u4f5c\u8005\u53d1\u5e03\u4fe1\u606f\u540e\u7acb\u5373\u63a5\u6536\u4fe1\u606f\uff0c\u800c\u4e0d\u662f\u8981\u6c42\u4ed6\u4eec\u6216\u4ed6\u4eec\u7684\u8f6f\u4ef6\u5b9a\u671f\u68c0\u67e5\u4fe1\u606f\u6e90\u4ee5\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<\/blockquote>\n<p>\u7b80\u800c\u8a00\u4e4b\uff0c\u4e00\u4e2a\u6210\u719f\u7684\u5b9e\u65f6\u7f51\u7edc\u53ef\u80fd\u4e0d\u4f1a\u5373\u5c06\u5230\u6765\uff0c\u4f46\u5176\u80cc\u540e\u7684\u60f3\u6cd5\u6b63\u5728\u63a8\u52a8\u5bf9\u51e0\u4e4e\u5373\u65f6\u8bbf\u95ee\u4fe1\u606f\u7684\u4e0d\u65ad\u589e\u957f\u7684\u671f\u671b\u3002 \u6211\u4eec\u5c06\u5728\u672c\u7ae0\u4e2d\u8ba8\u8bba\u7684WebSocket\u534f\u8bae\u662f\u671d\u7740\u8fd9\u4e2a\u65b9\u5411\u7684\u826f\u597d\u652f\u6301\u7684\u6b65\u9aa4\u3002<\/p>\n<h2>12.1 Introducing WebSocket<\/h2>\n<p>WebSocket\u534f\u8bae\u662f\u4ece\u5934\u5f00\u59cb\u8bbe\u8ba1\u7684\uff0c\u65e8\u5728\u4e3aWeb\u4e0a\u7684\u53cc\u5411\u6570\u636e\u4f20\u8f93\u95ee\u9898\u63d0\u4f9b\u5b9e\u7528\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5141\u8bb8\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u968f\u65f6\u4f20\u8f93\u6d88\u606f\uff0c\u4ece\u800c\u8981\u6c42\u5b83\u4eec\u5f02\u6b65\u5904\u7406\u6d88\u606f\u63a5\u6536\u3002 \uff08\u6700\u65b0\u7684\u6d4f\u89c8\u5668\u652f\u6301WebSocket\u4f5c\u4e3aHTML5\u7684\u5ba2\u6237\u7aefAPI\u3002\uff09<\/p>\n<p>Netty\u5bf9WebSocket\u7684\u652f\u6301\u5305\u62ec\u6240\u6709\u6b63\u5728\u4f7f\u7528\u7684\u4e3b\u8981\u5b9e\u73b0\uff0c\u56e0\u6b64\u5728\u60a8\u7684\u4e0b\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u91c7\u7528\u5b83\u975e\u5e38\u7b80\u5355\u3002 \u4e0eNetty\u4e00\u6837\uff0c\u60a8\u53ef\u4ee5\u5b8c\u5168\u4f7f\u7528\u534f\u8bae\uff0c\u800c\u65e0\u9700\u62c5\u5fc3\u5176\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\u3002 \u6211\u4eec\u5c06\u901a\u8fc7\u521b\u5efa\u57fa\u4e8eWebSocket\u7684\u5b9e\u65f6\u804a\u5929\u5e94\u7528\u7a0b\u5e8f\u6765\u8bc1\u660e\u8fd9\u4e00\u70b9\u3002<\/p>\n<h2>12.2 Our example WebSocket application<\/h2>\n<p>\u6211\u4eec\u7684\u793a\u4f8b\u5e94\u7528\u7a0b\u5e8f\u5c06\u901a\u8fc7\u4f7f\u7528WebSocket\u534f\u8bae\u5b9e\u73b0\u57fa\u4e8e\u6d4f\u89c8\u5668\u7684\u804a\u5929\u5e94\u7528\u7a0b\u5e8f\u6765\u6f14\u793a\u5b9e\u65f6\u529f\u80fd\uff0c\u4f8b\u5982\u60a8\u53ef\u80fd\u5728Facebook\u7684\u6587\u672c\u6d88\u606f\u529f\u80fd\u4e2d\u9047\u5230\u8fc7\u3002 \u6211\u4eec\u5c06\u901a\u8fc7\u5141\u8bb8\u591a\u4e2a\u7528\u6237\u540c\u65f6\u76f8\u4e92\u901a\u4fe1\u6765\u8fdb\u4e00\u6b65\u53d1\u5c55\u3002<br \/>\n<!-- -->\u56fe12.1\u8bf4\u660e\u4e86\u5e94\u7528\u7a0b\u5e8f\u903b\u8f91\uff1a<\/p>\n<ol>\n<li>\u4e00\u4e2a\u5ba2\u6237\u7aef\u53d1\u9001\u4e00\u6761\u6d88\u606f\u3002<\/li>\n<li>\u8fd9\u6761\u6d88\u606f\u5e7f\u64ad\u5230\u6240\u6709\u5df2\u7ecf\u5efa\u7acb\u8fde\u63a5\u7684\u5176\u4ed6\u5ba2\u6237\u7aef\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u5c31\u662f\u60a8\u671f\u671b\u804a\u5929\u5ba4\u5de5\u4f5c\u7684\u65b9\u5f0f\uff1a\u6bcf\u4e2a\u4eba\u90fd\u53ef\u4ee5\u4e0e\u5176\u4ed6\u4eba\u4ea4\u8c08\u3002 \u5728\u6211\u4eec\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4ec5\u5b9e\u73b0\u670d\u52a1\u5668\u7aef\uff0c\u5ba2\u6237\u7aef\u662f\u901a\u8fc7\u7f51\u9875\u8bbf\u95ee\u804a\u5929\u5ba4\u7684\u6d4f\u89c8\u5668\u3002 \u6b63\u5982\u60a8\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u9875\u4e2d\u770b\u5230\u7684\u90a3\u6837\uff0cWebSocket\u4f7f\u7f16\u5199\u6b64\u670d\u52a1\u5668\u53d8\u5f97\u7b80\u5355\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aqzt.com\/wp-content\/uploads\/20220223064945-24.png\" alt=\"Figure 12.1 WebSocket application logic\" \/><\/p>\n<h2>12.3 Adding WebSocket support<\/h2>\n<p>\u79f0\u4e3a\u5347\u7ea7\u63e1\u624b\u7684\u673a\u5236\u7528\u4e8e\u4ece\u6807\u51c6HTTP\u6216HTTPS\u534f\u8bae\u5207\u6362\u5230WebSocket\u3002 \u56e0\u6b64\uff0c\u4f7f\u7528WebSocket\u7684\u5e94\u7528\u7a0b\u5e8f\u5c06\u59cb\u7ec8\u4ee5HTTP \/ S\u5f00\u5934\uff0c\u7136\u540e\u6267\u884c\u5347\u7ea7\u3002 \u5f53\u6070\u597d\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u65f6\uff0c\u5e94\u7528\u7a0b\u5e8f\u662f\u7279\u5b9a\u7684; \u5b83\u53ef\u80fd\u662f\u5728\u542f\u52a8\u65f6\u6216\u8005\u5728\u8bf7\u6c42\u7279\u5b9aURL\u65f6\u3002<\/p>\n<p>\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u91c7\u7528\u4ee5\u4e0b\u7ea6\u5b9a\uff1a\u5982\u679c\u8bf7\u6c42\u7684URL\u4ee5\/ ws\u7ed3\u5c3e\uff0c\u6211\u4eec\u5c06\u534f\u8bae\u5347\u7ea7\u5230WebSocket\u3002 \u5426\u5219\uff0c\u670d\u52a1\u5668\u5c06\u4f7f\u7528\u57fa\u672cHTTP \/ S. \u8fde\u63a5\u5347\u7ea7\u540e\uff0c\u6240\u6709\u6570\u636e\u90fd\u5c06\u4f7f\u7528WebSocket\u4f20\u8f93\u3002 \u56fe12.2\u8bf4\u660e\u4e86\u670d\u52a1\u5668\u903b\u8f91\uff0c\u5b83\u4e00\u5982Netty\uff0c\u5c06\u7531\u4e00\u7ec4ChannelHandler\u5b9e\u73b0\u3002 \u5728\u6211\u4eec\u89e3\u91ca\u7528\u4e8e\u5904\u7406HTTP\u548cWebSocket\u534f\u8bae\u7684\u6280\u672f\u65f6\uff0c\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8282\u4e2d\u5bf9\u5b83\u4eec\u8fdb\u884c\u63cf\u8ff0\u3002<\/p>\n<h3>12.3.1 Handling HTTP requests<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u5c06\u5b9e\u73b0\u5904\u7406HTTP\u8bf7\u6c42\u7684\u7ec4\u4ef6\u3002 \u6b64\u7ec4\u4ef6\u5c06\u63d0\u4f9b\u8bbf\u95ee\u804a\u5929\u5ba4\u7684\u9875\u9762\uff0c\u5e76\u663e\u793a\u5df2\u8fde\u63a5\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6d88\u606f\u3002 \u4ee3\u7801\u6e05\u535512.1\u5305\u542b\u4e86\u8fd9\u4e2aHttpRequestHandler\u7684\u4ee3\u7801\uff0c\u5b83\u4e3aSimpleHttpRequest\u6d88\u606f\u6269\u5c55\u4e86SimpleChannelInboundHandler\u3002 \u8bf7\u6ce8\u610fchannelRead0() \u7684\u5b9e\u73b0\u5982\u4f55\u8f6c\u53d1URI \/ ws\u7684\u4efb\u4f55\u8bf7\u6c42\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aqzt.com\/wp-content\/uploads\/20220223064946-70.png\" alt=\"Figure 12.2\" \/><\/p>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-java\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/>66<br \/>67<br \/>68<br \/>69<br \/>70<br \/>71<br \/>72<br \/>73<br \/>74<br \/>75<br \/>76<br \/>77<br \/>78<br \/>79<br \/>80<br \/>81<br \/>82<br \/><\/div><\/td><td><div class=\"text codecolorer\">1\/\/ Extends SimpleChannelInboundHandler to handle FullHttpRequest messages<br \/>\n2public class HttpRequestHandler<br \/>\n3 &nbsp; &nbsp;extends SimpleChannelInboundHandler&amp;lt;FullHttpRequest&amp;gt; {<br \/>\n4 &nbsp; &nbsp;private final String wsUri;<br \/>\n5 &nbsp; &nbsp;private static final File INDEX;<br \/>\n6 &nbsp; &nbsp;<br \/>\n7 &nbsp; &nbsp;static {<br \/>\n8 &nbsp; &nbsp; &nbsp; &nbsp;URL location = HttpRequestHandler.class<br \/>\n9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getProtectionDomain()<br \/>\n10 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getCodeSource().getLocation();<br \/>\n11 &nbsp; &nbsp; &nbsp; &nbsp;try {<br \/>\n12 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String path = location.toURI() + &amp;quot;index.html&amp;quot;;<br \/>\n13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;path = !path.contains(&amp;quot;file:&amp;quot;) ? path : path.substring(5);<br \/>\n14 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;INDEX = new File(path);<br \/>\n15 &nbsp; &nbsp; &nbsp; &nbsp;} catch (URISyntaxException e) {<br \/>\n16 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new IllegalStateException(&amp;quot;Unable to locate index.html&amp;quot;, e);<br \/>\n17 &nbsp; &nbsp; &nbsp; &nbsp;}<br \/>\n18 &nbsp; &nbsp; }<br \/>\n19<br \/>\n20 &nbsp; &nbsp; public HttpRequestHandler(String wsUri) {<br \/>\n21 &nbsp; &nbsp; &nbsp; &nbsp; this.wsUri = wsUri;<br \/>\n22 &nbsp; &nbsp; }<br \/>\n23 &nbsp;<br \/>\n24 &nbsp; &nbsp; @Override<br \/>\n25 &nbsp; &nbsp; public void channelRead0(ChannelHandlerContext ctx,<br \/>\n26 &nbsp; &nbsp; &nbsp; &nbsp; FullHttpRequest request) throws Exception {<br \/>\n27 &nbsp; &nbsp; &nbsp; &nbsp; \/\/ If a WebSocket upgrade is requested, increments the reference count(retain) and passes it to the next ChannelInboundHandler<br \/>\n28 &nbsp; &nbsp; &nbsp; &nbsp; if(wsUri.equalsIgnoreCase(request.getUri())) {<br \/>\n29 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.fireChannelRead(request.retain());<br \/>\n30 &nbsp; &nbsp; &nbsp; &nbsp; } else {<br \/>\n31 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Handlers 100 Continue requests in conformity with HTTP 1.1<br \/>\n32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(HttpHeaders.is100ContinueExpected(request)) {<br \/>\n33 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; send100Continue(ctx);<br \/>\n34 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n35 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Reads index.html<br \/>\n36 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RandomAccessFile file = new RandomAccessFile(INDEX, &amp;quot;r&amp;quot;);<br \/>\n37 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpResponse response = new DefaultHttpResponse(<br \/>\n38 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.getProtocolVersion(), HttpResponseStatus.OK);<br \/>\n39 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.headers().set(<br \/>\n40 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpHeaders.Names.CONTENT_TYPE,<br \/>\n41 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;quot;text\/plain; charset=UTF-8&amp;quot;);<br \/>\n42 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean keepAlive = HttpHeaders.isKeepAlive(request);<br \/>\n43 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ If keepalive is requested, adds the required headers<br \/>\n44 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(keepAlive) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n45 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;response.headers().set(<br \/>\n46 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpHeaders.Names.CONTENT_LENGTH, file.length());<br \/>\n47 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;response.headers().set(HttpHeaders.Names.CONNECTION,<br \/>\n48 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpHeaders.Values.KEEP_ALIVE);<br \/>\n49 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n50<br \/>\n51 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Writes the HttpResponse to the client<br \/>\n52 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.write(response);<br \/>\n53 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Writes index.html to the client<br \/>\n54 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ctx.pipeline().get(SslHandler.class) == null) {<br \/>\n55 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.write(new DefaultFileRegion(file.getChannel(), 0, file.length()));<br \/>\n56 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br \/>\n57 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ctx.write(new ChunkedNioFile(file.getChannel()));<br \/>\n58 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n59 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Writes and flushes the LastHttpContent to the client<br \/>\n60 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);<br \/>\n61 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!keepAlive) {<br \/>\n62 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;future.addListener(ChannelFutureListener.CLOSE);<br \/>\n63 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br \/>\n64 &nbsp; &nbsp; &nbsp; &nbsp;}<br \/>\n65 &nbsp; &nbsp;}<br \/>\n66<br \/>\n67 &nbsp; &nbsp;private static void send100Continue(ChannelHandlerContext ctx) {<br \/>\n68 &nbsp; &nbsp; &nbsp; FullHttpResponse response = new DefaultFullHttpResponse{<br \/>\n69 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE);<br \/>\n70 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.writeAndFlush(response);<br \/>\n71 &nbsp; &nbsp;}<br \/>\n72<br \/>\n73 &nbsp; &nbsp;@Override<br \/>\n74 &nbsp; &nbsp;public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)<br \/>\n75 &nbsp; &nbsp; &nbsp; &nbsp;throws Exception {<br \/>\n76 &nbsp; &nbsp; &nbsp; &nbsp;cause.printStackTrace();<br \/>\n77 &nbsp; &nbsp; &nbsp; &nbsp;ctx.close();<br \/>\n78 &nbsp; &nbsp;}<br \/>\n79}<br \/>\n80 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n81<br \/>\n82<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p>\u5982\u679cHTTP\u8bf7\u6c42\u5f15\u7528URI \/ ws\uff0c\u5219HttpRequestHandler\u5728FullHttpRequest\u4e0a\u8c03\u7528 retain() \u5e76\u901a\u8fc7\u8c03\u7528 fireChannelRead(msg) \u5c06\u5176\u8f6c\u53d1\u5230\u4e0b\u4e00\u4e2a ChannelInboundHandler . \u9700\u8981\u8c03\u7528retain()\uff0c\u56e0\u4e3a\u5728 channelRead() \u5b8c\u6210\u540e\uff0c\u5b83\u5c06\u8c03\u7528 FullHttpRequest \u4e0a\u7684 release() \u6765\u91ca\u653e\u5176\u8d44\u6e90\u3002\uff08\u8bf7\u53c2\u9605\u7b2c6\u7ae0\u4e2d\u5bf9SimpleChannelInboundHandler\u7684\u8ba8\u8bba\u3002\uff09<\/p>\n<p>\u5982\u679c\u5ba2\u6237\u7aef\u53d1\u9001HTTP 1.1\u6807\u5934Expect\uff1a100-continue\uff0c\u5219HttpRequestHandler\u53d1\u9001100 Continue\u54cd\u5e94\u3002 \u5728\u8bbe\u7f6e\u6807\u5934\u540e\uff0cHttpRequestHandler\u5c06HttpResponse d\u5199\u56de\u5ba2\u6237\u7aef\u3002 \u8fd9\u4e0d\u662fFullHttpResponse\uff0c\u56e0\u4e3a\u5b83\u53ea\u662f\u54cd\u5e94\u7684\u7b2c\u4e00\u90e8\u5206\u3002 \u6b64\u5916\uff0c\u6b64\u5904\u4e0d\u8c03 writeAndFlush()\u3002<br \/>\n<!-- -->\u8fd9\u662f\u5728\u6700\u540e\u5b8c\u6210\u7684\u3002<\/p>\n<p>\u5982\u679c\u65e2\u4e0d\u9700\u8981\u52a0\u5bc6\u4e5f\u4e0d\u9700\u8981\u538b\u7f29\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u5c06index.html e\u7684\u5185\u5bb9\u5b58\u50a8\u5728DefaultFileRegion\u4e2d\u6765\u5b9e\u73b0\u6700\u9ad8\u6548\u7387\u3002 \u8fd9\u5c06\u5229\u7528\u96f6\u62f7\u8d1d\u6765\u6267\u884c\u4f20\u8f93\u3002 \u56e0\u6b64\uff0c\u60a8\u9700\u8981\u68c0\u67e5ChannelPipeline\u4e2d\u662f\u5426\u5b58\u5728SslHandler\u3002 \u6216\u8005\uff0c\u60a8\u4f7f\u7528ChunkedNioFile\u3002<\/p>\n<p>HttpRequestHandler\u5199\u4e00\u4e2aLastHttpContent\u6765\u6807\u8bb0\u54cd\u5e94\u7684\u7ed3\u675f\u3002 \u5982\u679c\u672a\u8bf7\u6c42keepalive\uff0c\u5219HttpRequestHandler\u5c06ChannelFutureListener\u6dfb\u52a0\u5230\u4e0a\u6b21\u5199\u5165\u7684 ChannelFuture \u5e76\u5173\u95ed\u8fde\u63a5\u3002 \u8fd9\u662f\u60a8\u8c03\u7528 writeAndFlush() \u6765\u5237\u65b0\u6240\u6709\u4ee5\u524d\u5199\u5165\u7684\u6d88\u606f\u7684\u5730\u65b9\u3002<\/p>\n<p>\u8fd9\u4ee3\u8868\u804a\u5929\u670d\u52a1\u5668\u7684\u7b2c\u4e00\u90e8\u5206\uff0c\u5b83\u7ba1\u7406\u7eafHTTP\u8bf7\u6c42\u548c\u54cd\u5e94\u3002 \u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u5904\u7406WebSocket\u5e27\uff0c\u5b83\u4eec\u4f20\u8f93\u5b9e\u9645\u7684\u804a\u5929\u6d88\u606f\u3002<\/p>\n<p><strong>WebSocket Frames<\/strong> WebSockets\u4ee5\u5e27\u7684\u5f62\u5f0f\u4f20\u8f93\u6570\u636e\uff0c\u6bcf\u4e2a\u5e27\u90fd\u4ee3\u8868\u6d88\u606f\u7684\u4e00\u90e8\u5206\u3002 \u5b8c\u6574\u7684\u6d88\u606f\u53ef\u80fd\u5305\u542b\u8bb8\u591a\u5e27\u3002<\/p>\n<h3>12.3.2 Handling WebSocket frames<\/h3>\n<p>\u7531IETF\u53d1\u5e03\u7684WebSocket RFC\u5b9a\u4e49\u4e86\u516d\u4e2a\u5e27; Netty\u4e3a\u6bcf\u4e2a\u4eba\u63d0\u4f9bPOJO\u5b9e\u65bd\u3002 \u886812.1\u5217\u51fa\u4e86\u5e27\u7c7b\u578b\u5e76\u63cf\u8ff0\u4e86\u5b83\u4eec\u7684\u7528\u6cd5\u3002<\/p>\n<p>BinaryWebSocketFrame<br \/>\n\u5305\u542b binary data<br \/>\nTextWebSocketFrame<br \/>\n\u5305\u542b text data<br \/>\nContinuationWebSocketFrame<br \/>\n\u5305\u542b text \u6216\u8005 binary \u6570\u636e\uff0c\u4ed6\u5c5e\u4e8e\u524d\u4e00\u4e2a BinaryWebSocketFrame \u6216\u8005 TextWebSocketFrame<br \/>\nCloseWebSocketFrame<br \/>\n\u8868\u793aCLOSE\u8bf7\u6c42\uff0c\u5305\u542b\u5173\u95ed\u72b6\u6001\u4ee3\u7801\u548c\u77ed\u8bed<br \/>\nPingWebSocketFrame<br \/>\n\u8bf7\u6c42\u4f20\u8f93PongWebSocketFrame<br \/>\nPongWebSocketFrame<br \/>\n\u4f5c\u4e3a\u54cd\u5e94\u53d1\u9001\u7ed9\u4e00\u4e2a PingWebSocketFrame<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aqzt.com\/wp-content\/uploads\/20220223064947-41.png\" alt=\"Table 12.1 WebSocketFrame types\" \/><\/p>\n<p>\u6211\u4eec\u7684\u804a\u5929\u7a0b\u5e8f\u5c06\u4f1a\u4f7f\u7528\u4e0b\u9762\u8fd9\u4e9b frame \u7c7b\u578b:<\/p>\n<ul>\n<li>CloseWebSocketFrame<\/li>\n<li>PingWebSocketFrame<\/li>\n<li>PongWebSocketFrame<\/li>\n<li>TextWebSocketFrame<\/li>\n<\/ul>\n<p>TextWebSocketFrame\u662f\u6211\u4eec\u5b9e\u9645\u9700\u8981\u5904\u7406\u7684\u552f\u4e00\u4e00\u4e2a\u3002 \u6839\u636eWebSocket RFC\uff0cNetty\u63d0\u4f9b\u4e86\u4e00\u4e2aWebSocketServerProtocolHandler\u6765\u7ba1\u7406\u5176\u4ed6\u7684\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u6e05\u5355\u663e\u793a\u4e86TextWebSocketFrames\u7684ChannelInboundHandler\uff0c\u5b83\u8fd8\u5c06\u8ddf\u8e2a\u5176ChannelGroup\u4e2d\u7684\u6240\u6709\u6d3b\u52a8WebSocket\u8fde\u63a5\u3002<\/p>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-java\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/><\/div><\/td><td><div class=\"text codecolorer\">1\/\/ Extends SimpleChannelInboundHandler and handle TextWebSocketFramemessages<br \/>\n2pyblic class TextWebSocketFrameHandler<br \/>\n3 &nbsp; &nbsp;extends SimpleChannelInboundHandler&amp;lt;TextWebSocketFrame&amp;gt; {<br \/>\n4 &nbsp; &nbsp;private final ChannelGroup group;<br \/>\n5 &nbsp;<br \/>\n6 &nbsp; &nbsp;public TextWebSocketFrameHandler(ChannelGroup group) {<br \/>\n7 &nbsp; &nbsp; &nbsp; &nbsp;this.group = group;<br \/>\n8 &nbsp; &nbsp;}<br \/>\n9<br \/>\n10 &nbsp; &nbsp;\/\/ Overrides userEventTriggered() to handle custome events<br \/>\n11 &nbsp; &nbsp;@Override<br \/>\n12 &nbsp; &nbsp;public void userEventTriggered(ChannelHandlerContext ctx,<br \/>\n13 &nbsp; &nbsp; &nbsp; &nbsp;Object evt) throws Exception {<br \/>\n14 &nbsp; &nbsp; &nbsp; &nbsp;if(evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) &nbsp; &nbsp; <br \/>\n15 &nbsp; &nbsp; &nbsp; &nbsp;{<br \/>\n16 &nbsp; &nbsp; &nbsp; &nbsp;ctx.pipeline().remove(HttpRequestHandler.class);<br \/>\n17 &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Notifies all connected WebSocket clients that new Client has connected<br \/>\n18 &nbsp; &nbsp; &nbsp; &nbsp;group.writeAndFlush(new TextWebSocketFrame(<br \/>\n19 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;quot;Client&amp;quot; + ctx.channel() + &amp;quot; joined&amp;quot;));<br \/>\n20 &nbsp; &nbsp; &nbsp; &nbsp;group.add(ctx.channel());<br \/>\n21 &nbsp; &nbsp; &nbsp; &nbsp;} else {<br \/>\n22 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;super.userEventTriggered(ctx, evt);<br \/>\n23 &nbsp; &nbsp; &nbsp; &nbsp;}<br \/>\n24 &nbsp; &nbsp;}<br \/>\n25<br \/>\n26 &nbsp; &nbsp;@Override<br \/>\n27 &nbsp; &nbsp;public void channelRead0(ChannelHandlerContext ctx,<br \/>\n28 &nbsp; &nbsp; &nbsp; &nbsp;TextWebSocketFrame msg) throws Exception {<br \/>\n29 &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Increments the reference count of the message and writes it to all connected clients in the ChannelGroup<br \/>\n30 &nbsp; &nbsp; &nbsp; &nbsp;group.writeAndFlush(msg.retain());<br \/>\n31 &nbsp; &nbsp;}<br \/>\n32} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n33 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n34<br \/>\n35<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p>TextWebSocketFrameHandler\u53ea\u6709\u5f88\u5c11\u7684\u804c\u8d23\u3002 \u5f53\u4e0e\u65b0\u5ba2\u6237\u7aef\u7684WebSocket\u63e1\u624b\u6210\u529f\u5b8c\u6210\u65f6\uff0c\u5b83\u901a\u8fc7\u5199\u5165ChannelGroup\u4e2d\u7684\u6240\u6709Channel\u6765\u901a\u77e5\u6240\u6709\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\uff0c\u7136\u540e\u5c06\u65b0Channel\u6dfb\u52a0\u5230ChannelGroup.<\/p>\n<p>\u5982\u679c\u6536\u5230TextWebSocketFrame\uff0c\u5b83\u4f1a\u8c03\u7528retain\uff08\uff09\u5e76\u4f7f\u7528writeAndFlush\uff08\uff09\u5c06\u5176\u4f20\u8f93\u5230ChannelGroup\uff0c\u4ee5\u4fbf\u6240\u6709\u8fde\u63a5\u7684WebSocket Channel\u90fd\u63a5\u6536\u5b83\u3002<\/p>\n<p>\u548c\u4ee5\u524d\u4e00\u6837\uff0c\u8c03\u7528 retain() \u662f\u5fc5\u9700\u7684\uff0c\u56e0\u4e3a\u5f53 channelRead0() \u8fd4\u56de\u65f6TextWebSocketFrame \u7684\u5f15\u7528\u8ba1\u6570\u5c06\u51cf\u5c11\u3002 \u7531\u4e8e\u6240\u6709\u64cd\u4f5c\u90fd\u662f\u5f02\u6b65\u7684\uff0c\u56e0\u6b64writeAndFlush() \u53ef\u80fd\u4f1a\u5728\u4ee5\u540e\u5b8c\u6210\uff0c\u5e76\u4e14\u5b83\u4e0d\u80fd\u8bbf\u95ee\u5df2\u53d8\u4e3a\u65e0\u6548\u7684\u5f15\u7528\u3002<\/p>\n<p>\u7531\u4e8eNetty\u5728\u5185\u90e8\u5904\u7406\u5927\u90e8\u5206\u5269\u4f59\u529f\u80fd\uff0c\u56e0\u6b64\u73b0\u5728\u552f\u4e00\u8981\u505a\u7684\u5c31\u662f\u4e3a\u6bcf\u4e2a\u521b\u5efa\u7684\u65b0Channel\u521d\u59cb\u5316ChannelPipeline\u3002 \u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2aChannelInitializer\u3002<\/p>\n<h3>12.3.3 Initializing the ChannelPipeline<\/h3>\n<p>\u5982\u60a8\u6240\u77e5\uff0c\u8981\u5728 ChannelPipeline \u4e2d\u5b89\u88c5 ChannelHandler\uff0c\u60a8\u9700\u8981\u6269\u5c55 ChannelInitializer\u5e76\u5b9e\u73b0 initChannel() \u3002 \u4ee5\u4e0b\u6e05\u5355\u663e\u793a\u4e86\u751f\u6210\u7684ChatServerInitializer\u7684\u4ee3\u7801\u3002<\/p>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-java\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/><\/div><\/td><td><div class=\"text codecolorer\">1\/\/ Extends ChannelInitializer<br \/>\n2public class ChatServerIntializer extends ChannelIntializer&amp;lt;Channel&amp;gt; {<br \/>\n3 &nbsp; &nbsp;private final ChannelGroup group;<br \/>\n4<br \/>\n5 &nbsp; &nbsp;public ChatServerIntializer(ChannelGroup group) {<br \/>\n6 &nbsp; &nbsp; &nbsp; &nbsp;this.group = group;<br \/>\n7 &nbsp; &nbsp;}<br \/>\n8<br \/>\n9 &nbsp; &nbsp;\/\/ Adds all needed ChannelHandlers to the ChannelPipeline<br \/>\n10 &nbsp; &nbsp;@Override<br \/>\n11 &nbsp; &nbsp;protected void initChannel(Channel ch) throws Exception {<br \/>\n12 &nbsp; &nbsp; &nbsp; &nbsp;ChannelPipeline pipeline = ch.pipeline();<br \/>\n13 &nbsp; &nbsp; &nbsp; &nbsp;pipeline.addLast(new HttpServerCodec());<br \/>\n14 &nbsp; &nbsp; &nbsp; &nbsp;pipeline.addLast(new ChunkedWriteHandler());<br \/>\n15 &nbsp; &nbsp; &nbsp; &nbsp;pipeline.addLast(new HttpObjectAggregator(64 * 1024));<br \/>\n16 &nbsp; &nbsp; &nbsp; &nbsp;pipeline.addLast(new HttpRequestHandler(&amp;quot;\/ws&amp;quot;));<br \/>\n17 &nbsp; &nbsp; &nbsp; &nbsp;pipeline.addLast(new WebSocketServerProtocolHandler(&amp;quot;\/ws&amp;quot;));<br \/>\n18 &nbsp; &nbsp; &nbsp; &nbsp;pipeline.addLast(new TextWebSocketFrameHandler(group));<br \/>\n19 &nbsp; &nbsp;}<br \/>\n20} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n21<br \/>\n22<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p>\u5bf9 initChannel() \u7684\u8c03\u7528\u901a\u8fc7\u5b89\u88c5\u6240\u6709\u5fc5\u9700\u7684 ChannelHandler \u6765\u8bbe\u7f6e\u65b0\u6ce8\u518c Channel \u7684ChannelPipeline\u3002 \u886812.2\u603b\u7ed3\u4e86\u8fd9\u4e9b\u5185\u5bb9\u53ca\u5176\u5404\u81ea\u7684\u804c\u8d23\u3002<\/p>\n<p>HttpServerCodec<br \/>\n\u5c06\u5b57\u8282\u89e3\u7801\u4e3aHttpRequest\uff0cHttpContent\uff0c\u548cLastHttpContent\u3002 \u7f16\u7801HttpRequest\uff0cHttpContent\u548cLastHttpContent\u5230\u5b57\u8282\u3002<br \/>\nChunkedWriteHandler<br \/>\n\u5199\u5165\u6587\u4ef6\u7684\u5185\u5bb9\u3002<br \/>\nHttpObjectAggregator<br \/>\n\u5c06HttpMessage\u53ca\u5176\u540e\u7eedHttpContent\u805a\u5408\u5230\u5355\u4e2aFullHttpRequest\u6216FullHttpResponse\u4e2d\uff08\u53d6\u51b3\u4e8e\u5b83\u662f\u5426\u7528\u4e8e\u5904\u7406\u8bf7\u6c42\u6216\u54cd\u5e94\uff09\u3002 \u5b89\u88c5\u6b64\u9009\u9879\u540e\uff0c\u7ba1\u9053\u4e2d\u7684\u4e0b\u4e00\u4e2aChannelHandler\u5c06\u4ec5\u63a5\u6536\u5b8c\u6574\u7684HTTP\u8bf7\u6c42\u3002<br \/>\nHttpRequestHandler<br \/>\n\u5904\u7406FullHttpRequest\uff08\u672a\u53d1\u9001\u5230 \/ws URI\uff09\u3002<br \/>\nWebSocketServerProtocolHandler<br \/>\n\u6839\u636eWebSocket\u89c4\u8303\u7684\u8981\u6c42\uff0c\u5904\u7406WebSocket\u5347\u7ea7\u63e1\u624b\uff0cPingWebSocketFrames\uff0cPongWebSocketFrames\u548cCloseWebSocketFrames\u3002<br \/>\nTextWebSocketFrameHandler<br \/>\n\u5904\u7406TextWebSocketFrames\u548c\u63e1\u624b\u5b8c\u6210\u4e8b\u4ef6<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aqzt.com\/wp-content\/uploads\/20220223064948-38.png\" alt=\"Table 12.2 ChannelHandlers\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aqzt.com\/wp-content\/uploads\/20220223064949-60.png\" alt=\"Table 12.2 ChannelHandlers for the WebSocket chat server\" \/><br \/>\n<!-- -->Netty \u7684 WebSocketServerProtocolHandler \u5904\u7406\u6240\u6709\u5f3a\u5236 WebSocket \u5e27\u7c7b\u578b\u548c\u5347\u7ea7\u63e1\u624b\u672c\u8eab\u3002 \u5982\u679c\u63e1\u624b\u6210\u529f\uff0c\u5219\u5c06\u6240\u9700\u7684ChannelHandler\u6dfb\u52a0\u5230\u7ba1\u9053\u4e2d\uff0c\u5e76\u5220\u9664\u4e0d\u518d\u9700\u8981\u7684\u90a3\u4e9b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aqzt.com\/wp-content\/uploads\/20220223064950-67.png\" alt=\"Figure 12.3 ChannelPipeline before WebSocket upgrade\" \/><\/p>\n<p>\u5347\u7ea7\u524d\u7684\u7ba1\u9053\u72b6\u6001\u5982\u56fe12.3\u6240\u793a\u3002 \u8fd9\u8868\u793aChatServerInitializer\u521d\u59cb\u5316\u540e\u7684ChannelPipeline\u3002<\/p>\n<p>\u5347\u7ea7\u5b8c\u6210\u540e\uff0cWebSocketServerProtocolHandler\u5c06\u66ff\u6362\u5e26\u6709WebSocketFrameDecoder\u7684HttpRequestDecoder\u548c\u5e26\u6709WebSocketFrameEncoder\u7684HttpResponseEncoder\u3002 \u4e3a\u4e86\u6700\u5927\u9650\u5ea6\u5730\u63d0\u9ad8\u6027\u80fd\uff0c\u5b83\u5c06\u5220\u9664WebSocket\u8fde\u63a5\u4e0d\u9700\u8981\u7684\u4efb\u4f55ChannelHandler\u3002 \u8fd9\u4e9b\u5c06\u5305\u62ec\u56fe12.3\u4e2d\u6240\u793a\u7684HttpObjectAggregator\u548cHttpRequestHandler\u3002<\/p>\n<p>\u56fe12.4\u663e\u793a\u4e86\u8fd9\u4e9b\u64cd\u4f5c\u5b8c\u6210\u540e\u7684ChannelPipeline\u3002 \u8bf7\u6ce8\u610f\uff0cNetty\u76ee\u524d\u652f\u6301\u56db\u79cd\u7248\u672c\u7684WebSocket\u534f\u8bae\uff0c\u6bcf\u79cd\u7248\u672c\u90fd\u6709\u81ea\u5df1\u7684\u5b9e\u73b0\u7c7b\u3002 \u6839\u636e\u5ba2\u6237\u7aef\uff08\u6b64\u5904\u4e3a\u6d4f\u89c8\u5668\uff09\u652f\u6301\u7684\u5185\u5bb9\uff0c\u81ea\u52a8\u6267\u884c\u6b63\u786e\u7248\u672c\u7684WebSocketFrameDecoder\u548cWebSocketFrameEncoder\u7684\u9009\u62e9\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aqzt.com\/wp-content\/uploads\/20220223064951-72.png\" alt=\"Figure 12.4 ChannelPipeline after WebSocket upgrade\" \/><\/p>\n<h3>12.3.4 Bootstrapping<\/h3>\n<p>\u56fe\u7247\u7684\u6700\u540e\u4e00\u90e8\u5206\u662f\u5f15\u5bfc\u670d\u52a1\u5668\u5e76\u5b89\u88c5ChatServerInitializer\u7684\u4ee3\u7801\u3002 \u8fd9\u5c06\u7531ChatServer\u7c7b\u5904\u7406\uff0c\u5982\u6b64\u5904\u6240\u793a\u3002<\/p>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-java\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/><\/div><\/td><td><div class=\"text codecolorer\">1public class ChatServer {<br \/>\n2 &nbsp; &nbsp;\/\/ Creates DefaultChannelGroup that will hold all connected WebSocket channels<br \/>\n3 &nbsp; &nbsp;private final ChannelGroup channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE);<br \/>\n4 &nbsp; &nbsp;private final EventLoopGroup group = new NioEventLoopGroup();<br \/>\n5 &nbsp; &nbsp;private Channel channel;<br \/>\n6<br \/>\n7 &nbsp; &nbsp;public ChannelFuture start(InetSocketAddress address) {<br \/>\n8 &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Bootstraps the server<br \/>\n9 &nbsp; &nbsp; &nbsp; &nbsp;ServerBootstrap bootstrap = new ServerBootstrap();<br \/>\n10 &nbsp; &nbsp; &nbsp; &nbsp;bootstrap.group(group)<br \/>\n11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.channel(NioServerSocketChannel.class)<br \/>\n12 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.childHandler(createInitializer(channelGroup));<br \/>\n13 &nbsp; &nbsp; &nbsp; &nbsp;ChannelFuture future = bootstrap.bind(address);<br \/>\n14 &nbsp; &nbsp; &nbsp; &nbsp;future.syncUninterruptibly();<br \/>\n15 &nbsp; &nbsp; &nbsp; &nbsp;channel = future.channel();<br \/>\n16 &nbsp; &nbsp; &nbsp; &nbsp;return future;<br \/>\n17 &nbsp; }<br \/>\n18<br \/>\n19<br \/>\n20 &nbsp; \/\/ Creates the ChatServerInitializer<br \/>\n21 &nbsp; protected ChannelInitializer&amp;lt;Channel&amp;gt; createInitializer(ChannelGroup group) {<br \/>\n22 &nbsp; &nbsp; &nbsp; return new ChatServerInitializer(group);<br \/>\n23 &nbsp; }<br \/>\n24<br \/>\n25 &nbsp; \/\/ Handles server shutdown and releases all resources<br \/>\n26 &nbsp; public void destroy() {<br \/>\n27 &nbsp; &nbsp; &nbsp; if(channel != null) {<br \/>\n28 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel.close();<br \/>\n29 &nbsp; &nbsp; &nbsp; }<br \/>\n30 &nbsp; &nbsp; &nbsp; channelGroup.close();<br \/>\n31 &nbsp; &nbsp; &nbsp; group.shutdownGracefully();<br \/>\n32 &nbsp; }<br \/>\n33 &nbsp;<br \/>\n34 &nbsp; public static void main(String[] args) throws Exception {<br \/>\n35 &nbsp; &nbsp; &nbsp; if (args.length != 1) {<br \/>\n36 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println(&amp;quot;Please give port as argument.&amp;quot;);<br \/>\n37 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(1);<br \/>\n38 &nbsp; &nbsp; &nbsp; }<br \/>\n39 &nbsp; &nbsp; &nbsp; int port = Integer.praseInt(args[0]);<br \/>\n40 &nbsp; &nbsp; &nbsp; final ChatServer endpoint = new ChatServer();<br \/>\n41 &nbsp; &nbsp; &nbsp; ChannelFuture future = endpoint.start( new InetSocketAddress(port));<br \/>\n42 &nbsp; &nbsp; &nbsp; Runtime.getRuntime().addShutdownHook(new Thread() {<br \/>\n43 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br \/>\n44 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {<br \/>\n45 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endpoint.destroy();<br \/>\n46 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n47 &nbsp; &nbsp; &nbsp; });<br \/>\n48 &nbsp; &nbsp; &nbsp; future.channel().closeFuture().syncUninterruptibly();<br \/>\n49 &nbsp; }<br \/>\n50 } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n51<br \/>\n52<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p>\u8fd9\u5b8c\u6210\u4e86\u5e94\u7528\u7a0b\u5e8f\u672c\u8eab\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u6765\u6d4b\u8bd5\u5427\u3002<\/p>\n<h2>12.4 Testing the application<\/h2>\n<p>chapter12\u76ee\u5f55\u4e2d\u7684\u793a\u4f8b\u4ee3\u7801\u5305\u542b\u6784\u5efa\u548c\u8fd0\u884c\u670d\u52a1\u5668\u6240\u9700\u7684\u4e00\u5207\u3002 \uff08\u5982\u679c\u60a8\u5c1a\u672a\u8bbe\u7f6e\u5305\u62ecApache Maven\u5728\u5185\u7684\u5f00\u53d1\u73af\u5883\uff0c\u8bf7\u53c2\u9605\u7b2c2\u7ae0\u4e2d\u7684\u8bf4\u660e\u3002\uff09<\/p>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528\u4ee5\u4e0bMaven\u547d\u4ee4\u6765\u6784\u5efa\u548c\u542f\u52a8\u670d\u52a1\u5668\uff1a<\/p>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-bash\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/><\/div><\/td><td><div class=\"text codecolorer\">1mvn -PChatServer clean package exec:exec<br \/>\n2<br \/>\n3<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p>\u9879\u76ee\u6587\u4ef6pom.xml\u914d\u7f6e\u4e3a\u5728\u7aef\u53e39999\u4e0a\u542f\u52a8\u670d\u52a1\u5668\u3002\u8981\u4f7f\u7528\u5176\u4ed6\u7aef\u53e3\uff0c\u60a8\u53ef\u4ee5\u7f16\u8f91\u6587\u4ef6\u4e2d\u7684\u503c\u6216\u4f7f\u7528System\u5c5e\u6027\u8986\u76d6\u5b83\uff1a<\/p>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-bash\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/><\/div><\/td><td><div class=\"text codecolorer\">1mvn -PChatServer -Dport=1111 clean package exec:exec<br \/>\n2<br \/>\n3<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p>\u4ee5\u4e0b\u6e05\u5355\u663e\u793a\u4e86\u547d\u4ee4\u7684\u4e3b\u8981\u8f93\u51fa\uff08\u5df2\u5220\u9664\u975e\u5fc5\u8981\u884c\uff09\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aqzt.com\/wp-content\/uploads\/20220223064952-37.png\" alt=\"Listing 12.5 Compile and start the ChatServer\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aqzt.com\/wp-content\/uploads\/20220223064954-70.png\" alt=\"Figure 12.5 WebSocket ChatServer demonstration\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u6d4f\u89c8\u5668\u6307\u5411http\uff1a\/\/ localhost\uff1a9999\u6765\u8bbf\u95ee\u8be5\u5e94\u7528\u7a0b\u5e8f\u3002 \u56fe12.5\u663e\u793a\u4e86Chrome\u6d4f\u89c8\u5668\u4e2d\u7684UI\u3002<\/p>\n<p>\u8be5\u56fe\u663e\u793a\u4e86\u4e24\u4e2a\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\u3002 \u7b2c\u4e00\u4e2a\u662f\u4f7f\u7528\u9876\u90e8\u7684\u754c\u9762\u8fde\u63a5\u3002 \u7b2c\u4e8c\u4e2a\u5ba2\u6237\u7aef\u901a\u8fc7\u5e95\u90e8\u7684Chrome\u6d4f\u89c8\u5668\u547d\u4ee4\u884c\u8fde\u63a5\u3002 \u60a8\u4f1a\u6ce8\u610f\u5230\u4e24\u4e2a\u5ba2\u6237\u7aef\u90fd\u53d1\u9001\u4e86\u6d88\u606f\uff0c\u5e76\u4e14\u6bcf\u6761\u6d88\u606f\u90fd\u663e\u793a\u5728\u4e24\u4e2a\u5ba2\u6237\u7aef\u4e0a\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u6f14\u793a\uff0c\u8bf4\u660eWebSocket\u5982\u4f55\u5728\u6d4f\u89c8\u5668\u4e2d\u5b9e\u73b0\u5b9e\u65f6\u901a\u4fe1\u3002<\/p>\n<h3>12.4.1 What about encryption?<\/h3>\n<p>\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u60a8\u5f88\u5feb\u5c31\u4f1a\u88ab\u8981\u6c42\u4e3a\u6b64\u670d\u52a1\u5668\u6dfb\u52a0\u52a0\u5bc6\u3002 \u4f7f\u7528Netty\uff0c\u53ea\u9700\u5c06SslHandler\u6dfb\u52a0\u5230ChannelPipeline\u5e76\u8fdb\u884c\u914d\u7f6e\u5373\u53ef\u3002 \u4ee5\u4e0b\u6e05\u5355\u663e\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u6269\u5c55ChatServerInitializer\u6765\u521b\u5efaSecureChatServerInitializer\u6765\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002<\/p>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-java\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/><\/div><\/td><td><div class=\"text codecolorer\">1\/\/ Adding encryption to the ChannelPipeline<br \/>\n2\/\/ Extends ChatServerInitializer to add encryption<br \/>\n3public class SecureChatServerInitializer extends ChatServerInitializer {<br \/>\n4 &nbsp; &nbsp;private final SslContext context;<br \/>\n5<br \/>\n6 &nbsp; &nbsp;public SecureChatServerInitializer(ChannelGroup group,<br \/>\n7 &nbsp; &nbsp; &nbsp; &nbsp;SslContext context) {<br \/>\n8 &nbsp; &nbsp; &nbsp; &nbsp;super(group);<br \/>\n9 &nbsp; &nbsp; &nbsp; &nbsp;this.context = context;<br \/>\n10 &nbsp; }<br \/>\n11<br \/>\n12 &nbsp; @Override<br \/>\n13 &nbsp; protected void initChannel(Channel ch) throws Exception {<br \/>\n14 &nbsp; &nbsp; &nbsp; \/\/ Calls the parent&amp;#x27;s initChannel()<br \/>\n15 &nbsp; &nbsp; &nbsp; super.initChannel(ch);<br \/>\n16 &nbsp; &nbsp; &nbsp; SSLEngine engine = context.newEngine(ch.alloc());<br \/>\n17 &nbsp; &nbsp; &nbsp; \/\/ Adds the SslHandler to the ChannelPipeline<br \/>\n18 &nbsp; &nbsp; &nbsp; ch.pipeline().addFirst(new SslHandler(engine)); &nbsp; <br \/>\n19 &nbsp;}<br \/>\n20} &nbsp; &nbsp; <br \/>\n21<br \/>\n22<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p>\u6700\u540e\u4e00\u6b65\u662f\u8c03\u6574ChatServer\u4ee5\u4f7f\u7528SecureChatServerInitializer\uff0c\u4ee5\u4fbf\u5728\u7ba1\u9053\u4e2d\u5b89\u88c5SslHandler\u3002 \u8fd9\u7ed9\u4e86\u6211\u4eec\u8fd9\u91cc\u663e\u793a\u7684SecureChatServer\u3002<\/p>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-java\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/><\/div><\/td><td><div class=\"text codecolorer\">1\/\/ Adding encryption to the ChatServer<br \/>\n2\/\/ SecureChatServer extends ChatServer to support encryption<br \/>\n3public class SecureChatServer extends ChatServer {<br \/>\n4 &nbsp; &nbsp;private final SslContext context;<br \/>\n5<br \/>\n6 &nbsp; &nbsp;public SecureChatServer(SslContext context) {<br \/>\n7 &nbsp; &nbsp; &nbsp; &nbsp;this.context = context;<br \/>\n8 &nbsp; &nbsp;}<br \/>\n9<br \/>\n10 &nbsp; &nbsp;@Override<br \/>\n11 &nbsp; &nbsp;protected ChannelInitializer&amp;lt;Channel&amp;gt; createInitializer(ChannelGroup group) {<br \/>\n12 &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Returns the previously created SecureChatServerInitializer to enable encryption<br \/>\n13 &nbsp; &nbsp; &nbsp; &nbsp;return new SecureChatServerInitializer(group, context);<br \/>\n14 &nbsp; }<br \/>\n15<br \/>\n16 &nbsp; public static void main(String[] args) throws Exception {<br \/>\n17 &nbsp; &nbsp; &nbsp; if(args.length != 1) {<br \/>\n18 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println(&amp;quot;Please give port as argument&amp;quot;);<br \/>\n19 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(1);<br \/>\n20 &nbsp; &nbsp; &nbsp; }<br \/>\n21 &nbsp; &nbsp; &nbsp; int port = Integer.parseInt(args[0]);<br \/>\n22 &nbsp; &nbsp; &nbsp; SelfSignedCertificate cert = new SelfSignedCertificate();<br \/>\n23 &nbsp; &nbsp; &nbsp; SslContext context = SslContext.newServerContext(<br \/>\n24 &nbsp; &nbsp; &nbsp; cert.certificate(), cert.privateKey());<br \/>\n25<br \/>\n26 &nbsp; &nbsp; &nbsp; final SecureChatServer endpoint = new SecureChatServer(context);<br \/>\n27 &nbsp; &nbsp; &nbsp; ChannelFuture future = endpoint.start(new InetSocketAddress(port));<br \/>\n28 &nbsp; &nbsp; &nbsp; Runtime.getRuntime().addShutdownHook(new Thread() {<br \/>\n29 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br \/>\n30 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {<br \/>\n31 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endpoint.destroy();<br \/>\n32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n33 &nbsp; &nbsp; &nbsp; });<br \/>\n34 &nbsp; &nbsp; &nbsp; future.channel().closeFuture().syncUninterruptibly();<br \/>\n35 &nbsp; &nbsp;}<br \/>\n36 } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n37<br \/>\n38<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p>\u8fd9\u5c31\u662f\u4e3a\u6240\u6709\u901a\u4fe1\u542f\u7528SSL \/ TLS\u52a0\u5bc6\u6240\u9700\u7684\u5168\u90e8\u5185\u5bb9\u3002 \u548c\u4ee5\u524d\u4e00\u6837\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Apache Maven\u6765\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002 \u5b83\u8fd8\u5c06\u68c0\u7d22\u4efb\u4f55\u6240\u9700\u7684\u4f9d\u8d56\u9879\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aqzt.com\/wp-content\/uploads\/20220223064955-90.png\" alt=\"Listing 12.8 Starting the SecureChatServer\" \/><br \/>\n<!-- -->\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u4ece\u5176HTTPS URL\u8bbf\u95eeSecureChatServer\uff1a<a href=\"https:\/\/localhost:9999%E3%80%82\">https:\/\/localhost:9999\u3002<\/a><\/p>\n<h2>12.5 Summary<\/h2>\n<p>\u5728\u672c\u7ae0\u4e2d\uff0c\u60a8\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528Netty\u7684WebSocket\u5b9e\u73b0\u6765\u7ba1\u7406Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5b9e\u65f6\u6570\u636e\u3002 \u6211\u4eec\u4ecb\u7ecd\u4e86\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u8ba8\u8bba\u4e86\u60a8\u53ef\u80fd\u9047\u5230\u7684\u9650\u5236\u3002 \u867d\u7136\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u53ef\u80fd\u65e0\u6cd5\u4f7f\u7528WebSocket\uff0c\u4f46\u5e94\u8be5\u6e05\u695a\u5b83\u4ee3\u8868\u4e86\u4e00\u4e2a\u91cd\u8981\u7684\u8fdb\u6b65\u7f51\u7edc\u6280\u672f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Network protocols WebSocket\u662f\u4e00\u79cd\u9ad8\u7ea7\u7f51\u7edc\u534f\u8bae\uff0c\u65e8\u5728\u63d0\u9ad8Web\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002 \u6211\u4eec\u5c06\u901a\u8fc7\u7f16\u5199\u793a\u4f8b\u5e94\u7528\u7a0b\u5e8f\u6765\u63a2\u7d22Netty\u5bf9\u5b83\u4eec\u7684\u652f\u6301\u3002 \u5728\u7b2c12\u7ae0\u4e2d\uff0c\u60a8\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528WebSocket\u5b9e\u73b0\u53cc\u5411\u6570\u636e\u4f20\u8f93\uff0c\u65b9\u6cd5\u662f\u6784\u5efa\u4e00\u4e2a\u804a\u5929\u5ba4\u670d\u52a1\u5668\uff0c\u5176\u4e2d\u591a\u4e2a\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\u53ef\u4ee5\u5b9e\u65f6\u901a\u4fe1\u3002 \u60a8\u8fd8\u5c06\u770b\u5230\u5982\u4f55\u901a\u8fc7\u68c0\u6d4b\u5ba2\u6237\u7aef\u662f\u5426\u652f\u6301\u5b83\uff0c\u4ece\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684HTTP\u5207\u6362\u5230WebSocket\u534f\u8bae\u3002 \u6211\u4eec\u5c06\u5728\u7b2c13\u7ae0\u4e2d\u603b\u7ed3\u7b2c3\u90e8\u5206\uff0c\u7814\u7a76Netty\u5bf9\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae\uff08UDP\uff09\u7684\u652f\u6301\u3002\u5728\u8fd9\u91cc\uff0c\u60a8\u5c06\u6784\u5efa\u4e00\u4e2a\u5e7f\u64ad\u670d\u52a1\u5668\u548c\u76d1\u89c6\u5668\u5ba2\u6237<\/p>\n","protected":false},"author":1,"featured_media":30579,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[6],"tags":[292,170,390,23,26,21,10,64],"collection":[],"_links":{"self":[{"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/posts\/30596"}],"collection":[{"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/comments?post=30596"}],"version-history":[{"count":0,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/posts\/30596\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/media\/30579"}],"wp:attachment":[{"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/media?parent=30596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/categories?post=30596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/tags?post=30596"},{"taxonomy":"collection","embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/collection?post=30596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}