Documentation ¶
Overview ¶
Package ws provides a websocket connection that implements io.ReadWriteCloser and some handy callbacks for managing websocket connections, as well as an http.Handler for automatically upgrading HTTP requests to WebSockets
Index ¶
Constants ¶
const ( // ModeBinary causes Conn.Write to write binary messages. ModeBinary = websocket.BinaryMessage // ModeText causes Conn.Write to write text messages. ModeText = websocket.TextMessage )
Variables ¶
var ( // ErrWriteTimeout means that outbox was blocking for longer than Conn.WriteTimeout ErrWriteTimeOut = errors.New("write timed out") )
Functions ¶
Types ¶
type Conn ¶
type Conn struct { Conn *websocket.Conn // ResponseHeader can be modified eg. in OnConnect to be included in the initial http response ResponseHeader http.Header // PingInterval is how often we send a ping frame to make sure someone is still listening PingInterval time.Duration // PongTimeout is how long after sending a ping frame we will wait for a pong frame before closing the connection PongTimeout time.Duration MaxMessageSize int64 WriteTimeout time.Duration // contains filtered or unexported fields }
Conn exposes per-socket connection configs, and implements io.ReadWriteCloser TODO - implement net.Conn?
func (*Conn) Read ¶
Read implements io.Reader. If type information is needed, you can use ReadMessage. Both methods consume the same stream and should probably not be used together. WARNING: Read should only be used by ws.Handler that does not expose OnMessage Read will block indefinitely by default if ws.Server is implemented
func (*Conn) ReadMessage ¶
ReadMessage is an alternative to Read and Server.OnMessage that provides websocket messagetype information. Blocks until message is available for reading.
func (*Conn) SetMessageMode ¶
SetMessageMode configures the types of messages that Conn will send with Write. This has no bearning on messages gotten from Read. Valid values for m are ws.ModeBinary or ws.ModeText
func (*Conn) Write ¶
Write sends a message over the underlying websocket connection. Safe for concurrent use. The message type will be equal to c.messageType which can be configured with c.SetMessageMode. If writing both types is required, Conn.Conn().WriteMessage is available Implements io.ReadWriteCloser
type Connector ¶
type Connector interface { // Conn and SetConn expose the underlying connection Conn() *Conn SetConn(*Conn) // callbacks OnConnect(*http.Request) error OnOpen() error OnClose(wasClean bool, code int, reason error) error }
Connector handles Websocket connections by calling back on Connect, Open, and Close
type Handler ¶
type Handler struct { // Set options on Upgrader to configure Upgrader *websocket.Upgrader // default options will be passed on to each Server yielded by this Handler // PingInterval is how often we send a ping frame to make sure someone is still listening PingInterval time.Duration // PongTimeout is how long after sending a ping frame we will wait for a pong frame before closing the connection PongTimeout time.Duration MaxMessageSize int64 WriteTimeout time.Duration // ReadBufferSize determines the buffer size of the inbox channel // The purpose of the read buffer is to detect instances that are not consuming the // read buffer if used. Increase this if the buffer is filling faster than you can // consume it. ReadBufferSize int // contains filtered or unexported fields }
Handler is an http.Handler that creates and spits out new WS connections
func NewHandler ¶
NewHandler returns a ready-to-use-or-configure Handler
func (*Handler) AllowAnyOrigin ¶
func (h *Handler) AllowAnyOrigin()
AllowAnyOrigin causes the Conn not to reject any connection attempts based on origin
type Server ¶
type Server interface { Connector // OnMessage is called each time a message is received on the Conn if implemented. // If implemented, Read should not be called as it will not receive the messages // and will instead block indefinitely. // TODO allow configuration to use both? OnMessage(payload []byte, isBinary bool) error }
Server serves Websocket connections by calling back on Connect, Open, Message, and Close