MCPcopy
hub / github.com/olahol/melody / main

Function main

examples/filewatch/main.go:11–43  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9)
10
11func 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}

Callers

nothing calls this directly

Calls 5

NewFunction · 0.92
HandleRequestMethod · 0.80
HandleConnectMethod · 0.80
WriteMethod · 0.80
BroadcastMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…