diff --git a/config.go b/config.go index f483af1..42ae530 100644 --- a/config.go +++ b/config.go @@ -2,13 +2,13 @@ package melody import "time" -// Melody configuration, all times in milliseconds. +// Melody configuration. type Config struct { - WriteWait time.Duration - PongWait time.Duration - PingPeriod time.Duration - MaxMessageSize int64 - MessageBufferSize int + 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 // Size of each sessions message buffer. } func newConfig() *Config { diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..4b0d115 --- /dev/null +++ b/doc.go @@ -0,0 +1,19 @@ +package melody + +// 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") +// } diff --git a/melody.go b/melody.go index 1026a75..1ec4778 100644 --- a/melody.go +++ b/melody.go @@ -21,7 +21,7 @@ type Melody struct { hub *hub } -// Returns a new melody instance. +// Returns a new melody instance with default Upgrader and Config. func New() *Melody { upgrader := &websocket.Upgrader{ ReadBufferSize: 1024, @@ -54,7 +54,7 @@ func (m *Melody) HandleDisconnect(fn func(*Session)) { m.disconnectHandler = fn } -// Callback when a message comes in. +// Callback when a text message comes in. func (m *Melody) HandleMessage(fn func(*Session, []byte)) { m.messageHandler = fn } @@ -69,7 +69,7 @@ func (m *Melody) HandleError(fn func(*Session, error)) { m.errorHandler = fn } -// Handles a http request and upgrades it to a websocket. +// Handles http requests and upgrades them to websocket connections. func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) { conn, err := m.Upgrader.Upgrade(w, r, nil) @@ -105,7 +105,7 @@ func (m *Melody) BroadcastFilter(msg []byte, fn func(*Session) bool) { m.hub.broadcast <- message } -// Broadcasts a message to all sessions except session `s`. +// Broadcasts a message to all sessions except session s. func (m *Melody) BroadcastOthers(msg []byte, s *Session) { m.BroadcastFilter(msg, func(q *Session) bool { return s != q diff --git a/session.go b/session.go index 0c890a6..72bf9f0 100644 --- a/session.go +++ b/session.go @@ -107,15 +107,15 @@ func (s *Session) readPump(messageHandler handleMessageFunc, messageHandlerBinar } // Write message to session. -func (s *Session) WriteBinary(msg []byte) { - s.writeMessage(&envelope{t: websocket.BinaryMessage, msg: msg}) -} - -// Write message to session. func (s *Session) Write(msg []byte) { s.writeMessage(&envelope{t: websocket.TextMessage, msg: msg}) } +// Write binary message to session. +func (s *Session) WriteBinary(msg []byte) { + s.writeMessage(&envelope{t: websocket.BinaryMessage, msg: msg}) +} + // Close session. func (s *Session) Close() { s.writeMessage(&envelope{t: websocket.CloseMessage, msg: []byte{}})