-
Notifications
You must be signed in to change notification settings - Fork 2
/
wwwcats.go
54 lines (41 loc) · 1.15 KB
/
wwwcats.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
package main
import (
"flag"
"log"
"net/http"
"github.com/gorilla/websocket"
)
var addr = flag.String("l", ":8080", "http service address")
var REVISION = 9
func main() {
flag.Parse()
// Create a global list of lobbies
lobbies := make(map[string]*Lobby)
// Serve the client-side software
fs := http.FileServer(http.Dir("public_html"))
http.Handle("/", fs)
// Handle incoming websocket connections
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
handleConnections(w, r, lobbies)
})
// Start the server
log.Println("Now listening on", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
// Upgrade incoming connections to websockets
func handleConnections(w http.ResponseWriter, r *http.Request, lobbies map[string]*Lobby) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
// Instantiate the new client object
client := &Client{conn: conn, send: make(chan []byte, 256)}
// Hand the client off to these goroutines which will handle all i/o
go client.readPump(lobbies)
go client.writePump()
}