()
| 11 | var idCounter atomic.Int64 |
| 12 | |
| 13 | func main() { |
| 14 | m := melody.New() |
| 15 | |
| 16 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 17 | http.ServeFile(w, r, "index.html") |
| 18 | }) |
| 19 | |
| 20 | http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { |
| 21 | m.HandleRequest(w, r) |
| 22 | }) |
| 23 | |
| 24 | m.HandleConnect(func(s *melody.Session) { |
| 25 | id := idCounter.Add(1) |
| 26 | |
| 27 | s.Set("id", id) |
| 28 | |
| 29 | s.Write([]byte(fmt.Sprintf("iam %d", id))) |
| 30 | }) |
| 31 | |
| 32 | m.HandleDisconnect(func(s *melody.Session) { |
| 33 | if id, ok := s.Get("id"); ok { |
| 34 | m.BroadcastOthers([]byte(fmt.Sprintf("dis %d", id)), s) |
| 35 | } |
| 36 | }) |
| 37 | |
| 38 | m.HandleMessage(func(s *melody.Session, msg []byte) { |
| 39 | if id, ok := s.Get("id"); ok { |
| 40 | m.BroadcastOthers([]byte(fmt.Sprintf("set %d %s", id, msg)), s) |
| 41 | } |
| 42 | }) |
| 43 | |
| 44 | http.ListenAndServe(":5000", nil) |
| 45 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…