Documentation
¶
Overview ¶
Package chat is text chat between a viewer and the person at the host, during a session. docs/design/session-chat.md is the design.
Wire protocol ¶
Service "chat", in feature "chat". The viewer opens it with {"v": 1}. The host answers {"hello": {"max": MaxRunes}} or {"error": CODE} (and closes). Then, in any order, on the same stream:
- viewer to host: {"id": N, "text": "..."}; the host answers {"ack": N} or {"ack": N, "error": CODE}.
- host to viewer: {"msg": {"text": "...", "at": UNIX_MS}}.
- host to viewer: {"error": "chat_off"} before the host ends the stream because chat was switched off.
Every message is a wire JSON message of at most MaxMessage bytes. Each end checks the other's text with Clean; message text is never logged or written to disk.
Index ¶
- Constants
- Variables
- func Clean(s string) (string, error)
- func Code(err error) string
- type Client
- type Hub
- func (h *Hub) Handler() peer.Handler
- func (h *Hub) MarkRead(session int) error
- func (h *Hub) Messages(session int, after uint64) ([]Message, error)
- func (h *Hub) OnMessage(fn func(t Thread, m Message)) (stop func())
- func (h *Hub) Send(session int, text string) (Message, error)
- func (h *Hub) Subscribe() (<-chan struct{}, func())
- func (h *Hub) Summaries() []Thread
- func (h *Hub) Threads() []Thread
- type Incoming
- type Message
- type RefusedError
- type Thread
Constants ¶
const ( FromViewer = "viewer" FromHost = "host" )
From says who wrote a message.
const ( // MaxRunes is the most characters (code points) one message may have. MaxRunes = 2000 // MaxMessage bounds one JSON message on a chat stream. MaxMessage = 16 << 10 )
const ( CodeEmpty = "empty" CodeTooLong = "too_long" CodeBadUTF8 = "bad_utf8" CodeTooFast = "too_fast" CodeOff = "chat_off" CodeBadRequest = "bad_request" )
Codes a host answers with.
const Service = "chat"
Service is the stream service name.
Variables ¶
var ( ErrNoThread = errors.New("chat: no such session") ErrNotListening = errors.New("chat: that viewer has no chat open") )
Errors of Hub.Send.
var ( ErrEmpty = errors.New("chat: the message is empty") ErrTooLong = errors.New("chat: the message is too long") ErrBadUTF8 = errors.New("chat: the message is not valid UTF-8 text") ErrTooFast = errors.New("chat: too many messages, wait a moment") ErrOff = errors.New("chat: the host's owner switched chat off") ErrUnsupported = errors.New("chat: the host does not offer chat") ErrClosed = errors.New("chat: the conversation has ended") )
Errors for the codes, and for a host without chat.
var Default = NewHub()
Default is the hub of this process's host, served as the "chat" service.
Functions ¶
func Clean ¶
Clean checks and tidies message text: it must be valid UTF-8 (and not contain U+FFFD, which a JSON decoder puts where invalid bytes were); line breaks become "\n"; other control characters and format characters (bidirectional overrides and the like, but not the zero-width joiner and non-joiner) are removed; white space around it is trimmed. What is left must not be empty or longer than MaxRunes characters.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the viewer's end of a chat stream.
func Open ¶
Open starts chat with the host on c. It fails with ErrOff when the host's owner switched chat off (or did not allow it for this session), and ErrUnsupported when the host has no chat.
func (*Client) Done ¶
func (c *Client) Done() <-chan struct{}
Done is closed when the stream has ended; Err then says why.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub holds a host's conversations, in memory only.
func (*Hub) OnMessage ¶
OnMessage calls fn for every message a viewer sends, after the hub took it, with the thread as it is then (without its messages). fn must be quick. stop ends it.
func (*Hub) Subscribe ¶
func (h *Hub) Subscribe() (<-chan struct{}, func())
Subscribe returns a channel that receives whenever a thread changed, and a function that ends the subscription.
type Message ¶
type Message struct {
// N numbers the hub's messages from 1, across threads.
N uint64 `json:"n"`
From string `json:"from"`
Text string `json:"text"`
// At is when the hub took it, in Unix milliseconds.
At int64 `json:"at"`
}
Message is one message of a thread.
type RefusedError ¶
type RefusedError struct{ Code string }
RefusedError is a refusal with a code this version does not know.
func (*RefusedError) Error ¶
func (e *RefusedError) Error() string
type Thread ¶
type Thread struct {
// Session is the host's ID of the viewer's session.
Session int `json:"session"`
Name string `json:"name"`
// Open: the viewer has a chat stream open, so it can be written to.
Open bool `json:"open"`
// Ended: the session is over.
Ended bool `json:"ended"`
// Unread counts the viewer's messages the person at the host has not
// marked read (MarkRead).
Unread int `json:"unread"`
// Last and LastAt are the number and time of the newest message kept
// (0 when there is none).
Last uint64 `json:"last"`
LastAt int64 `json:"last_at"`
// Messages is left out of Summaries (see Hub.Messages).
Messages []Message `json:"messages,omitempty"`
}
Thread is the conversation with one viewer session.