Browse Source

better docs

master
Ola Holmström 10 years ago
parent
commit
5c5e059a07
  1. 12
      config.go
  2. 19
      doc.go
  3. 8
      melody.go
  4. 10
      session.go

12
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 {

19
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")
// }

8
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

10
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{}})

Loading…
Cancel
Save