feat(protocol): allow override of dial func
This commit is contained in:
@ -32,12 +32,15 @@ func (c *Client) Connect() error {
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
transport := &transport.WebsocketTransport{
|
||||
PingInterval: c.opts.PingInterval,
|
||||
PingTimeout: c.opts.PingTimeout,
|
||||
ReceiveTimeout: c.opts.ReceiveTimeout,
|
||||
SendTimeout: c.opts.SendTimeout,
|
||||
BufferSize: c.opts.BufferSize,
|
||||
transport := &Transport{
|
||||
dial: c.opts.DialFunc,
|
||||
ws: &transport.WebsocketTransport{
|
||||
PingInterval: c.opts.PingInterval,
|
||||
PingTimeout: c.opts.PingTimeout,
|
||||
ReceiveTimeout: c.opts.ReceiveTimeout,
|
||||
SendTimeout: c.opts.SendTimeout,
|
||||
BufferSize: c.opts.BufferSize,
|
||||
},
|
||||
}
|
||||
|
||||
conn, err := gosocketio.Dial(c.endpoint, transport)
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func EndpointFromHAddr(addr string) (string, error) {
|
||||
func EndpointFromAddr(addr string) (string, error) {
|
||||
host, rawPort, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
var addrErr *net.AddrError
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
46
reach/client/socketio/transport.go
Normal file
46
reach/client/socketio/transport.go
Normal file
@ -0,0 +1,46 @@
|
||||
package socketio
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/Pyxis/golang-socketio/transport"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type Transport struct {
|
||||
dial DialFunc
|
||||
ws *transport.WebsocketTransport
|
||||
}
|
||||
|
||||
// Connect implements transport.Transport.
|
||||
func (t *Transport) Connect(url string) (conn transport.Connection, err error) {
|
||||
if t.dial == nil {
|
||||
return t.ws.Connect(url)
|
||||
} else {
|
||||
dialer := websocket.Dialer{
|
||||
NetDial: func(network, addr string) (net.Conn, error) {
|
||||
return t.dial(network, addr)
|
||||
},
|
||||
}
|
||||
|
||||
socket, _, err := dialer.Dial(url, t.ws.RequestHeader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return transport.NewWebsocketConnection(socket, t.ws), nil
|
||||
}
|
||||
}
|
||||
|
||||
// HandleConnection implements transport.Transport.
|
||||
func (t *Transport) HandleConnection(w http.ResponseWriter, r *http.Request) (conn transport.Connection, err error) {
|
||||
return t.ws.HandleConnection(w, r)
|
||||
}
|
||||
|
||||
// Serve implements transport.Transport.
|
||||
func (t *Transport) Serve(w http.ResponseWriter, r *http.Request) {
|
||||
t.ws.Serve(w, r)
|
||||
}
|
||||
|
||||
var _ transport.Transport = &Transport{}
|
Reference in New Issue
Block a user