{"id":85270,"date":"2020-04-01T11:20:42","date_gmt":"2020-04-01T03:20:42","guid":{"rendered":"http:\/\/4563.org\/?p=85270"},"modified":"2020-04-01T11:20:42","modified_gmt":"2020-04-01T03:20:42","slug":"kotlin-%e2%9d%a4%ef%b8%8f-netty-%e6%88%bf%e9%97%b4%e7%b3%bb%e7%bb%9f%e5%b0%8f%e7%bb%83%e4%b9%a0","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=85270","title":{"rendered":"Kotlin \u2764\ufe0f Netty = \u623f\u95f4\u7cfb\u7edf\u5c0f\u7ec3\u4e60"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Kotlin \u2764\ufe0f Netty = \u623f\u95f4\u7cfb\u7edf\u5c0f\u7ec3\u4e60               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dcalsky <\/span>  <span><i><\/i> 11<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>Github \u4ed3\u5e93\u94fe\u63a5: https:\/\/github.com\/dcalsky\/wws-socket<\/p>\n<p>N \u5e74\u524d\u7528 socket.io \u505a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u591a\u4eba\u6e38\u620f\u5bf9\u6218\u670d\u52a1\u5668\uff0c\u5f53\u65f6\u7528\u7684 websocket \u3002\u6700\u8fd1\u53d1\u73b0\u5bf9 socket \u4e86\u89e3\u4e0e\u4f7f\u7528\u5e76\u4e0d\u6df1\u523b\uff0c\u4e8e\u662f\u4ece\u5934\u505a\u4e00\u4e2a\u5c0f\u9879\u76ee\uff0c\u4e00\u6b65\u6b65\u52a0\u6df1\u5bf9 TCP \u6570\u636e\u4f20\u8f93\u7684\u7406\u89e3\u3002<\/p>\n<h1>\u6536\u83b7<\/h1>\n<p> <\/p>\n<ol>\n<li>\u80fd\u591f\u719f\u7ec3\u5730\u62c6\u88c5 tcp frame \uff08\u6240\u8c13\u201c\u7c98\u5305\u534a\u5305\u95ee\u9898\u201d\uff0c\u8fd9\u6837\u8bf4\u6bd4\u8f83\u597d\u7406\u89e3<\/li>\n<li>Bytes \u8bfb\u5199\u5c0f \/\u5927\u7aef\u5e8f(little\/big endian)\u4e86\u89e3<\/li>\n<li>\u5c1d\u8bd5\u4e86\u7528 Kotlin \u8bd5\u8bd5\u4e0d\u540c\u7684 Java \u751f\u6001\uff0c\u5e76\u4e14\u9605\u8bfb\u4e86\u8bb8\u591a netty \u7ecf\u5178 codec \u7684\u4ee3\u7801\uff0c\u53d7\u76ca\u532a\u6d45<\/li>\n<li>\u5c1d\u8bd5\u5b9a\u4e49\u4e86\u7b80\u5355\u7684 packet \u7ed3\u6784\uff0c\u4e0d\u518d\u4f9d\u8d56 json \u6216\u5176\u4ed6\u901a\u7528\u6570\u636e\u683c\u5f0f<\/li>\n<li>Kotlin \u73a9\u4f4d\u64cd\u4f5c\u771f\u7684\u9ebb\u70e6<\/li>\n<\/ol>\n<h1>\u90e8\u5206\u4ecb\u7ecd\uff08\u8be6\u89c1 README<\/h1>\n<p> <\/p>\n<p>The Simplest room IM system made by <strong>Netty<\/strong> TCP. Inspired by <strong>@WWS<\/strong>.<\/p>\n<h2>PackageT<\/h2>\n<p><strong>PackageT<\/strong> is a TCP communication packet structure of WWS-SOCKET that includes two parts: <strong>Header<\/strong> and <strong>Body<\/strong>.<\/p>\n<p>Client use socket to create connection with <strong>server<\/strong> and then send packet based <strong>PackageT<\/strong> to it. Once server validates packet successfully, it will reply a packet back.<\/p>\n<pre><code>                              PackageT                                                                           +---------------------------------------------------------------------+                                           |       20 Bytes                          Unlimited                   |                                           |+---------------------++--------------------------------------------+|                                           ||       Header        ||                    Body                    ||                                           |+---------------------++--------------------------------------------+|                                           +---------------------------------------------------------------------+ <\/code><\/pre>\n<h2>Header<\/h2>\n<p>The header of packageT has fixed 20 bytes including 4 parts. <code>Message Type<\/code> has 2 bytes with uint16 (little-endian), <code>body Length<\/code> has 4 bytes with uint32 (little-endian), <code>Send Time<\/code> has 8 bytes with uint64 (little-endian) and <code>Package Hash<\/code> has 6 bytes.<\/p>\n<pre><code>                                    PackageT Header                                                +--------------------------------------------------------------------------------------+           |    2 Bytes         4 Bytes                  8 Bytes                   6 Bytes        |           | +------------++-----------------+ +-------------------------++---------------------+ |           | |Message Type||   Body Length   | |        Send Time        ||     Package Hash    | |           | +------------++-----------------+ +-------------------------++---------------------+ |           +--------------------------------------------------------------------------------------+                                                                                    <\/code><\/pre>\n<h3>Message Type<\/h3>\n<h3>Body Length<\/h3>\n<p>The byte length of packet body, client\/server should measure body before sending packet.<\/p>\n<h3>Send Time<\/h3>\n<p>Unix millisecond timestamp when the packet sent.<\/p>\n<h3>Hash<\/h3>\n<p>Client generates a unique hash value before sending a packet and attach it to the header of packet. And then server should reply a packet with this hash in the header (except <strong>ROOM_MSG<\/strong> response type).<\/p>\n<h2>Body<\/h2>\n<p>The body structure of PackageT depends on <em>Message Type<\/em>, which means different message type has corresponding body.<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Kotlin \u2764\ufe0f Netty =&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/85270"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85270"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/85270\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85270"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}