gomatrix/example_test.go

29 lines
584 B
Go
Raw Normal View History

2016-12-09 16:09:48 +00:00
package gomatrix
import (
"fmt"
)
2016-12-09 16:28:41 +00:00
func Example_sync() {
2016-12-09 16:09:48 +00:00
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "MDAefhiuwehfuiwe")
syncer := cli.Syncer.(*DefaultSyncer)
syncer.OnEventType("m.room.message", func(ev *Event) {
fmt.Println("Message: ", ev)
})
2016-12-09 16:28:41 +00:00
// Blocking version
2016-12-09 16:09:48 +00:00
if err := cli.Sync(); err != nil {
fmt.Println("Sync() returned ", err)
}
2016-12-09 16:16:45 +00:00
2016-12-09 16:28:41 +00:00
// Non-blocking version
2016-12-09 16:16:45 +00:00
go func() {
for {
if err := cli.Sync(); err != nil {
fmt.Println("Sync() returned ", err)
}
// Optional: Wait a period of time before trying to sync again.
}
}()
}