4.3 Lib v0.3 组件图
由上图可以看出,Lib 主要由 protobuf 定义(packet.proto) 和 commons 通用组件组成。客户端与服务器通信采用二进制协议,并采用 protobuf 序列化/反序列化协议。
通信协议
+--------+----------------------+
| length | packet data |
+--------+----------------------+
Protobuf 定义
syntax = "proto3";
package lib;
option go_package = "github.com/huoyijie/GoChat/lib";
enum PackKind {
// Server
PONG = 0;
ERR = 1;
RES = 2;
PUSH = 3;
// All
MSG = 4;
// Client
PING = 5;
SIGNUP = 6;
SIGNIN = 7;
TOKEN = 8;
SIGNOUT = 9;
USERS = 10;
}
message Packet {
PackKind kind = 1;
uint64 id = 2;
bytes data = 3;
}
message Ping{
bytes payload = 1;
}
message Pong{
bytes payload = 1;
}
message Auth {
string username = 1;
bytes passhash = 2;
}
message Signup {
Auth auth = 1;
}
message Signin {
Auth auth = 1;
}
message Token {
bytes token = 1;
}
message TokenRes {
int32 code = 1;
uint64 id = 2;
string username = 3;
bytes token = 4;
}
message Signout {}
message SignoutRes {
int32 code = 1;
}
message User {
string username = 1;
bool online = 2;
}
message Users {}
message UsersRes {
int32 code = 1;
repeated User users = 2;
}
enum MsgKind {
TEXT = 0;
}
message Msg {
int64 id = 1;
MsgKind kind = 2;
string from = 3;
string to = 4;
bytes data = 5;
}
message ErrRes {
int32 code = 1;
}
enum PushKind {
ONLINE = 0;
}
message Push {
PushKind kind = 1;
bytes data = 2;
}
enum OnlineKind {
ON = 0;
OFF = 1;
}
message Online {
OnlineKind kind = 1;
string username = 2;
}
Commons
主要是封装一些工具方法和错误码定义,如编解码、加解密等。