-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample_chat.go
89 lines (74 loc) · 2.42 KB
/
example_chat.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"flag"
"fmt"
"github.com/trevex/golem"
"log"
"net/http"
)
var addr = flag.String("addr", ":8080", "http service address")
// Create room manager
var myroommanager = golem.NewRoomManager()
// Every join and leave request will specify the name of the room
// the connection wants to join or leave.
type RoomRequest struct {
Name string `json:"name"`
}
// On join, join the room with the specified name.
func join(conn *golem.Connection, data *RoomRequest) {
fmt.Println("Joining", data.Name)
myroommanager.Join(data.Name, conn)
}
// On leave, leave the specified room.
func leave(conn *golem.Connection, data *RoomRequest) {
fmt.Println("Leaving", data.Name)
myroommanager.Leave(data.Name, conn)
}
// Every message has a room it broadcast to and the actual message content.
type RoomMessage struct {
To string `json:"to"`
Msg string `json:"msg"`
}
// Emit the msg event to every member of the To-Room with the provided message content.
func msg(conn *golem.Connection, data *RoomMessage) {
fmt.Println("Sending to", data.To)
myroommanager.Emit(data.To, "msg", &data.Msg)
}
// Make sure the connection leaves all lobbies.
// When roommanager is used, this is a necessary step! Otherwise the member counting of the manager won't
// be accurate anymore. If the connection didn't join or already left all lobbies this will result in a single if
// check and therefore is not costly.
func connClose(conn *golem.Connection) {
myroommanager.LeaveAll(conn)
}
// If a room is created or removed because of insufficient users
// print the name!
// ( The functions need to be of the type func(string) and receive the rooms name as argument )
func roomCreated(name string) {
fmt.Println("Room created:", name)
}
func roomRemoved(name string) {
fmt.Println("Room removed:", name)
}
func main() {
flag.Parse()
// Create a router
myrouter := golem.NewRouter()
// Add the events to the router
myrouter.On("join", join)
myrouter.On("leave", leave)
myrouter.On("msg", msg)
myrouter.OnClose(connClose)
// React on room manager events
myroommanager.On("create", roomCreated)
myroommanager.On("remove", roomRemoved)
// Serve the public files
http.Handle("/", http.FileServer(http.Dir("./public")))
// Handle websockets using golems handler
http.HandleFunc("/ws", myrouter.Handler())
// Listen
fmt.Println("Listening on", *addr)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}