-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample_simple.go
59 lines (49 loc) · 1.38 KB
/
example_simple.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
package main
import (
"flag"
"fmt"
"github.com/trevex/golem"
"log"
"net/http"
)
var addr = flag.String("addr", ":8080", "http service address")
// This struct represents the message which is accepted by
// the hello-function.
// If a handler function takes a special data
// type that is not an byte array, golem automatically
// tries to unmarshal the data into the specific type.
type Hello struct {
To string `json:"to"`
From string `json:"from"`
}
// Type of data being emitted with answer-Event
type Answer struct {
Msg string `json:"msg"`
}
// Function taken special data type and utilizing golem's
// inbuilt unmarshalling
func hello(conn *golem.Connection, data *Hello) {
fmt.Println("Hello from", data.From, "to", data.To)
conn.Emit("answer", &Answer{"Thanks, client!"})
}
// Event but no data transmission
func poke(conn *golem.Connection) {
fmt.Println("Poke-Event triggered!")
conn.Emit("answer", &Answer{"Ouch I am sensible!"})
}
func main() {
flag.Parse()
// Create a router
myrouter := golem.NewRouter()
// Add the events to the router
myrouter.On("hello", hello)
myrouter.On("poke", poke)
// Serve the public files
http.Handle("/", http.FileServer(http.Dir("./public")))
// Handle websockets using golems handler
http.HandleFunc("/ws", myrouter.Handler())
// Listen
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}