Separate stream creation from XMPP use (client, component, etc).
The address of the server is not necessarily related to the JID, e.g. DNS SRV lookups, manual configuration, etc. Splitting them up means there's more control and flexibility over how things are put together. We can always add convenience funcs later, e.g. something like DialClient(jid, password), to handle the common case.
This commit is contained in:
parent
381c04b8b3
commit
c9c8526476
10
client.go
10
client.go
|
|
@ -12,12 +12,20 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Parse args.
|
// Parse args.
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
jid, _ := xmpp.ParseJID(*jid)
|
jid, _ := xmpp.ParseJID(*jid)
|
||||||
password := *password
|
password := *password
|
||||||
|
|
||||||
x, err := xmpp.NewClientXMPP(jid, password, &xmpp.ClientConfig{InsecureSkipVerify: true})
|
// Create stream.
|
||||||
|
stream, err := xmpp.NewStream(jid.Domain + ":5222")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure stream as a client connection.
|
||||||
|
x, err := xmpp.NewClientXMPP(stream, jid, password, &xmpp.ClientConfig{InsecureSkipVerify: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
11
component.go
11
component.go
|
|
@ -7,16 +7,25 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
addr = flag.String("a", "", "Server component address")
|
||||||
jid = flag.String("j", "", "JID")
|
jid = flag.String("j", "", "JID")
|
||||||
secret = flag.String("s", "", "Component secret")
|
secret = flag.String("s", "", "Component secret")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
addr := *addr
|
||||||
jid, _ := xmpp.ParseJID(*jid)
|
jid, _ := xmpp.ParseJID(*jid)
|
||||||
secret := *secret
|
secret := *secret
|
||||||
|
|
||||||
x, err := xmpp.NewComponentXMPP("localhost:5347", jid, secret)
|
// Create stream.
|
||||||
|
stream, err := xmpp.NewStream(addr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure stream as a component connection.
|
||||||
|
x, err := xmpp.NewComponentXMPP(stream, jid, secret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,13 +19,8 @@ type ClientConfig struct {
|
||||||
InsecureSkipVerify bool
|
InsecureSkipVerify bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a client XMPP stream.
|
// Create a client XMPP over the stream.
|
||||||
func NewClientXMPP(jid JID, password string, config *ClientConfig) (*XMPP, error) {
|
func NewClientXMPP(stream *Stream, jid JID, password string, config *ClientConfig) (*XMPP, error) {
|
||||||
|
|
||||||
stream, err := NewStream(jid.Domain + ":5222")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create a component XMPP connection.
|
// Create a component XMPP connection over the stream.
|
||||||
func NewComponentXMPP(addr string, jid JID, secret string) (*XMPP, error) {
|
func NewComponentXMPP(stream *Stream, jid JID, secret string) (*XMPP, error) {
|
||||||
|
|
||||||
stream, err := NewStream(addr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
streamId, err := startComponent(stream, jid)
|
streamId, err := startComponent(stream, jid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue