-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (48 loc) · 1.22 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
package main
import (
"net/http"
"log"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{}
var clients []*websocket.Conn
func removeClient(clientIndex int) {
clients[clientIndex] = clients[len(clients) - 1]
clients = clients[:len(clients) - 1]
}
const serveDir = "compressed/"
func main() {
http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, serveDir + "index.html")
})
http.HandleFunc("/main.js", func (w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, serveDir + "main.js")
})
http.HandleFunc("/main.css", func (w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, serveDir + "main.css")
})
http.HandleFunc("/chatSocket", func (w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
clientIndex := len(clients)
clients = append(clients, c)
defer removeClient(clientIndex)
defer c.Close()
for {
mt, msg, err := c.ReadMessage()
if err != nil {
return
}
for i := 0; i < len(clients); i++ {
err = clients[i].WriteMessage(mt, msg)
if (err != nil) {
return
}
}
}
})
log.Println("Starting server.")
log.Fatal(http.ListenAndServe(":8080", nil))
}