Kotlin ❤️ Netty = 房间系统小练习
Github 仓库链接: https://github.com/dcalsky/wws-socket
N 年前用 socket.io 做了一个简单的多人游戏对战服务器,当时用的 websocket 。最近发现对 socket 了解与使用并不深刻,于是从头做一个小项目,一步步加深对 TCP 数据传输的理解。
收获
- 能够熟练地拆装 tcp frame (所谓“粘包半包问题”,这样说比较好理解
- Bytes 读写小 /大端序(little/big endian)了解
- 尝试了用 Kotlin 试试不同的 Java 生态,并且阅读了许多 netty 经典 codec 的代码,受益匪浅
- 尝试定义了简单的 packet 结构,不再依赖 json 或其他通用数据格式
- 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.