diff --git a/client.go b/client.go index cd7640b..2c8cec9 100644 --- a/client.go +++ b/client.go @@ -143,7 +143,7 @@ func NewClient(config Config, r *Router) (c *Client, err error) { c.config.ConnectTimeout = 15 // 15 second as default } - c.transport = &XMPPTransport{Config: config.TransportConfiguration} + c.transport = NewTransport(config.TransportConfiguration) return } diff --git a/component.go b/component.go index 4a94389..3301c96 100644 --- a/component.go +++ b/component.go @@ -67,7 +67,7 @@ func (c *Component) Connect() error { } func (c *Component) Resume(sm SMState) error { var err error - c.transport = &XMPPTransport{Config: c.ComponentOptions.TransportConfiguration} + c.transport = NewTransport(c.ComponentOptions.TransportConfiguration) if err = c.transport.Connect(); err != nil { return err } diff --git a/transport.go b/transport.go index e9a35db..2abd3e3 100644 --- a/transport.go +++ b/transport.go @@ -3,6 +3,7 @@ package xmpp import ( "crypto/tls" "errors" + "strings" ) var TLSNotSupported = errors.New("Transport does not support StartTLS") @@ -29,7 +30,14 @@ type Transport interface { Close() error } +// NewTransport creates a new Transport instance. +// The type of transport is determined by the address in the configuration: +// - if the address is a URL with the `ws` or `wss` scheme WebsocketTransport is used +// - in all other cases a XMPPTransport is used +// For XMPPTransport it is mandatory for the address to have a port specified. func NewTransport(config TransportConfiguration) Transport { + if strings.HasPrefix(config.Address, "ws:") || strings.HasPrefix(config.Address, "wss:") { + return &WebsocketTransport{Config: config} + } return &XMPPTransport{Config: config} - }