From 231bfeb247e9c1a50943292827dc4b2d4cbf8d4f Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 29 Nov 2016 19:30:33 +1300 Subject: [PATCH] sample: add a version using the synchronous API --- libnmdc_sample.go | 1 + libnmdc_sample2.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 libnmdc_sample2.go diff --git a/libnmdc_sample.go b/libnmdc_sample.go index 089888c..6b46a84 100644 --- a/libnmdc_sample.go +++ b/libnmdc_sample.go @@ -2,6 +2,7 @@ // This sample file demonstrates use of the libnmdc.go library. It's excluded // when building the library package, but you can run it via `go run libnmdc_sample.go`. +// This version demonstrates the channel API. package main diff --git a/libnmdc_sample2.go b/libnmdc_sample2.go new file mode 100644 index 0000000..4022e8f --- /dev/null +++ b/libnmdc_sample2.go @@ -0,0 +1,37 @@ +// +build ignore + +// This sample file demonstrates use of the libnmdc.go library. It's excluded +// when building the library package, but you can run it via `go run libnmdc_sample.go`. +// This version demonstrates the sync API. + +package main + +import ( + "fmt" + "libnmdc" +) + +func main() { + + libnmdc.HubConnectionOptions{ + Address: "127.0.0.1", + Self: libnmdc.NewUserInfo("slowpoke9"), + + OnEventSync: func(ev libnmdc.HubEvent) { + switch event.EventType { + case libnmdc.EVENT_CONNECTION_STATE_CHANGED: + fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message) + + case libnmdc.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) + + } + }, + }.Connect() +}