()
| 9 | ) |
| 10 | |
| 11 | func main() { |
| 12 | file := "file.txt" |
| 13 | |
| 14 | m := melody.New() |
| 15 | w, _ := fsnotify.NewWatcher() |
| 16 | |
| 17 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 18 | http.ServeFile(w, r, "index.html") |
| 19 | }) |
| 20 | |
| 21 | http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { |
| 22 | m.HandleRequest(w, r) |
| 23 | }) |
| 24 | |
| 25 | m.HandleConnect(func(s *melody.Session) { |
| 26 | content, _ := os.ReadFile(file) |
| 27 | s.Write(content) |
| 28 | }) |
| 29 | |
| 30 | go func() { |
| 31 | for { |
| 32 | ev := <-w.Events |
| 33 | if ev.Op == fsnotify.Write { |
| 34 | content, _ := os.ReadFile(ev.Name) |
| 35 | m.Broadcast(content) |
| 36 | } |
| 37 | } |
| 38 | }() |
| 39 | |
| 40 | w.Add(file) |
| 41 | |
| 42 | http.ListenAndServe(":5000", nil) |
| 43 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…