v1

package
v0.0.0-...-83fd405 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CloseNormalClosure           = 1000
	CloseGoingAway               = 1001
	CloseProtocolError           = 1002
	CloseUnsupportedData         = 1003
	CloseNoStatusReceived        = 1005
	CloseAbnormalClosure         = 1006
	CloseInvalidFramePayloadData = 1007
	ClosePolicyViolation         = 1008
	CloseMessageTooBig           = 1009
	CloseMandatoryExtension      = 1010
	CloseInternalServerErr       = 1011
	CloseServiceRestart          = 1012
	CloseTryAgainLater           = 1013
	CloseTLSHandshake            = 1015
)

Close codes defined in RFC 6455, section 11.7. Duplicate of codes from gorilla/websocket for convenience.

Variables

View Source
var DefaultConfig = Config{
	WriteWait:         10 * time.Second,
	PongWait:          60 * time.Second,
	PingPeriod:        (60 * time.Second * 9) / 10,
	MaxMessageSize:    1024 * 512,
	MessageBufferSize: 4096,
}

DefaultConfig default melody configuration

Functions

func FormatCloseMessage

func FormatCloseMessage(closeCode int, text string) []byte

FormatCloseMessage formats closeCode and text as a WebSocket close message.

Types

type Config

type Config struct {
	WriteWait         time.Duration // Milliseconds until write times out.
	PongWait          time.Duration // Timeout for waiting on pong.
	PingPeriod        time.Duration // Milliseconds between pings.
	MaxMessageSize    int64         // Maximum size in bytes of a message.
	MessageBufferSize int           // The max amount of messages that can be in a sessions buffer before it starts dropping them.
}

Config melody configuration struct.

type Melody

type Melody struct {
	Config   Config
	Upgrader *websocket.Upgrader
	// contains filtered or unexported fields
}

Melody implements a websocket manager.

func NewMelody

func NewMelody(cfg Config) *Melody

NewMelody creates a new melody instance with default Upgrader and Config.

func (*Melody) BroadcastBinary

func (m *Melody) BroadcastBinary(msg []byte) error

BroadcastBinary broadcasts a binary message to all sessions.

func (*Melody) BroadcastBinaryFilter

func (m *Melody) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool) error

BroadcastBinaryFilter broadcasts a binary message to all sessions that fn returns true for.

func (*Melody) BroadcastBinaryMultiple

func (m *Melody) BroadcastBinaryMultiple(msg []byte, sessions []*Session) error

BroadcastBinaryMultiple broadcasts a binary message to multiple sessions given in the sessions slice.

func (*Melody) BroadcastBinaryOthers

func (m *Melody) BroadcastBinaryOthers(msg []byte, s *Session) error

BroadcastBinaryOthers broadcasts a binary message to all sessions except session s.

func (*Melody) BroadcastText

func (m *Melody) BroadcastText(msg []byte) error

BroadcastText broadcasts a text message to all sessions.

func (*Melody) BroadcastTextFilter

func (m *Melody) BroadcastTextFilter(msg []byte, fn func(*Session) bool) error

BroadcastTextFilter broadcasts a text message to all sessions that fn returns true for.

func (*Melody) BroadcastTextMultiple

func (m *Melody) BroadcastTextMultiple(msg []byte, sessions []*Session) error

BroadcastTextMultiple broadcasts a text message to multiple sessions given in the sessions slice.

func (*Melody) BroadcastTextOthers

func (m *Melody) BroadcastTextOthers(msg []byte, s *Session) error

BroadcastTextOthers broadcasts a text message to all sessions except session s.

func (*Melody) Close

func (m *Melody) Close() error

Close closes the melody instance and all connected sessions.

func (*Melody) CloseWithMsg

func (m *Melody) CloseWithMsg(msg []byte) error

CloseWithMsg closes the melody instance with the given close payload and all connected sessions. Use the FormatCloseMessage function to format a proper close message payload.

func (*Melody) HandleAfterSendBinaryMessage

func (m *Melody) HandleAfterSendBinaryMessage(fn func(*Session, []byte))

HandleAfterSendBinaryMessage fires fn when a binary message is successfully sent.

func (*Melody) HandleAfterSendTextMessage

func (m *Melody) HandleAfterSendTextMessage(fn func(*Session, []byte))

HandleAfterSendTextMessage fires fn when a text message is successfully sent.

func (*Melody) HandleBinaryMessage

func (m *Melody) HandleBinaryMessage(fn func(*Session, []byte))

HandleBinaryMessage fires fn when a binary message comes in.

func (*Melody) HandleClose

func (m *Melody) HandleClose(fn func(*Session, int, string) error)

HandleClose sets the handler for close messages received from the session. The code argument to h is the received close code or CloseNoStatusReceived if the close message is empty. The default close handler sends a close frame back to the session.

The application must read the connection to process close messages as described in the section on Control Frames above.

The connection read methods return a CloseError when a close frame is received. Most applications should handle close messages as part of their normal error handling. Applications should only set a close handler when the application must perform some action before sending a close frame back to the session.

func (*Melody) HandleConnect

func (m *Melody) HandleConnect(fn func(*Session))

HandleConnect fires fn when a session connects.

func (*Melody) HandleDisconnect

func (m *Melody) HandleDisconnect(fn func(*Session))

HandleDisconnect fires fn when a session disconnects.

func (*Melody) HandleError

func (m *Melody) HandleError(fn func(*Session, error))

HandleError fires fn when a session has an error.

func (*Melody) HandlePong

func (m *Melody) HandlePong(fn func(*Session))

HandlePong fires fn when a pong is received from a session.

func (*Melody) HandleRequest

func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) error

HandleRequest upgrades http requests to websocket connections and dispatches them to be handled by the melody instance.

func (*Melody) HandleRequestWithStates

func (m *Melody) HandleRequestWithStates(w http.ResponseWriter, r *http.Request, states SessionStates) error

HandleRequestWithStates does the same as HandleRequest but populates session.States with states.

func (*Melody) HandleTextMessage

func (m *Melody) HandleTextMessage(fn func(*Session, []byte))

HandleTextMessage fires fn when a text message comes in.

func (*Melody) IsClosed

func (m *Melody) IsClosed() bool

IsClosed returns the status of the melody instance.

func (*Melody) Len

func (m *Melody) Len() int

Len return the number of connected sessions.

type Server

type Server struct {
	*Melody
	// contains filtered or unexported fields
}

func NewServer

func NewServer(cfg Config) *Server

func (Server) ListenAddr

func (s Server) ListenAddr() string

func (*Server) Serve

func (s *Server) Serve(addr string) (err error)

func (*Server) ServeHTTP

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

func (*Server) ServeTLS

func (s *Server) ServeTLS(addr string, certFile string, keyFile string) (err error)

func (Server) SessionNum

func (s Server) SessionNum() int

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session wrapper around websocket connections.

func NewSession

func NewSession(
	r *http.Request,
	conn *websocket.Conn,
	m *Melody,
	states SessionStates,
) *Session

func (*Session) Close

func (s *Session) Close() error

Close closes session.

func (*Session) CloseWithMsg

func (s *Session) CloseWithMsg(msg []byte) error

CloseWithMsg closes the session with the provided payload. Use the FormatCloseMessage function to format a proper close message payload.

func (*Session) Get

func (s *Session) Get(key string) (value interface{}, exists bool)

Get returns the value for the given key, ie: (value, true). If the value does not exists it returns (nil, false)

func (Session) ID

func (s Session) ID() SessionID

func (*Session) IsClosed

func (s *Session) IsClosed() bool

IsClosed returns the status of the connection.

func (*Session) MustGet

func (s *Session) MustGet(key string) interface{}

MustGet returns the value for the given key if it exists, otherwise it panics.

func (Session) Request

func (s Session) Request() *http.Request

func (*Session) Set

func (s *Session) Set(key string, value interface{})

Set is used to store a new key/value pair exclusivelly for this session. It also lazy initializes s.Keys if it was not used previously.

func (Session) URLPath

func (s Session) URLPath() string

func (*Session) WriteBinary

func (s *Session) WriteBinary(msg []byte) error

WriteBinary writes a binary message to session.

func (*Session) WriteText

func (s *Session) WriteText(msg []byte) error

WriteText writes message to session.

type SessionID

type SessionID = uint64

type SessionStates

type SessionStates = map[string]interface{}

Jump to

Keyboard shortcuts

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