-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
47 lines (38 loc) · 875 Bytes
/
server.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
package main
import (
"log"
"net/http"
"github.com/gorilla/websocket"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("client")))
http.HandleFunc("/connected", connected)
err := http.ListenAndServe(":6969", nil)
if err != nil {
panic(err)
}
}
var up = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
func connected(w http.ResponseWriter, r *http.Request) {
c, err := up.Upgrade(w, r, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer c.Close()
for {
msgtype, message, err := c.ReadMessage()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
break
}
log.Printf("recv: %s", message)
err = c.WriteMessage(msgtype, message)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}