-
Notifications
You must be signed in to change notification settings - Fork 0
/
math-register.go
67 lines (51 loc) · 1.49 KB
/
math-register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"flag"
"fmt"
"log"
"net/url"
"strconv"
"github.com/koding/kite"
"github.com/koding/kite/config"
)
var (
flagPort = flag.Int("port", 6667, "Port to bind")
flagTransport = flag.String("transport", "", "Change underlying transport")
)
func main() {
flag.Parse()
// Create a kite
k := kite.New("math", "1.0.0")
// Add our handler method
k.HandleFunc("square", Square)
// Get config from kite.Key directly, usually it's under ~/.kite/kite.key
c := config.MustGet()
k.Config = c
k.Config.Port = *flagPort
k.SetLogLevel(kite.DEBUG)
k.Id = c.Id
// by default it's already WebSocket
if *flagTransport != "" && *flagTransport == "xhrpolling" {
k.Config.Transport = config.XHRPolling
}
// Register to kite with this url
kiteURL := &url.URL{Scheme: "http", Host: "localhost:" + strconv.Itoa(*flagPort), Path: "/kite"}
// Register us ...
err := k.RegisterForever(kiteURL)
if err != nil {
log.Fatal(err)
}
// And finally attach to a server and run it
k.Run()
}
func Square(r *kite.Request) (interface{}, error) {
// Unmarshal method arguments
a := r.Args.One().MustFloat64()
result := a * a
fmt.Printf("Call received, sending result %.0f back\n", result)
// Print a log on remote Kite.
// This message will be printed on client's console.
r.Client.Go("kite.log", fmt.Sprintf("Message from %s: \"You have requested square of %.0f\"", r.LocalKite.Kite().Name, a))
// You can return anything as result, as long as it is JSON marshalable.
return result, nil
}