Documentation ¶
Index ¶
- Constants
- Variables
- func NetUpgrade(handler RequestHandler) func(http.ResponseWriter, *http.Request)
- func ReleaseFrame(fr *Frame)
- func Upgrade(handler RequestHandler) func(ctx *fasthttp.RequestCtx)
- func UpgradeAsClient(c net.Conn, url string, r *fasthttp.Request) error
- type Code
- type Conn
- func Client(c net.Conn, url string) (*Conn, error)
- func ClientWithHeaders(c net.Conn, url string, req *fasthttp.Request) (*Conn, error)
- func Dial(url string) (*Conn, error)
- func DialTLS(url string, cnf *tls.Config) (*Conn, error)
- func DialWithHeaders(url string, req *fasthttp.Request) (*Conn, error)
- func DialWithHeadersAndCustomDialer(url string, req *fasthttp.Request, customDialer *net.Dialer) (*Conn, error)
- func (conn *Conn) Close() error
- func (conn *Conn) CloseString(b string) error
- func (conn *Conn) LocalAddr() net.Addr
- func (conn *Conn) NextFrame() (fr *Frame, err error)
- func (conn *Conn) ReadFrame(fr *Frame) (nn int, err error)
- func (conn *Conn) ReadFull(b []byte, fr *Frame) ([]byte, error)
- func (conn *Conn) ReadMessage(b []byte) (Mode, []byte, error)
- func (conn *Conn) RemoteAddr() net.Addr
- func (conn *Conn) ReplyClose(fr *Frame) (err error)
- func (conn *Conn) Reset(c net.Conn)
- func (conn *Conn) SendCode(code Code, status StatusCode, b []byte) error
- func (conn *Conn) SendCodeString(code Code, status StatusCode, b string) error
- func (conn *Conn) SetUserValue(key string, value interface{})
- func (conn *Conn) UserValue(key string) interface{}
- func (conn *Conn) Write(b []byte) (int, error)
- func (conn *Conn) WriteFrame(fr *Frame) (int, error)
- func (conn *Conn) WriteMessage(mode Mode, b []byte) (int, error)
- func (conn *Conn) WriteString(b string) (int, error)
- 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) Mode() (mode Mode)
- 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 Mode
- type NetUpgradeHandler
- type NetUpgrader
- type RequestHandler
- type StatusCode
- type UpgradeHandler
- type Upgrader
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 << 22
DefaultPayloadSize defines the default payload size (when none was defined).
Variables ¶
var ( // EOF represents an io.EOF error. EOF = io.EOF )
var ( // ErrCannotUpgrade shows up when an error ocurred when upgrading a connection. ErrCannotUpgrade = errors.New("cannot upgrade connection") )
Functions ¶
func NetUpgrade ¶
func NetUpgrade(handler RequestHandler) func(http.ResponseWriter, *http.Request)
NetUpgrade returns a RequestHandler for net/http doing the upgrading process easier.
func Upgrade ¶
func Upgrade(handler RequestHandler) func(ctx *fasthttp.RequestCtx)
Upgrade returns a RequestHandler for fasthttp doing the upgrading process easier.
Types ¶
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 { // Mode indicates Write default mode. Mode Mode // 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 websocket connection handler.
This handler is compatible with io.Reader, io.ReaderFrom, io.Writer, io.WriterTo
func Client ¶
Client returns Conn using existing connection.
url must be complete URL format i.e. http://localhost:8080/ws
func ClientWithHeaders ¶
ClientWithHeaders returns a Conn using existing connection and sending personalized headers.
func Dial ¶
Dial establishes a websocket connection as client.
url parameter must follow WebSocket URL format i.e. ws://host:port/path
func DialTLS ¶
DialTLS establishes a websocket connection as client with the parsed 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 (*Conn) CloseString ¶
CloseString sends b as close reason and closes the descriptor.
When connection is handled by server the connection is closed automatically.
func (*Conn) NextFrame ¶
NextFrame reads next connection frame and returns if there were no error.
If NextFrame fr is not nil do not forget to ReleaseFrame(fr) This function responds automatically to PING and PONG messages.
func (*Conn) ReadFull ¶
ReadFull will read the parsed frame fully and writing the payload into b.
This function responds automatically to PING and PONG messages.
func (*Conn) ReadMessage ¶
ReadMessage reads next message from conn and returns the mode, b and/or error.
b is used to avoid extra allocations and can be nil.
This function responds automatically to PING and PONG messages.
func (*Conn) RemoteAddr ¶
RemoteAddr returns peer remote address.
func (*Conn) ReplyClose ¶
ReplyClose is used to reply to CodeClose.
func (*Conn) SendCode ¶
func (conn *Conn) SendCode(code Code, status StatusCode, b []byte) error
SendCode writes code, status and message to conn.
status is used by CodeClose to report any close status (as HTTP responses). Can be 0. b can be nil.
func (*Conn) SendCodeString ¶
func (conn *Conn) SendCodeString(code Code, status StatusCode, b string) error
SendCodeString writes code, status and message to conn as SendCode does.
func (*Conn) SetUserValue ¶
SetUserValue assigns a key to the given value
func (*Conn) WriteFrame ¶
WriteFrame writes fr to the connection endpoint.
func (*Conn) WriteMessage ¶
WriteMessage writes b to conn using mode.
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 NetUpgradeHandler ¶
type NetUpgradeHandler func(resp http.ResponseWriter, req *http.Request) bool
NetUpgradeHandler is the upgrading handler for net/http.
type NetUpgrader ¶
type NetUpgrader struct { // UpgradeHandler allows user to handle RequestCtx when upgrading. // // If UpgradeHandler returns false the connection won't be upgraded and // the parsed ctx will be used as a response. UpgradeHandler NetUpgradeHandler // Handler is the request handler for ws connections. Handler RequestHandler // Protocols are the supported protocols. Protocols []string // Origin is used to limit the clients coming from the defined origin Origin string // Compress defines whether using compression or not. // TODO Compress bool }
NetUpgrader upgrades HTTP connection to a websocket connection if it's possible.
NetUpgrader executes NetUpgrader.Handler after successful websocket upgrading.
func (*NetUpgrader) Upgrade ¶
func (upgr *NetUpgrader) Upgrade(resp http.ResponseWriter, req *http.Request)
Upgrade upgrades HTTP to websocket connection if possible.
If client does not request any websocket connection this function will execute ctx.NotFound()
When connection is successfully stablished the function calls s.Handler.
type RequestHandler ¶
type RequestHandler func(conn *Conn)
RequestHandler is the websocket connection handler.
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 the upgrading handler.
type Upgrader ¶
type Upgrader struct { // UpgradeHandler allows user to handle RequestCtx when upgrading. // // If UpgradeHandler returns false the connection won't be upgraded and // the parsed ctx will be used as a response. UpgradeHandler UpgradeHandler // Handler is the request handler for ws connections. Handler RequestHandler // Protocols are the supported protocols. Protocols []string // Origin is used to limit the clients coming from the defined origin Origin string // Compress defines whether using compression or not. // TODO Compress bool }
Upgrader upgrades HTTP connection to a websocket connection if it's possible.
Upgrader executes Upgrader.Handler after successful websocket upgrading.
func (*Upgrader) Upgrade ¶
func (upgr *Upgrader) Upgrade(ctx *fasthttp.RequestCtx)
Upgrade upgrades HTTP to websocket connection if possible.
If client does not request any websocket connection this function will execute ctx.NotFound()
When connection is successfully stablished the function calls s.Handler.