diff --git a/config.go b/config.go index 81ebd05..b87afee 100644 --- a/config.go +++ b/config.go @@ -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. @@ -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 + } +} diff --git a/melody.go b/melody.go index ca2c5dd..ac2b669 100644 --- a/melody.go +++ b/melody.go @@ -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