feat(protocol): allow override of dial func

This commit is contained in:
2024-08-05 18:10:19 +02:00
parent b976bde363
commit 6842d4d88a
16 changed files with 158 additions and 18 deletions

View File

@ -1,6 +1,13 @@
package socketio
import "time"
import (
"net"
"time"
"github.com/pkg/errors"
)
type DialFunc func(network, addr string) (net.Conn, error)
type Options struct {
PingInterval time.Duration
@ -8,6 +15,7 @@ type Options struct {
ReceiveTimeout time.Duration
SendTimeout time.Duration
BufferSize int
DialFunc DialFunc
}
type OptionFunc func(opts *Options)
@ -19,6 +27,7 @@ func NewOptions(funcs ...OptionFunc) *Options {
ReceiveTimeout: 60 * time.Second,
SendTimeout: 60 * time.Second,
BufferSize: 1024 * 32,
DialFunc: DefaultDialFunc,
}
for _, fn := range funcs {
fn(opts)
@ -60,3 +69,19 @@ func WithBufferSize(size int) OptionFunc {
opts.BufferSize = size
}
}
var DefaultDialFunc = func(network, addr string) (net.Conn, error) {
conn, err := net.Dial(network, addr)
if err != nil {
return nil, errors.WithStack(err)
}
return conn, nil
}
// WithDialFunc configures the client to use the given dial func
func WithDialFunc(dial DialFunc) OptionFunc {
return func(opts *Options) {
opts.DialFunc = dial
}
}