Documentation
¶
Overview ¶
Package protocol hand-ports the NG Chat wire types from the server's Zod schemas (ngchat repo, src/shared/protocol). The server validates client messages with strict schemas — unknown fields are rejected — so the structs in this file must stay exactly in sync with the server. Server messages are decoded tolerantly instead (see server.go), so server-side additions never break this client.
Index ¶
- func Decode(data []byte) any
- type Authenticate
- type Authenticated
- type ChannelID
- type ChatMessage
- type Error
- type GetChannelID
- type IdleTimeout
- type Kicked
- type Message
- type Ping
- type PingData
- type Pong
- type Subscribe
- type Subscribed
- type Typing
- type TypingEvent
- type Unauthorized
- type Unknown
- type Unsubscribe
- type Unsubscribed
- type UserJoined
- type UserLeft
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Authenticate ¶
Authenticate must be the first message sent on a new socket. Only Authenticate and Ping are accepted pre-auth; the server closes unauthenticated sockets after 15s.
func NewAuthenticate ¶
func NewAuthenticate(token string) Authenticate
NewAuthenticate builds an authenticate message for the given chat JWT.
type Authenticated ¶
type Authenticated struct {
ClientHash string `json:"clientHash"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
IsSiteMod bool `json:"isSiteMod"`
MOTD json.RawMessage `json:"motd"`
ServerTime int64 `json:"serverTime"`
UserID int `json:"userID"`
Username string `json:"username"`
}
Authenticated confirms a successful authenticate and describes the user.
func (Authenticated) MOTDText ¶
func (a Authenticated) MOTDText() string
MOTDText returns the message of the day when it is a plain string, otherwise "".
type ChatMessage ¶
type ChatMessage struct {
Name string `json:"name"`
ChannelID int `json:"channelID"`
Message string `json:"message"`
}
ChatMessage sends a chat line (1..5000 chars) to a subscribed channel. Slash commands (/me, /dm, ...) are plain message text parsed server-side.
func NewChatMessage ¶
func NewChatMessage(channelID int, text string) ChatMessage
NewChatMessage builds a message send for a channel.
type Error ¶
type Error struct {
Message string `json:"message"`
RedirectChannel string `json:"redirectChannel"`
}
Error is a generic request failure; RedirectChannel is set when a channel lookup failed and the server suggests another channel.
type GetChannelID ¶
GetChannelID resolves a channel name to its numeric ID. Unknown names come back as an Error with a RedirectChannel.
func NewGetChannelID ¶
func NewGetChannelID(channelName string) GetChannelID
NewGetChannelID builds a channel-name lookup. Names are lowercase on the server; callers should lowercase before sending.
type IdleTimeout ¶
type IdleTimeout struct{}
IdleTimeout precedes a disconnect for 24h without chatting; the close reason "idle timeout" must not trigger auto-reconnect.
type Kicked ¶
type Kicked struct {
Message string `json:"message"`
}
Kicked precedes a server-initiated disconnect of this user.
type Message ¶
type Message struct {
Name string `json:"name"`
ChannelID int `json:"channelID"`
ID *int64 `json:"id"`
Message string `json:"message"`
MessageRaw string `json:"messageRaw"`
Mentions []string `json:"mentions"`
IsSpoiler bool `json:"isSpoiler"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
UserIcon string `json:"userIcon"`
UserID int `json:"userID"`
Username string `json:"username"`
UserPageURL string `json:"userPageURL"`
ServerTime int64 `json:"serverTime"`
}
Message is a chat line. It also covers the meMessage, slapMessage, serverMessage and directMessage variants, which share its shape; check Name to tell them apart. Message is server-rendered HTML; MessageRaw is the sender's original text.
type Ping ¶
Ping is the application-level heartbeat. The server terminates any socket with no inbound traffic for 15 seconds, so clients must ping continuously (the browser client uses a 1-5s cadence).
type PingData ¶
type PingData struct {
Time int64 `json:"time"`
}
PingData carries the client timestamp echoed back in Pong.
type Pong ¶
type Pong struct {
Data PingData `json:"data"`
}
Pong answers Ping, echoing the client timestamp.
type Subscribe ¶
Subscribe joins a channel by ID; the reply is Subscribed with the backfill buffer and user list.
func NewSubscribe ¶
NewSubscribe builds a channel subscription.
type Subscribed ¶
type Subscribed struct {
ChannelID int `json:"channelID"`
MessageBuffer []json.RawMessage `json:"messageBuffer"`
UserList json.RawMessage `json:"userList"`
Notifications json.RawMessage `json:"notifications"`
ServerTime int64 `json:"serverTime"`
}
Subscribed answers Subscribe. MessageBuffer holds up to 25 recent events (same envelopes as live ones — decode each with Decode); it is the only history a client ever receives.
type TypingEvent ¶
type TypingEvent struct {
ChannelID int `json:"channelID"`
UserID int `json:"userID"`
Username string `json:"username"`
}
TypingEvent reports another user composing in a channel.
type Unauthorized ¶
type Unauthorized struct {
}
Unauthorized reports an auth failure. "jwt expired"/"jwt malformed" are soft failures: re-mint and re-send Authenticate on the same socket.
type Unknown ¶
type Unknown struct {
Name string
}
Unknown is any frame this client does not (yet) understand, including known names whose payload failed to decode. Callers must treat it as ignorable — that tolerance is what lets old binaries survive protocol additions (ADR 0002).
type Unsubscribe ¶
Unsubscribe leaves a channel.
func NewUnsubscribe ¶
func NewUnsubscribe(channelID int) Unsubscribe
NewUnsubscribe builds a channel unsubscription.
type Unsubscribed ¶
type Unsubscribed struct {
ChannelID int `json:"channelID"`
}
Unsubscribed answers Unsubscribe.