Documentation
¶
Overview ¶
Package together keeps Watch together sessions: people on different devices, at home or away, watching the same thing at the same moment.
Everything goes through this server. Whoever starts a session gets a short code to share; others join with it. When anyone plays, pauses, seeks or moves on to another title, the session's state changes and every member's player follows. Players wait for changes with Wait, which returns as soon as the state moves past the version they last saw, so a change reaches everyone within one round trip.
The state says where playback was at UpdatedAt and whether it's paused; players work out where it is now from the server's clock (ServerTime in every answer), and seek when they drift too far from it.
Index ¶
- Constants
- Variables
- func NormalizeCode(code string) string
- type Hub
- func (h *Hub) Act(code string, p Person, action string, positionMs, itemID int64) (State, error)
- func (h *Hub) Close()
- func (h *Hub) End(code string, p Person) error
- func (h *Hub) Join(code string, p Person) (State, error)
- func (h *Hub) Leave(code string, p Person) error
- func (h *Hub) Peek(code string) (State, error)
- func (h *Hub) Run(ctx context.Context)
- func (h *Hub) Start(p Person, itemID, positionMs int64, paused bool, resume string) (State, error)
- func (h *Hub) Wait(ctx context.Context, code string, p Person, since int64, wait time.Duration) (State, error)
- type Last
- type Member
- type Person
- type State
Constants ¶
const ( ActionPlay = "play" // carry on playing from PositionMs ActionPause = "pause" // pause at PositionMs ActionSeek = "seek" // jump to PositionMs, keeping play or pause ActionItem = "item" // switch everyone to ItemID, from PositionMs ActionJoin = "join" // someone joined ActionLeave = "leave" // someone left ActionStart = "start" // the session started )
Things people do in a session.
const ( // MaxWait is the longest Wait holds a request. MaxWait = 25 * time.Second )
Variables ¶
var ( ErrNoSession = errors.New("that Watch together code doesn't work; it may have ended") ErrNotMember = errors.New("join the session first") ErrFull = errors.New("this Watch together session is full") ErrTooMany = errors.New("too many Watch together sessions right now") ErrAction = errors.New("unknown action") ErrExists = errors.New("that session is still going; join it") )
Functions ¶
func NormalizeCode ¶
NormalizeCode takes a code as someone typed it.
Types ¶
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub holds every session. The zero value isn't usable; call New.
func (*Hub) Close ¶
func (h *Hub) Close()
Close is the server shutting down: waits return at once, so players hear about the restart and come back to their session afterwards (see Start).
func (*Hub) Run ¶
Run drops members who stopped asking and sessions nobody came back to, until ctx ends.
type Last ¶
type Last struct {
Action string `json:"action"`
By string `json:"by"`
ClientID string `json:"clientId"`
At int64 `json:"at"` // milliseconds
}
Last is the latest thing someone did, for players to show ("Sam paused").
type Member ¶
type Member struct {
ClientID string `json:"clientId"`
UserID int64 `json:"userId"`
Name string `json:"name"` // the person
Device string `json:"device,omitempty"` // such as "Living room TV"
Platform string `json:"platform,omitempty"` // web, ios, android, tvos…
Host bool `json:"host,omitempty"` // started the session
JoinedAt int64 `json:"joinedAt"`
// contains filtered or unexported fields
}
Member is someone's player in a session.
type State ¶
type State struct {
Code string `json:"code"`
Version int64 `json:"version"`
ItemID int64 `json:"itemId"`
PositionMs int64 `json:"positionMs"` // where playback was at UpdatedAt
Paused bool `json:"paused"`
UpdatedAt int64 `json:"updatedAt"` // milliseconds, server clock
ServerTime int64 `json:"serverTime"` // milliseconds, when this was sent
Members []Member `json:"members"`
Last *Last `json:"last,omitempty"`
// Ended means the session is over; players stop following it.
Ended bool `json:"ended,omitempty"`
}
State is a session as members see it.