V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
dcalsky
V2EX  ›  分享创造

Kotlin ❤️ Netty = 房间系统小练习

  •  
  •   dcalsky · 2020-05-05 19:32:56 +08:00 · 2343 次点击
    这是一个创建于 1450 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Github 仓库链接: https://github.com/dcalsky/wws-socket

    N 年前用 socket.io 做了一个简单的多人游戏对战服务器,当时用的 websocket 。最近发现对 socket 了解与使用并不深刻,于是从头做一个小项目,一步步加深对 TCP 数据传输的理解。

    收获

    1. 能够熟练地拆装 tcp frame (所谓“粘包半包问题”,这样说比较好理解
    2. Bytes 读写小 /大端序(little/big endian)了解
    3. 尝试了用 Kotlin 试试不同的 Java 生态,并且阅读了许多 netty 经典 codec 的代码,受益匪浅
    4. 尝试定义了简单的 packet 结构,不再依赖 json 或其他通用数据格式
    5. Kotlin 玩位操作真的麻烦

    部分介绍(详见 README

    The Simplest room IM system made by Netty TCP. Inspired by @WWS.

    PackageT

    PackageT is a TCP communication packet structure of WWS-SOCKET that includes two parts: Header and Body.

    Client use socket to create connection with server and then send packet based PackageT to it. Once server validates packet successfully, it will reply a packet back.

                                  PackageT                                                                          
    +---------------------------------------------------------------------+                                          
    |       20 Bytes                          Unlimited                   |                                          
    |+---------------------++--------------------------------------------+|                                          
    ||       Header        ||                    Body                    ||                                          
    |+---------------------++--------------------------------------------+|                                          
    +---------------------------------------------------------------------+
    

    Header

    The header of packageT has fixed 20 bytes including 4 parts. Message Type has 2 bytes with uint16 (little-endian), body Length has 4 bytes with uint32 (little-endian), Send Time has 8 bytes with uint64 (little-endian) and Package Hash has 6 bytes.

                                        PackageT Header                                               
    +--------------------------------------------------------------------------------------+          
    |    2 Bytes         4 Bytes                  8 Bytes                   6 Bytes        |          
    | +------------++-----------------+ +-------------------------++---------------------+ |          
    | |Message Type||   Body Length   | |        Send Time        ||     Package Hash    | |          
    | +------------++-----------------+ +-------------------------++---------------------+ |          
    +--------------------------------------------------------------------------------------+                                                                                   
    

    Message Type

    Body Length

    The byte length of packet body, client/server should measure body before sending packet.

    Send Time

    Unix millisecond timestamp when the packet sent.

    Hash

    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 ROOM_MSG response type).

    Body

    The body structure of PackageT depends on Message Type, which means different message type has corresponding body.

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5430 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:09 · PVG 15:09 · LAX 00:09 · JFK 03:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.