-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
67 lines (58 loc) · 1.69 KB
/
main.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 (
"fmt"
"github.com/gorilla/websocket"
"log"
"net/http"
_ "net/http/pprof"
"strings"
)
var (
rooms = map[string]Room{}
upgrader = websocket.Upgrader{} // configure the upgrader
config = map[string]string{}
DefaultError = map[string]string{"ErrorReason": "You sent in a request with invalid json"}
AuthError = map[string]string{"ErrorReason": "You are not authorized to perform this action"}
passcodesToRooms = map[string]string{"blue123": "commBlue"," red456": "commRed"}
)
// room names
const (
COMM_BLUE = "commBlue"
COMM_GREEN = "commGreen"
COMM_RED = "commRed"
LOCATION_BLUE = "locationBlue"
LOCATION_RED = "locationRed"
)
func handleConnections(writer http.ResponseWriter, request *http.Request) {
roomName := passcodesToRooms[strings.Split(request.URL.RawQuery, "=")[1]]
room := rooms[roomName]
ws, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
log.Fatal(err)
}
// call addConnection
fmt.Sprintf("registering client to room")
registerClient(ws, room)
serveForever(ws, room)
}
func initRooms() {
rooms [COMM_BLUE] = buildRoom()
rooms [COMM_GREEN] = buildRoom()
rooms [COMM_RED] = buildRoom()
rooms [LOCATION_BLUE] = buildRoom()
rooms [LOCATION_RED] = buildRoom()
for _, room := range rooms {
go handleMessages(room)
}
}
func main() {
http.HandleFunc("/ws", handleConnections)
// Start listening for incoming chat messages
initRooms()
// Start the server on localhost port 5000 and log any errors
log.Println("http server started on :5000")
err := http.ListenAndServe(":5000", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}