Sync examples

This commit is contained in:
Kegan Dougal 2016-12-09 16:16:45 +00:00
parent 76fa23b9a4
commit e989121b7a
1 changed files with 17 additions and 2 deletions

View File

@ -4,14 +4,29 @@ import (
"fmt" "fmt"
) )
func Example() { func Example_blockingSync() {
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "MDAefhiuwehfuiwe") cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "MDAefhiuwehfuiwe")
syncer := cli.Syncer.(*DefaultSyncer) syncer := cli.Syncer.(*DefaultSyncer)
syncer.OnEventType("m.room.message", func(ev *Event) { syncer.OnEventType("m.room.message", func(ev *Event) {
fmt.Println("Message: ", ev) fmt.Println("Message: ", ev)
}) })
// To make the example non-blocking, call Sync() in a goroutine.
if err := cli.Sync(); err != nil { if err := cli.Sync(); err != nil {
fmt.Println("Sync() returned ", err) fmt.Println("Sync() returned ", err)
} }
} }
func Example_nonBlockingSync() {
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)
})
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.
}
}()
}