Documentation ¶
Index ¶
- Constants
- Variables
- func ReleaseFrame(fr *Frame)
- func UpgradeAsClient(c net.Conn, url string, r *fasthttp.Request) error
- type Client
- func ClientWithHeaders(c net.Conn, url string, req *fasthttp.Request) (*Client, error)
- func Dial(url string) (*Client, error)
- func DialContext(url string, dialer *net.Dialer) (*Client, error)
- func DialTLS(url string, cnf *tls.Config) (*Client, error)
- func DialWithHeaders(url string, req *fasthttp.Request) (*Client, error)
- func MakeClient(c net.Conn, url string) (*Client, error)
- type CloseHandler
- type Code
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) CloseDetail(status StatusCode, reason string)
- func (c *Conn) ID() uint64
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) Ping(data []byte)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SetUserValue(key string, value interface{})
- func (c *Conn) UserValue(key string) interface{}
- func (c *Conn) Write(data []byte) (int, error)
- func (c *Conn) WriteFrame(fr *Frame)
- type Error
- type ErrorHandler
- type Frame
- func (fr *Frame) Code() Code
- func (fr *Frame) CopyTo(fr2 *Frame)
- func (fr *Frame) HasRSV1() bool
- func (fr *Frame) HasRSV2() bool
- func (fr *Frame) HasRSV3() bool
- func (fr *Frame) IsClose() bool
- func (fr *Frame) IsContinuation() bool
- func (fr *Frame) IsControl() bool
- func (fr *Frame) IsFin() bool
- func (fr *Frame) IsMasked() bool
- func (fr *Frame) IsPing() bool
- func (fr *Frame) IsPong() bool
- func (fr *Frame) Len() (length uint64)
- func (fr *Frame) Mask()
- func (fr *Frame) MaskKey() []byte
- func (fr *Frame) Payload() []byte
- func (fr *Frame) PayloadLen() int
- func (fr *Frame) PayloadSize() uint64
- func (fr *Frame) ReadFrom(rd io.Reader) (int64, error)
- func (fr *Frame) Reset()
- func (fr *Frame) SetBinary()
- func (fr *Frame) SetClose()
- func (fr *Frame) SetCode(code Code)
- func (fr *Frame) SetContinuation()
- func (fr *Frame) SetFin()
- func (fr *Frame) SetMask(b []byte)
- func (fr *Frame) SetPayload(b []byte)
- func (fr *Frame) SetPayloadSize(size uint64)
- func (fr *Frame) SetPing()
- func (fr *Frame) SetPong()
- func (fr *Frame) SetRSV1()
- func (fr *Frame) SetRSV2()
- func (fr *Frame) SetRSV3()
- func (fr *Frame) SetStatus(status StatusCode)
- func (fr *Frame) SetText()
- func (fr *Frame) Status() (status StatusCode)
- func (fr *Frame) String() string
- func (fr *Frame) Unmask()
- func (fr *Frame) UnsetMask()
- func (fr *Frame) Write(b []byte) (int, error)
- func (fr *Frame) WriteTo(wr io.Writer) (n int64, err error)
- type FrameHandler
- type MessageHandler
- type OpenHandler
- type PingHandler
- type PongHandler
- type RequestHandler
- type Server
- func (s *Server) HandleClose(closeHandler CloseHandler)
- func (s *Server) HandleData(msgHandler MessageHandler)
- func (s *Server) HandleError(errHandler ErrorHandler)
- func (s *Server) HandleFrame(frameHandler FrameHandler)
- func (s *Server) HandleOpen(openHandler OpenHandler)
- func (s *Server) HandlePing(pingHandler PingHandler)
- func (s *Server) HandlePong(pongHandler PongHandler)
- func (s *Server) NetUpgrade(resp http.ResponseWriter, req *http.Request)
- func (s *Server) Upgrade(ctx *fasthttp.RequestCtx)
- type StatusCode
- type UpgradeHandler
- type UpgradeNetHandler
Constants ¶
const ( // StatusNone is used to let the peer know nothing happened. StatusNone StatusCode = 1000 // StatusGoAway peer's error. StatusGoAway = 1001 // StatusProtocolError problem with the peer's way to communicate. StatusProtocolError = 1002 // StatusNotAcceptable when a request is not acceptable StatusNotAcceptable = 1003 // StatusReserved when a reserved field have been used StatusReserved = 1004 // StatusNotConsistent IDK StatusNotConsistent = 1007 // StatusViolation a violation of the protocol happened StatusViolation = 1008 // StatusTooBig payload bigger than expected StatusTooBig = 1009 // StatuseExtensionsNeeded IDK StatuseExtensionsNeeded = 1010 // StatusUnexpected IDK StatusUnexpected = 1011 )
const DefaultPayloadSize = 1 << 20
DefaultPayloadSize defines the default payload size (when none was defined).
Variables ¶
var ( // ErrCannotUpgrade shows up when an error occurred when upgrading a connection. ErrCannotUpgrade = errors.New("cannot upgrade connection") )
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client holds a WebSocket connection.
The client is NOT concurrently safe. It is intended to be used with the Frame struct.
func ClientWithHeaders ¶
ClientWithHeaders returns a Conn using an existing connection and sending custom headers.
func Dial ¶
Dial establishes a websocket connection as client.
url parameter must follow the WebSocket URL format i.e. ws://host:port/path
func DialContext ¶
Dial with context
func DialTLS ¶
DialTLS establishes a websocket connection as client with the tls.Config. The config will be used if the URL is wss:// like.
func DialWithHeaders ¶
DialWithHeaders establishes a websocket connection as client sending a personalized request.
func MakeClient ¶
MakeClient returns Conn using an existing connection.
url must be a complete URL format i.e. http://localhost:8080/ws
func (*Client) Write ¶
Write writes the content `b` as text.
To send binary content use WriteBinary.
func (*Client) WriteBinary ¶
WriteBinary writes the content `b` as binary.
To send text content use Write.
type CloseHandler ¶
CloseHandler fires when a connection has been closed.
type Code ¶
type Code uint8
Code to send.
const ( // CodeContinuation defines the continuation code CodeContinuation Code = 0x0 // CodeText defines the text code CodeText Code = 0x1 // CodeBinary defines the binary code CodeBinary Code = 0x2 // CodeClose defines the close code CodeClose Code = 0x8 // CodePing defines the ping code CodePing Code = 0x9 // CodePong defines the pong code CodePong Code = 0xA )
type Conn ¶
type Conn struct { // ReadTimeout ... ReadTimeout time.Duration // WriteTimeout ... WriteTimeout time.Duration // MaxPayloadSize prevents huge memory allocation. // // By default MaxPayloadSize is DefaultPayloadSize. MaxPayloadSize uint64 // contains filtered or unexported fields }
Conn represents a WebSocket connection on the server side.
This handler is compatible with io.Writer.
func (*Conn) CloseDetail ¶
func (c *Conn) CloseDetail(status StatusCode, reason string)
func (*Conn) RemoteAddr ¶
RemoteAddr returns peer remote address.
func (*Conn) SetUserValue ¶
SetUserValue assigns a key to the given value
func (*Conn) WriteFrame ¶
type Error ¶
type Error struct { Status StatusCode Reason string }
type ErrorHandler ¶
ErrorHandler fires when an unknown error happens.
type Frame ¶
type Frame struct {
// contains filtered or unexported fields
}
Frame is the unit used to transfer message between endpoints using the websocket protocol.
func (*Frame) IsContinuation ¶
IsContinuation returns true if the Frame code is Continuation
func (*Frame) IsControl ¶
IsControl returns whether the Frame is a control frame or not. That means if it's a Close, Ping or Pong frame.
func (*Frame) Len ¶
Len returns the length of the payload based on the header bits.
If you want to know the actual payload length use #PayloadLen
func (*Frame) PayloadLen ¶
PayloadLen returns the actual payload length
func (*Frame) PayloadSize ¶
PayloadSize returns the max payload size
func (*Frame) SetContinuation ¶
func (fr *Frame) SetContinuation()
SetContinuation sets CodeContinuation in Code field.
func (*Frame) SetPayload ¶
SetPayload sets the parsed bytes as frame's payload
func (*Frame) SetPayloadSize ¶
SetPayloadSize sets max payload size
func (*Frame) SetStatus ¶
func (fr *Frame) SetStatus(status StatusCode)
SetStatus sets status code.
Status code is usually used in Close request.
func (*Frame) Unmask ¶
func (fr *Frame) Unmask()
Unmask performs the unmasking of the current payload
type FrameHandler ¶
FrameHandler receives the raw frame. This handler is optional, if none is specified the server will run a default handler.
If the user specifies a FrameHandler, then it is going to receive all incoming frames.
type MessageHandler ¶
MessageHandler receives the payload content of a data frame indicating whether the content is binary or not.
type PingHandler ¶
PingHandler handles the data from a ping frame.
type PongHandler ¶
PongHandler receives the data from a pong frame.
type RequestHandler ¶
type RequestHandler func(conn *Conn)
RequestHandler is the websocket connection handler.
type Server ¶
type Server struct { // UpgradeHandler allows the user to handle RequestCtx when upgrading for fasthttp. // // If UpgradeHandler returns false the connection won't be upgraded. UpgradeHandler UpgradeHandler // UpgradeHandler allows the user to handle the request when upgrading for net/http. // // If UpgradeNetHandler returns false, the connection won't be upgraded. UpgradeNetHandler UpgradeNetHandler // Protocols are the supported protocols. Protocols []string // Origin is used to limit the clients coming from the defined origin Origin string // contains filtered or unexported fields }
Server represents the WebSocket server.
Server is going to be in charge of upgrading the connection, is not a server per-se.
func (*Server) HandleClose ¶
func (s *Server) HandleClose(closeHandler CloseHandler)
HandleClose sets a callback for handling connection close.
func (*Server) HandleData ¶
func (s *Server) HandleData(msgHandler MessageHandler)
HandleData sets the MessageHandler.
func (*Server) HandleFrame ¶
func (s *Server) HandleFrame(frameHandler FrameHandler)
HandleFrame sets a callback for handling all the incoming Frames.
If none is specified, the server will run a default handler.
func (*Server) HandleOpen ¶
func (s *Server) HandleOpen(openHandler OpenHandler)
HandleOpen sets a callback for handling opening connections.
func (*Server) HandlePing ¶
func (s *Server) HandlePing(pingHandler PingHandler)
HandlePing sets a callback for handling the data of the ping frames.
The server is in charge of replying to the PING frames, thus the client MUST not reply to any control frame.
func (*Server) HandlePong ¶
func (s *Server) HandlePong(pongHandler PongHandler)
HandlePong sets a callback for handling the data of the pong frames.
func (*Server) NetUpgrade ¶
func (s *Server) NetUpgrade(resp http.ResponseWriter, req *http.Request)
NetUpgrade upgrades the websocket connection for net/http.
func (*Server) Upgrade ¶
func (s *Server) Upgrade(ctx *fasthttp.RequestCtx)
Upgrade upgrades websocket connections.
type StatusCode ¶
type StatusCode uint16
StatusCode is sent when closing a connection.
The following constants have been defined by the RFC.
func (StatusCode) String ¶
func (status StatusCode) String() string
type UpgradeHandler ¶
type UpgradeHandler func(*fasthttp.RequestCtx) bool
UpgradeHandler is a middleware callback that determines whether the WebSocket connection should be upgraded or not. If UpgradeHandler returns false, the connection is not upgraded.
type UpgradeNetHandler ¶
type UpgradeNetHandler func(resp http.ResponseWriter, req *http.Request) bool
UpgradeNetHandler is like UpgradeHandler but for net/http.