2
0
mirror of https://github.com/FluuxIO/go-xmpp synced 2024-11-11 07:11:03 +00:00
go-xmpp/transport.go

54 lines
1.5 KiB
Go
Raw Normal View History

2019-10-06 17:37:56 +00:00
package xmpp
import (
"crypto/tls"
"encoding/xml"
2019-10-11 05:15:47 +00:00
"errors"
"io"
2019-10-11 05:19:55 +00:00
"strings"
2019-10-06 17:37:56 +00:00
)
2019-10-11 05:15:47 +00:00
var TLSNotSupported = errors.New("Transport does not support StartTLS")
type TransportConfiguration struct {
// Address is the XMPP Host and port to connect to. Host is of
// the form 'serverhost:port' i.e "localhost:8888"
Address string
Domain string
ConnectTimeout int // Client timeout in seconds. Default to 15
// tls.Config must not be modified after having been passed to NewClient. Any
// changes made after connecting are ignored.
TLSConfig *tls.Config
CharsetReader func(charset string, input io.Reader) (io.Reader, error) // passed to xml decoder
}
2019-10-06 17:37:56 +00:00
type Transport interface {
Connect() (string, error)
2019-10-06 17:37:56 +00:00
DoesStartTLS() bool
StartTLS() error
2019-10-06 17:37:56 +00:00
LogTraffic(logFile io.Writer)
GetDecoder() *xml.Decoder
2019-10-11 05:15:47 +00:00
IsSecure() bool
Ping() error
2019-10-06 17:37:56 +00:00
Read(p []byte) (n int, err error)
Write(p []byte) (n int, err error)
Close() error
}
2019-10-11 05:19:55 +00:00
// 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 {
2019-10-11 05:19:55 +00:00
if strings.HasPrefix(config.Address, "ws:") || strings.HasPrefix(config.Address, "wss:") {
return &WebsocketTransport{Config: config}
}
config.Address = ensurePort(config.Address, 5222)
return &XMPPTransport{Config: config}
}