Files

34 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package protocol
// MessageType 表示一条消息的传输类型。
// v1 只区分普通文本和文件两类负载。
type MessageType string
const (
// MessageTypeText 表示正文按 UTF-8 文本解释,天然兼容 ASCII。
MessageTypeText MessageType = "text"
// MessageTypeFile 表示正文是原始文件字节。
MessageTypeFile MessageType = "file"
// MessageTypeRegister 表示 peer 向 server 显式注册自己的身份。
MessageTypeRegister MessageType = "register"
// MessageTypeError 表示 server 向 peer 返回错误信息。
MessageTypeError MessageType = "error"
)
// ServerPeerID 是协议中约定的 server 端固定标识。
const ServerPeerID = "server"
// Message 是 peer 和 server 共用的传输消息结构。
// 头部元信息会被编码为 JSONBody 则作为原始字节拼接在头部之后。
type Message struct {
Type MessageType `json:"type"` // 消息类型,只允许 text 或 file。
ID uint64 `json:"id"` // 由发送方生成,用于追踪消息。
From string `json:"from"` // 发送方标识。
To string `json:"to"` // 接收方标识。
// FileName 仅在 Type 为 file 时使用。
FileName string `json:"file_name,omitempty"`
// Body 是真正传输的正文内容,不进入头部 JSON。
Body []byte `json:"-"`
}