Documentation ¶
Index ¶
- Constants
- Variables
- func FormatCloseMessage(closeCode int, text string) []byte
- type Client
- type Config
- type ISessionHandler
- type ISessionHandlerProducer
- type Melody
- func (m *Melody) BroadcastBinary(msg []byte) error
- func (m *Melody) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool) error
- func (m *Melody) BroadcastBinaryMultiple(msg []byte, sessions []*Session) error
- func (m *Melody) BroadcastBinaryOthers(msg []byte, s *Session) error
- func (m *Melody) BroadcastText(msg []byte) error
- func (m *Melody) BroadcastTextFilter(msg []byte, fn func(*Session) bool) error
- func (m *Melody) BroadcastTextMultiple(msg []byte, sessions []*Session) error
- func (m *Melody) BroadcastTextOthers(msg []byte, s *Session) error
- func (m *Melody) Close() error
- func (m *Melody) CloseWithMsg(msg []byte) error
- func (m *Melody) Dial(addr string, timeout time.Duration, handler ISessionHandler) (session *Session, err error)
- func (m *Melody) HandleProduceSessionHandler(fn SessionHandlerProduceFunc)
- func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) error
- func (m *Melody) HandleRequestWithStates(w http.ResponseWriter, r *http.Request, states SessionStates) error
- func (m *Melody) IsClosed() bool
- func (m *Melody) Len() int
- type Server
- type Session
- func (s *Session) Close() error
- func (s *Session) CloseWithMsg(msg []byte) error
- func (s *Session) Get(key string) (value interface{}, exists bool)
- func (s Session) ID() SessionID
- func (s *Session) IsClosed() bool
- func (s *Session) MustGet(key string) interface{}
- func (s Session) Request() *http.Request
- func (s *Session) Set(key string, value interface{})
- func (s *Session) SetHandler(handler ISessionHandler)
- func (s Session) URLPath() string
- func (s *Session) WriteBinary(msg []byte) error
- func (s *Session) WriteText(msg []byte) error
- type SessionHandler
- func (SessionHandler) HandleAfterSendBinaryMessage(*Session, []byte)
- func (SessionHandler) HandleAfterSendTextMessage(*Session, []byte)
- func (SessionHandler) HandleBinaryMessage(*Session, []byte)
- func (SessionHandler) HandleClose(*Session, int, string) error
- func (SessionHandler) HandleConnect(*Session) error
- func (SessionHandler) HandleDisconnect(*Session)
- func (SessionHandler) HandleError(*Session, error)
- func (SessionHandler) HandlePong(*Session)
- func (SessionHandler) HandleTextMessage(*Session, []byte)
- type SessionHandler2
- func (h SessionHandler2) HandleAfterSendBinaryMessage(session *Session, data []byte)
- func (h SessionHandler2) HandleAfterSendTextMessage(session *Session, data []byte)
- func (h SessionHandler2) HandleBinaryMessage(session *Session, data []byte)
- func (h SessionHandler2) HandleClose(session *Session, code int, s string) error
- func (h SessionHandler2) HandleConnect(session *Session) error
- func (h SessionHandler2) HandleDisconnect(session *Session)
- func (h SessionHandler2) HandleError(session *Session, err error)
- func (h SessionHandler2) HandlePong(session *Session)
- func (h SessionHandler2) HandleTextMessage(session *Session, data []byte)
- type SessionHandlerProduceFunc
- type SessionID
- type SessionStates
Constants ¶
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 ¶
var C = NewClient(DefaultConfig)
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
var S = NewServer(DefaultConfig)
Functions ¶
func FormatCloseMessage ¶
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 ISessionHandler ¶
type ISessionHandler interface { HandleTextMessage(*Session, []byte) HandleBinaryMessage(*Session, []byte) HandleAfterSendTextMessage(*Session, []byte) HandleAfterSendBinaryMessage(*Session, []byte) HandleError(*Session, error) HandleClose(*Session, int, string) error HandleConnect(*Session) error HandleDisconnect(*Session) HandlePong(*Session) }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type ISessionHandlerProducer ¶
type ISessionHandlerProducer interface {
ProduceSessionHandler(r *http.Request) ISessionHandler
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type Melody ¶
type Melody struct { Config Config Upgrader *websocket.Upgrader // contains filtered or unexported fields }
Melody implements a websocket manager.
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) BroadcastBinaryMultiple ¶
BroadcastBinaryMultiple broadcasts a binary message to multiple sessions given in the sessions slice.
func (*Melody) BroadcastBinaryOthers ¶
BroadcastBinaryOthers broadcasts a binary message to all sessions except session s.
func (*Melody) BroadcastText ¶
BroadcastText broadcasts a text message to all sessions.
func (*Melody) BroadcastTextFilter ¶
BroadcastTextFilter broadcasts a text message to all sessions that fn returns true for.
func (*Melody) BroadcastTextMultiple ¶
BroadcastTextMultiple broadcasts a text message to multiple sessions given in the sessions slice.
func (*Melody) BroadcastTextOthers ¶
BroadcastTextOthers 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) Dial ¶
func (m *Melody) Dial(addr string, timeout time.Duration, handler ISessionHandler) (session *Session, err error)
Dial connect to websocket server
func (*Melody) HandleProduceSessionHandler ¶
func (m *Melody) HandleProduceSessionHandler(fn SessionHandlerProduceFunc)
HandleProduceSessionHandler produce session handler
func (*Melody) HandleRequest ¶
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.
type Server ¶
type Server struct { *Melody // contains filtered or unexported fields }
func (Server) ListenAddr ¶
func (Server) SessionNum ¶
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Session wrapper around websocket connections.
func NewSession ¶
func NewSession( conn *websocket.Conn, r *http.Request, m *Melody, handler ISessionHandler, states SessionStates, ) *Session
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) MustGet ¶
MustGet returns the value for the given key if it exists, otherwise it panics.
func (*Session) Set ¶
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) SetHandler ¶
func (s *Session) SetHandler(handler ISessionHandler)
func (*Session) WriteBinary ¶
WriteBinary writes a binary message to session.
type SessionHandler ¶
type SessionHandler struct { }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func (SessionHandler) HandleAfterSendBinaryMessage ¶
func (SessionHandler) HandleAfterSendBinaryMessage(*Session, []byte)
func (SessionHandler) HandleAfterSendTextMessage ¶
func (SessionHandler) HandleAfterSendTextMessage(*Session, []byte)
func (SessionHandler) HandleBinaryMessage ¶
func (SessionHandler) HandleBinaryMessage(*Session, []byte)
func (SessionHandler) HandleClose ¶
func (SessionHandler) HandleClose(*Session, int, string) error
func (SessionHandler) HandleConnect ¶
func (SessionHandler) HandleConnect(*Session) error
func (SessionHandler) HandleDisconnect ¶
func (SessionHandler) HandleDisconnect(*Session)
func (SessionHandler) HandleError ¶
func (SessionHandler) HandleError(*Session, error)
func (SessionHandler) HandlePong ¶
func (SessionHandler) HandlePong(*Session)
func (SessionHandler) HandleTextMessage ¶
func (SessionHandler) HandleTextMessage(*Session, []byte)
type SessionHandler2 ¶
type SessionHandler2 struct {
// contains filtered or unexported fields
}
func NewSessionHandler2 ¶
func NewSessionHandler2( textMessageHandler handleMessageFunc, binaryMessageHandler handleMessageFunc, afterSendTextMessageHandler handleMessageFunc, afterSendBinaryMessageHandler handleMessageFunc, errorHandler handleErrorFunc, closeHandler handleCloseFunc, connectHandler handleConnectFunc, disconnectHandler handleSessionFunc, pongHandler handleSessionFunc, ) *SessionHandler2
func (SessionHandler2) HandleAfterSendBinaryMessage ¶
func (h SessionHandler2) HandleAfterSendBinaryMessage(session *Session, data []byte)
func (SessionHandler2) HandleAfterSendTextMessage ¶
func (h SessionHandler2) HandleAfterSendTextMessage(session *Session, data []byte)
func (SessionHandler2) HandleBinaryMessage ¶
func (h SessionHandler2) HandleBinaryMessage(session *Session, data []byte)
func (SessionHandler2) HandleClose ¶
func (h SessionHandler2) HandleClose(session *Session, code int, s string) error
func (SessionHandler2) HandleConnect ¶
func (h SessionHandler2) HandleConnect(session *Session) error
func (SessionHandler2) HandleDisconnect ¶
func (h SessionHandler2) HandleDisconnect(session *Session)
func (SessionHandler2) HandleError ¶
func (h SessionHandler2) HandleError(session *Session, err error)
func (SessionHandler2) HandlePong ¶
func (h SessionHandler2) HandlePong(session *Session)
func (SessionHandler2) HandleTextMessage ¶
func (h SessionHandler2) HandleTextMessage(session *Session, data []byte)
type SessionHandlerProduceFunc ¶
type SessionHandlerProduceFunc func(r *http.Request) ISessionHandler
func (SessionHandlerProduceFunc) ProduceSessionHandler ¶
func (fn SessionHandlerProduceFunc) ProduceSessionHandler(r *http.Request) ISessionHandler
type SessionID ¶
type SessionID = uint64
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type SessionStates ¶
type SessionStates = map[string]interface{}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////