Added multiple client connections in go rutines
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
"strconv"
|
||||
|
||||
appConfig "github.com/xmpploadtesting/config"
|
||||
"gosrc.io/xmpp"
|
||||
@@ -71,7 +71,7 @@ var xmppService XMPPService
|
||||
|
||||
// Init Initialise XMPP servie, and router
|
||||
func Init() {
|
||||
// TODO load configs and init clients from the config file
|
||||
|
||||
xmppService = XMPPService{
|
||||
Router: xmpp.NewRouter(),
|
||||
}
|
||||
@@ -87,37 +87,49 @@ func Init() {
|
||||
log.Printf("Client credentials length %v", len(clientCredentials))
|
||||
|
||||
xmppService.XMPPClients = make([]XMPPClient, 0)
|
||||
xmppClient := XMPPClient{
|
||||
Config: xmpp.Config{
|
||||
|
||||
TransportConfiguration: xmpp.TransportConfiguration{
|
||||
Address: appConfig.AppConfig.Service.Address + ":" + appConfig.AppConfig.Service.Port,
|
||||
Domain: appConfig.AppConfig.Service.Domain,
|
||||
TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
for _, credential := range clientCredentials {
|
||||
log.Printf("Host %v", credential.Host+":"+strconv.Itoa(credential.Port))
|
||||
log.Printf("Jid %v", credential.Jid+"@"+credential.Host)
|
||||
log.Printf("Port %v", credential.Port)
|
||||
log.Printf("Credential %v", credential.Credential)
|
||||
|
||||
xmppClient := XMPPClient{
|
||||
Config: xmpp.Config{
|
||||
|
||||
TransportConfiguration: xmpp.TransportConfiguration{
|
||||
Address: credential.Host + ":" + strconv.Itoa(credential.Port),
|
||||
Domain: credential.Host,
|
||||
TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
Jid: credential.Jid + "@" + credential.Host,
|
||||
Credential: xmpp.Password(credential.Credential),
|
||||
StreamLogger: os.Stdout,
|
||||
Insecure: true,
|
||||
},
|
||||
Jid: "admin@localhost",
|
||||
Credential: xmpp.Password("openadmin123"),
|
||||
StreamLogger: os.Stdout,
|
||||
Insecure: true,
|
||||
},
|
||||
}
|
||||
|
||||
go func() {
|
||||
|
||||
client, err := xmpp.NewClient(&xmppClient.Config, xmppService.Router, errorHandler)
|
||||
|
||||
// Client connection
|
||||
if err := client.Connect(); err != nil {
|
||||
msg := fmt.Sprintf("XMPP connection failed: %v", err)
|
||||
|
||||
fmt.Printf("Failed to connect to server. Exiting... %v", msg)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Unable to initialise client for %v", xmppClient.Config.Jid)
|
||||
}
|
||||
xmppClient.Client = client
|
||||
xmppService.XMPPClients = append(xmppService.XMPPClients, xmppClient)
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
client, err := xmpp.NewClient(&xmppClient.Config, xmppService.Router, errorHandler)
|
||||
|
||||
// Client connection
|
||||
if err := client.Connect(); err != nil {
|
||||
msg := fmt.Sprintf("XMPP connection failed: %v", err)
|
||||
|
||||
fmt.Printf("Failed to connect to server. Exiting... %v", msg)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Unable to initialise client for %v", xmppClient.Config.Jid)
|
||||
}
|
||||
xmppClient.Client = client
|
||||
xmppService.XMPPClients = append(xmppService.XMPPClients, xmppClient)
|
||||
|
||||
}
|
||||
|
||||
// SendOnlinePresenceStanza - send online presence stnza with data, to server
|
||||
@@ -125,14 +137,9 @@ func SendOnlinePresenceStanza(client *xmpp.Client, jid string) error {
|
||||
onlinePresencePacket := stanza.NewPresence(stanza.Attrs{From: jid, Type: stanza.StanzaType(stanza.PresenceShowChat)})
|
||||
onlinePresencePacket.Status = rolingStockPresenceMessage
|
||||
|
||||
//TODO All clients should send messages at once
|
||||
for {
|
||||
|
||||
err := client.Send(onlinePresencePacket)
|
||||
log.Printf("Sending online presence stanza: %v", err)
|
||||
// Delay before sending another message
|
||||
time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.DelayBetweenMassages))
|
||||
}
|
||||
err := client.Send(onlinePresencePacket)
|
||||
log.Printf("Sending online presence stanza: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) {
|
||||
|
||||
Reference in New Issue
Block a user