websocket

package module
v0.0.0-...-5c985c5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2025 License: MIT Imports: 18 Imported by: 0

README

Documentation Go workflow CircleCI codecov Go Report Card GitHub tag (latest SemVer)

websocket

Lightweight efficient websocket library.

Usage

Server
func server() error {
    s := websocket.Server{
        Handler: func(ctx context.Context, c *websocket.Conn) error {
            _, err := io.Copy(c, c)
            return err
        },
    }

    return http.ListenAndServe(":80", &s)
}

// or
func handler(w http.ResponseWriter, req *http.Request) error {
    var s websocket.Server

    // play with req: check auth, handle hacky case, etc...

    _, err := s.ServeHandler(w, req, func(ctx context.Context, c *websocket.Conn) error {
        _, err := io.Copy(c, c)
        return err
    })

    return err
}

// or
func withoutCallback(w http.ResponseWriter, req *http.Request) error {
    var s websocket.Server

    // play with req: check auth, handle hacky case, etc...

    c, err := s.Handshake(ctx, w, req)
    if err != nil {
        return errors.Wrap(err, "handshake")
    }

    defer c.Close()

    // all the following is connection handler essentially

    _, err = io.Copy(c, c)

    return err
}
Client
func dial() (*websocket.Conn, error){
    var cl websocket.Client

    return cl.DialContext(ctx, "ws://some-host/some/path?and=params")
}

// or
func manual() (*websocket.Conn, error){
    var cl websocket.Client

    req, err := cl.NewRequest(ctx, "ws://some-host/some/path?and=params")
    // if err != nil ...

    // set req.Header or something

    c, resp, err := cl.Handshake(ctx, req)
    // if err != nil ...

    // check resp.Header or something

    return c, nil
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//	ErrClosed       = errors.New("attempt to write to closed connection")
	ErrNotHijacker  = errors.New("response is not hijacker")
	ErrNotWebsocket = errors.New("not websocket")
	ErrProtocol     = StatusProtocol
	ErrTrailingData = errors.New("trailing data in request")
)

Functions

func FixError

func FixError(ctx context.Context, err error) error

func Stopper

func Stopper(ctx context.Context, dead func(time.Time) error) func()

Types

type Client

type Client struct {
	Header http.Header

	Dialer    net.Dialer
	TLSDialer tls.Dialer
}

func (*Client) DialContext

func (c *Client) DialContext(ctx context.Context, rurl string) (*Conn, error)

func (*Client) Handshake

func (cl *Client) Handshake(ctx context.Context, req *http.Request) (conn *Conn, resp *http.Response, err error)

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, rurl string) (*http.Request, error)

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

func (*Conn) Close

func (c *Conn) Close() (err error)

func (*Conn) CloseWriter

func (c *Conn) CloseWriter(status Status) (err error)

func (*Conn) CloseWriterBody

func (c *Conn) CloseWriterBody(status Status, body []byte) (err error)

func (*Conn) NextFrame

func (c *Conn) NextFrame(ctx context.Context) (Frame, error)

func (*Conn) NextRawFrame

func (c *Conn) NextRawFrame(ctx context.Context) (Frame, error)

func (*Conn) Read

func (c *Conn) Read(p []byte) (n int, err error)

func (*Conn) ReadContext

func (c *Conn) ReadContext(ctx context.Context, p []byte) (n int, err error)

func (*Conn) Write

func (c *Conn) Write(p []byte) (int, error)

func (*Conn) WriteFrame

func (c *Conn) WriteFrame(p []byte, op Opcode, final bool) (int, error)

type DialerContext

type DialerContext interface {
	DialContext(ctx context.Context, net, addr string) (net.Conn, error)
}

type Frame

type Frame struct {
	Opcode Opcode
	Length int
	Final  bool
	// contains filtered or unexported fields
}

func (Frame) More

func (f Frame) More() int

func (Frame) Read

func (f Frame) Read(p []byte) (n int, err error)

func (Frame) ReadAppendTo

func (f Frame) ReadAppendTo(ctx context.Context, b []byte) ([]byte, error)

func (Frame) ReadAppendToLimit

func (f Frame) ReadAppendToLimit(ctx context.Context, b []byte, limit int) ([]byte, error)

func (Frame) ReadContext

func (f Frame) ReadContext(ctx context.Context, p []byte) (n int, err error)

type Handler

type Handler = func(ctx context.Context, c *Conn) error

type HeaderBits

type HeaderBits [2]byte

func MakeHeaderBits

func MakeHeaderBits(op int, final, masked bool) HeaderBits

func (HeaderBits) Fin

func (f HeaderBits) Fin() bool

func (HeaderBits) IsDataFrame

func (f HeaderBits) IsDataFrame() bool

func (HeaderBits) Masked

func (f HeaderBits) Masked() bool

func (HeaderBits) Opcode

func (f HeaderBits) Opcode() Opcode

func (*HeaderBits) Parse

func (f *HeaderBits) Parse(b []byte, st int) int

func (HeaderBits) ParseLen

func (f HeaderBits) ParseLen(b []byte, st int) (l, i int)

func (HeaderBits) ReadMaskingKey

func (f HeaderBits) ReadMaskingKey(b []byte, st int, key []byte) (i int)

type Opcode

type Opcode byte
const (

	// Non-control frames.
	FrameContinue Opcode = 0
	FrameText     Opcode = 1
	FrameBinary   Opcode = 2

	// Control Frames.
	FrameClose Opcode = 0x8
	FramePing  Opcode = 0x9
	FramePong  Opcode = 0xa
)

func (Opcode) String

func (op Opcode) String() string

type Server

type Server struct {
	Handler Handler
}

func (*Server) Handshake

func (s *Server) Handshake(ctx context.Context, w http.ResponseWriter, req *http.Request) (*Conn, error)

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Server) ServeHandler

func (s *Server) ServeHandler(w http.ResponseWriter, req *http.Request, h Handler) (handshake bool, err error)

type Status

type Status uint16
const (
	StatusOK Status = 1000 + iota
	StatusGoingAway
	StatusProtocol
	StatusCantAccept

	StatusFormat
	StatusPolicy
	StatusTooBig

	StatusExtensions
	StatusInternal
)

func (Status) Error

func (s Status) Error() string

func (Status) OK

func (s Status) OK() bool

type StatusText

type StatusText struct {
	Status Status
	Text   string
}

func (*StatusText) Error

func (s *StatusText) Error() string

func (*StatusText) Unwrap

func (s *StatusText) Unwrap() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL