Files
melody/examples/filewatch/main.go
T
2022-11-25 17:42:08 +01:00

44 lines
721 B
Go

package main
import (
"io/ioutil"
"net/http"
"github.com/fsnotify/fsnotify"
"github.com/olahol/melody"
)
func main() {
file := "file.txt"
m := melody.New()
w, _ := fsnotify.NewWatcher()
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) {
content, _ := ioutil.ReadFile(file)
s.Write(content)
})
go func() {
for {
ev := <-w.Events
if ev.Op == fsnotify.Write {
content, _ := ioutil.ReadFile(ev.Name)
m.Broadcast(content)
}
}
}()
w.Add(file)
http.ListenAndServe(":5000", nil)
}