Move address into transport config

This makes it possible to use a factory function to create a transport of the right type and not having to repeat the address when calling Transport.Connect()
This commit is contained in:
Wichert Akkerman
2019-10-11 06:41:15 +02:00
committed by Mickaël Rémond
parent f8d0e99696
commit 7fa4b06705
13 changed files with 67 additions and 25 deletions
+4 -3
View File
@@ -11,21 +11,22 @@ import (
)
type WebsocketTransport struct {
Config TransportConfiguration
wsConn *websocket.Conn
netConn net.Conn
ctx context.Context
}
func (t *WebsocketTransport) Connect(address string, c Config) error {
func (t *WebsocketTransport) Connect() error {
t.ctx = context.Background()
ctx, cancel := context.WithTimeout(t.ctx, time.Duration(c.ConnectTimeout)*time.Second)
ctx, cancel := context.WithTimeout(t.ctx, time.Duration(t.Config.ConnectTimeout)*time.Second)
defer cancel()
if !c.Insecure && strings.HasPrefix(address, "wss:") {
return errors.New("Websocket address is not secure")
}
wsConn, _, err := websocket.Dial(ctx, address, nil)
wsConn, _, err := websocket.Dial(ctx, t.Config.Address, nil)
if err != nil {
t.wsConn = wsConn
t.netConn = websocket.NetConn(t.ctx, t.wsConn, websocket.MessageText)