Documentation
¶
Index ¶
- Constants
- Variables
- type Melody
- func (m *Melody) Broadcast(msg []byte) error
- func (m *Melody) BroadcastBinary(msg []byte) error
- func (m *Melody) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool) error
- func (m *Melody) BroadcastBinaryOthers(msg []byte, s *Session) error
- func (m *Melody) BroadcastFilter(msg []byte, fn func(*Session) bool) error
- func (m *Melody) BroadcastMultiple(msg []byte, sessions []*Session) error
- func (m *Melody) BroadcastOthers(msg []byte, s *Session) error
- func (m *Melody) Close() error
- func (m *Melody) CloseWithMsg(msg []byte) error
- func (m *Melody) HandleClose(fn func(*Session, websocket.CloseError) error)
- func (m *Melody) HandleConnect(fn func(*Session))
- func (m *Melody) HandleDisconnect(fn func(*Session))
- func (m *Melody) HandleError(fn func(*Session, error))
- func (m *Melody) HandleMessage(fn func(*Session, []byte))
- func (m *Melody) HandleMessageBinary(fn func(*Session, []byte))
- func (m *Melody) HandlePong(fn func(*Session))
- func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) error
- func (m *Melody) HandleRequestWithKeys(w http.ResponseWriter, r *http.Request, keys map[string]any) error
- func (m *Melody) HandleSentMessage(fn func(*Session, []byte))
- func (m *Melody) HandleSentMessageBinary(fn func(*Session, []byte))
- func (m *Melody) IsClosed() bool
- func (m *Melody) Len() int
- func (m *Melody) Sessions() ([]*Session, error)
- type MelodyOption
- func WithAcceptOptions(acceptOptions *websocket.AcceptOptions) MelodyOption
- func WithConcurrentMessageHandling(concurrentMessageHandling bool) MelodyOption
- func WithMaxMessageSize(maxMessageSize int64) MelodyOption
- func WithMessageBufferSize(messageBufferSize int) MelodyOption
- func WithOriginPatterns(patterns []string) MelodyOption
- func WithPingPeriod(pingPeriod time.Duration) MelodyOption
- func WithPongWait(pongWait time.Duration) MelodyOption
- func WithWriteWait(writeWait time.Duration) MelodyOption
- type MelodyOptions
- type MessageType
- type Session
- func (s *Session) Accept(w http.ResponseWriter, r *http.Request) error
- func (s *Session) Close() error
- func (s *Session) CloseWithMsg(msg []byte) error
- func (s *Session) Get(key string) (value any, exists bool)
- func (s *Session) IsClosed() bool
- func (s *Session) LocalAddr() net.Addr
- func (s *Session) MustGet(key string) any
- func (s *Session) RemoteAddr() net.Addr
- func (s *Session) Set(key string, value any)
- func (s *Session) UnSet(key string)
- func (s *Session) WebsocketConnection() *websocket.Conn
- func (s *Session) Write(msg []byte) error
- func (s *Session) WriteBinary(msg []byte) error
Constants ¶
const ( // TextMessage denotes a text data message. The text message payload is // interpreted as UTF-8 encoded text data. TextMessage = 1 // BinaryMessage denotes a binary data message. BinaryMessage = 2 // CloseMessage denotes a close control message. The optional message // payload contains a numeric code and text. Use the FormatCloseMessage // function to format a close message payload. CloseMessage = 8 // PingMessage denotes a ping control message. The optional message payload // is UTF-8 encoded text. PingMessage = 9 // PongMessage denotes a pong control message. The optional message payload // is UTF-8 encoded text. PongMessage = 10 )
The message types are defined in RFC 6455, section 11.8.
Variables ¶
var ( // ErrClosed is the error returned when the melody instance is closed. ErrClosed = errors.New("melody instance is closed") // ErrSessionClosed is the error returned when the session is closed. ErrSessionClosed = errors.New("session is closed") // ErrWriteClosed is the error returned when the session is closed. ErrWriteClosed = errors.New("tried to write to closed a session") // ErrMessageBufferFull is the error returned when the session message buffer is full. ErrMessageBufferFull = errors.New("session message buffer is full") )
Functions ¶
This section is empty.
Types ¶
type Melody ¶
type Melody struct {
// contains filtered or unexported fields
}
Melody is a websocket manager.
func New ¶
func New(options ...MelodyOption) *Melody
New creates a new melody instance with default Upgrader and Config.
func (*Melody) BroadcastBinary ¶
BroadcastBinary broadcasts a binary message to all sessions.
func (*Melody) BroadcastBinaryFilter ¶
BroadcastBinaryFilter broadcasts a binary message to all sessions that fn returns true for.
func (*Melody) BroadcastBinaryOthers ¶
BroadcastBinaryOthers broadcasts a binary message to all sessions except session s.
func (*Melody) BroadcastFilter ¶
BroadcastFilter broadcasts a text message to all sessions that fn returns true for.
func (*Melody) BroadcastMultiple ¶
BroadcastMultiple broadcasts a text message to multiple sessions given in the sessions slice.
func (*Melody) BroadcastOthers ¶
BroadcastOthers broadcasts a text message to all sessions except session s.
func (*Melody) CloseWithMsg ¶
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) HandleClose ¶
func (m *Melody) HandleClose(fn func(*Session, websocket.CloseError) 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 ¶
HandleConnect fires fn when a session connects.
func (*Melody) HandleDisconnect ¶
HandleDisconnect fires fn when a session disconnects.
func (*Melody) HandleError ¶
HandleError fires fn when a session has an error.
func (*Melody) HandleMessage ¶
HandleMessage fires fn when a text message comes in. NOTE: by default Melody handles messages sequentially for each session. This has the effect that a message handler exceeding the read deadline (Config.PongWait, by default 1 minute) will time out the session. Concurrent message handling can be turned on by setting Config.ConcurrentMessageHandling to true.
func (*Melody) HandleMessageBinary ¶
HandleMessageBinary fires fn when a binary message comes in.
func (*Melody) HandlePong ¶
HandlePong fires fn when a pong is received from a session.
func (*Melody) HandleRequest ¶
HandleRequest upgrades http requests to websocket connections and dispatches them to be handled by the melody instance.
func (*Melody) HandleRequestWithKeys ¶
func (m *Melody) HandleRequestWithKeys(w http.ResponseWriter, r *http.Request, keys map[string]any) error
HandleRequestWithKeys does the same as HandleRequest but populates session.Keys with keys.
func (*Melody) HandleSentMessage ¶
HandleSentMessage fires fn when a text message is successfully sent.
func (*Melody) HandleSentMessageBinary ¶
HandleSentMessageBinary fires fn when a binary message is successfully sent.
type MelodyOption ¶
type MelodyOption func(*MelodyOptions)
MelodyOption is a function that can be used to configure the Melody.
func WithAcceptOptions ¶
func WithAcceptOptions(acceptOptions *websocket.AcceptOptions) MelodyOption
WithAcceptOptions sets the accept options for the Melody.
func WithConcurrentMessageHandling ¶
func WithConcurrentMessageHandling(concurrentMessageHandling bool) MelodyOption
WithConcurrentMessageHandling sets the concurrent message handling for the Melody.
func WithMaxMessageSize ¶
func WithMaxMessageSize(maxMessageSize int64) MelodyOption
WithMaxMessageSize sets the max message size for the Melody.
func WithMessageBufferSize ¶
func WithMessageBufferSize(messageBufferSize int) MelodyOption
WithMessageBufferSize sets the message buffer size for the Melody.
func WithOriginPatterns ¶
func WithOriginPatterns(patterns []string) MelodyOption
WithOriginPatterns sets the origin patterns for the Melody.
func WithPingPeriod ¶
func WithPingPeriod(pingPeriod time.Duration) MelodyOption
WithPingPeriod sets the ping period for the Melody.
func WithPongWait ¶
func WithPongWait(pongWait time.Duration) MelodyOption
WithPongWait sets the pong wait for the Melody.
func WithWriteWait ¶
func WithWriteWait(writeWait time.Duration) MelodyOption
WithWriteWait sets the write wait for the Melody.
type MelodyOptions ¶
type MelodyOptions struct {
// contains filtered or unexported fields
}
MelodyOptions is a struct that contains the options for the Melody.
type MessageType ¶
type MessageType int
MessageType represents the type of a WebSocket message. See https://tools.ietf.org/html/rfc6455#section-5.6
type Session ¶
type Session struct {
Request *http.Request
Keys map[string]any
// contains filtered or unexported fields
}
Session wrapper around websocket connections.
func (*Session) CloseWithMsg ¶
CloseWithMsg closes the session with the provided payload. Use the FormatCloseMessage function to format a proper close message payload.
func (*Session) Get ¶
Get returns the value for the given key, ie: (value, true). If the value does not exists it returns (nil, false)
func (*Session) LocalAddr ¶
LocalAddr returns the local addr of the connection. We try to access the underlying connection through reflection.
func (*Session) MustGet ¶
MustGet returns the value for the given key if it exists, otherwise it panics.
func (*Session) RemoteAddr ¶
RemoteAddr returns the remote addr of the connection. We can get this from the HTTP request headers.
func (*Session) Set ¶
Set is used to store a new key/value pair exclusively for this session. It also lazy initializes s.Keys if it was not used previously.
func (*Session) WebsocketConnection ¶
WebsocketConnection returns the underlying websocket connection. This can be used to e.g. set/read additional websocket options or to write sychronous messages.
func (*Session) WriteBinary ¶
WriteBinary writes a binary message to session.