/
main.go
75 lines (53 loc) · 1.28 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
68
69
70
71
72
73
74
75
package main
import (
"net/http"
"strings"
"github.com/google/uuid"
"github.com/olahol/melody"
)
type GopherInfo struct {
ID, X, Y string
}
func main() {
m := melody.New()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
m.HandleRequest(w, r)
})
m.HandleConnect(func(s *melody.Session) {
ss, _ := m.Sessions()
for _, o := range ss {
value, exists := o.Get("info")
if !exists {
continue
}
info := value.(*GopherInfo)
s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y))
}
id := uuid.NewString()
s.Set("info", &GopherInfo{id, "0", "0"})
s.Write([]byte("iam " + id))
})
m.HandleDisconnect(func(s *melody.Session) {
value, exists := s.Get("info")
if !exists {
return
}
info := value.(*GopherInfo)
m.BroadcastOthers([]byte("dis "+info.ID), s)
})
m.HandleMessage(func(s *melody.Session, msg []byte) {
p := strings.Split(string(msg), " ")
value, exists := s.Get("info")
if len(p) != 2 || !exists {
return
}
info := value.(*GopherInfo)
info.X = p[0]
info.Y = p[1]
m.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s)
})
http.ListenAndServe(":5000", nil)
}