Added callback to process errors after connection.

Added tests and refactored a bit.
This commit is contained in:
CORNIERE Rémi
2019-12-05 18:12:00 +01:00
parent b74c0f0374
commit e675e65a59
10 changed files with 792 additions and 332 deletions

View File

@@ -0,0 +1,95 @@
package main
/*
xmpp_chat_client is a demo client that connect on an XMPP server to chat with other members
Note that this example sends to a very specific user. User logic is not implemented here.
*/
import (
. "bufio"
"fmt"
"os"
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
)
const (
currentUserAddress = "localhost:5222"
currentUserJid = "testuser@localhost"
currentUserPass = "testpass"
correspondantJid = "testuser2@localhost"
)
func main() {
config := xmpp.Config{
TransportConfiguration: xmpp.TransportConfiguration{
Address: currentUserAddress,
},
Jid: currentUserJid,
Credential: xmpp.Password(currentUserPass),
Insecure: true}
var client *xmpp.Client
var err error
router := xmpp.NewRouter()
router.HandleFunc("message", handleMessage)
if client, err = xmpp.NewClient(config, router, errorHandler); err != nil {
fmt.Println("Error new client")
}
// Connecting client and handling messages
// To use a stream manager, just write something like this instead :
//cm := xmpp.NewStreamManager(client, startMessaging)
//log.Fatal(cm.Run()) //=> this will lock the calling goroutine
if err = client.Connect(); err != nil {
fmt.Printf("XMPP connection failed: %s", err)
return
}
startMessaging(client)
}
func startMessaging(client xmpp.Sender) {
reader := NewReader(os.Stdin)
textChan := make(chan string)
var text string
for {
fmt.Print("Enter text: ")
go readInput(reader, textChan)
select {
case <-killChan:
return
case text = <-textChan:
reply := stanza.Message{Attrs: stanza.Attrs{To: correspondantJid}, Body: text}
err := client.Send(reply)
if err != nil {
fmt.Printf("There was a problem sending the message : %v", reply)
return
}
}
}
}
func readInput(reader *Reader, textChan chan string) {
text, _ := reader.ReadString('\n')
textChan <- text
}
var killChan = make(chan struct{})
// If an error occurs, this is used
func errorHandler(err error) {
fmt.Printf("%v", err)
killChan <- struct{}{}
}
func handleMessage(s xmpp.Sender, p stanza.Packet) {
msg, ok := p.(stanza.Message)
if !ok {
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
return
}
_, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From)
}

View File

@@ -35,7 +35,7 @@ func main() {
IQNamespaces("jabber:iq:version").
HandlerFunc(handleVersion)
component, err := xmpp.NewComponent(opts, router)
component, err := xmpp.NewComponent(opts, router, handleError)
if err != nil {
log.Fatalf("%+v", err)
}
@@ -47,6 +47,10 @@ func main() {
log.Fatal(cm.Run())
}
func handleError(err error) {
fmt.Println(err.Error())
}
func handleMessage(_ xmpp.Sender, p stanza.Packet) {
msg, ok := p.(stanza.Message)
if !ok {

View File

@@ -53,7 +53,7 @@ func main() {
handleIQ(s, p, player)
})
client, err := xmpp.NewClient(config, router)
client, err := xmpp.NewClient(config, router, errorHandler)
if err != nil {
log.Fatalf("%+v", err)
}
@@ -61,6 +61,9 @@ func main() {
cm := xmpp.NewStreamManager(client, nil)
log.Fatal(cm.Run())
}
func errorHandler(err error) {
fmt.Println(err.Error())
}
func handleMessage(s xmpp.Sender, p stanza.Packet, player *mpg123.Player) {
msg, ok := p.(stanza.Message)

View File

@@ -28,7 +28,7 @@ func main() {
router := xmpp.NewRouter()
router.HandleFunc("message", handleMessage)
client, err := xmpp.NewClient(config, router)
client, err := xmpp.NewClient(config, router, errorHandler)
if err != nil {
log.Fatalf("%+v", err)
}
@@ -39,6 +39,10 @@ func main() {
log.Fatal(cm.Run())
}
func errorHandler(err error) {
fmt.Println(err.Error())
}
func handleMessage(s xmpp.Sender, p stanza.Packet) {
msg, ok := p.(stanza.Message)
if !ok {

View File

@@ -26,7 +26,7 @@ func main() {
router := xmpp.NewRouter()
router.HandleFunc("message", handleMessage)
client, err := xmpp.NewClient(config, router)
client, err := xmpp.NewClient(config, router, errorHandler)
if err != nil {
log.Fatalf("%+v", err)
}
@@ -37,6 +37,10 @@ func main() {
log.Fatal(cm.Run())
}
func errorHandler(err error) {
fmt.Println(err.Error())
}
func handleMessage(s xmpp.Sender, p stanza.Packet) {
msg, ok := p.(stanza.Message)
if !ok {