Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package melody

import "time"

type Option func(config *Config)

// Config melody configuration struct.
type Config struct {
WriteWait time.Duration // Milliseconds until write times out.
Expand All @@ -20,3 +22,38 @@ func newConfig() *Config {
MessageBufferSize: 256,
}
}

// WithMaxMessageSize sets the write wait time.
func WithMaxMessageSize(maxMessageSize int64) Option {
return func(c *Config) {
c.MaxMessageSize = maxMessageSize
}
}

// WithMessageBufferSize sets the message buffer size.
func WithMessageBufferSize(messageBufferSize int) Option {
return func(c *Config) {
c.MessageBufferSize = messageBufferSize
}
}

// WithWriteWait sets the write wait time.
func WithWriteWait(writeWait time.Duration) Option {
return func(c *Config) {
c.WriteWait = writeWait
}
}

// WithPongWait sets the pong wait time.
func WithPongWait(pongWait time.Duration) Option {
return func(c *Config) {
c.PongWait = pongWait
}
}

// WithPingPeriod sets the ping period time.
func WithPingPeriod(pingPeriod time.Duration) Option {
return func(c *Config) {
c.PingPeriod = pingPeriod
}
}
9 changes: 9 additions & 0 deletions melody.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ func New() *Melody {
}
}

// NewWithConfig creates a melody instance with Custom configuration.
func NewWithConfig(options ...Option) *Melody {
m := New()
for _, opt := range options {
opt(m.Config)
}
return m
}

// HandleConnect fires fn when a session connects.
func (m *Melody) HandleConnect(fn func(*Session)) {
m.connectHandler = fn
Expand Down