libnmdc/Example_test.go

61 lines
1.2 KiB
Go

package libnmdc
import (
"fmt"
)
func ExampleConnect() {
opts := HubConnectionOptions{
Address: "127.0.0.1",
Self: *NewUserInfo("slowpoke9"),
}
hub := opts.Connect()
for {
event := <-hub.OnEvent
switch event.EventType {
case EVENT_CONNECTION_STATE_CHANGED:
fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message)
case EVENT_PUBLIC:
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)
if event.Message == "how are you" {
hub.SayPublic("good thanks!")
}
default:
fmt.Printf("%+v\n", event)
}
}
}
func ExampleConnectSync() {
cb := func(hub *HubConnection, event HubEvent) {
switch event.EventType {
case EVENT_CONNECTION_STATE_CHANGED:
fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message)
case EVENT_PUBLIC:
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)
if event.Message == "how are you" {
hub.SayPublic("good thanks!")
}
default:
fmt.Printf("%+v\n", event)
}
}
opts := HubConnectionOptions{
Address: "127.0.0.1",
Self: *NewUserInfo("slowpoke9"),
OnEventSync: cb,
}
opts.ConnectSync() // blocking
}