Documentation ¶
Overview ¶
Package melody implements a framework for dealing with WebSockets.
Example ¶
A broadcasting echo server:
func main() { r := gin.Default() m := melody.New() r.GET("/ws", func(c *gin.Context) { m.HandleRequest(c.Writer, c.Request) }) m.HandleMessage(func(s *melody.Session, msg []byte) { m.Broadcast(msg) }) r.Run(":5000") }
Index ¶
- type Config
- type Melody
- func (m *Melody) Broadcast(msg []byte)
- func (m *Melody) BroadcastBinary(msg []byte)
- func (m *Melody) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool)
- func (m *Melody) BroadcastBinaryOthers(msg []byte, s *Session)
- func (m *Melody) BroadcastFilter(msg []byte, fn func(*Session) bool)
- func (m *Melody) BroadcastOthers(msg []byte, s *Session)
- func (m *Melody) Close()
- 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) HandleRequest(w http.ResponseWriter, r *http.Request)
- type Session
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
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. }
Melody configuration.
type Melody ¶
type Melody struct { Config *Config Upgrader *websocket.Upgrader // contains filtered or unexported fields }
func (*Melody) BroadcastBinary ¶
Broadcasts a binary message to all sessions.
func (*Melody) BroadcastBinaryFilter ¶
Broadcasts a binary message to all sessions that fn returns true for.
func (*Melody) BroadcastBinaryOthers ¶
Broadcasts a binary message to all sessions except session s.
func (*Melody) BroadcastFilter ¶
Broadcasts a text message to all sessions that fn returns true for.
func (*Melody) BroadcastOthers ¶
Broadcasts a text message to all sessions except session s.
func (*Melody) Close ¶
func (m *Melody) Close()
Closes the melody instance and all connected sessions.
func (*Melody) HandleConnect ¶
Fires fn when a session connects.
func (*Melody) HandleDisconnect ¶
Fires fn when a session disconnects.
func (*Melody) HandleError ¶
Fires when a session has an error.
func (*Melody) HandleMessage ¶
Callback when a text message comes in.
func (*Melody) HandleMessageBinary ¶
Callback when a binary message comes in.
func (*Melody) HandleRequest ¶
func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request)
Handles http requests and upgrades them to websocket connections.