Documentation ¶
Overview ¶
Package lrserver implements a basic LiveReload server.
(See http://feedback.livereload.com/knowledgebase/articles/86174-livereload-protocol .)
Using the recommended default port 35729:
http://localhost:35729/livereload.js
serves the LiveReload client JavaScript, and:
ws://localhost:35729/livereload
communicates with the client via web socket.
File watching must be implemented by your own application, and reload/alert requests sent programmatically.
Multiple servers can be instantiated, and each can support multiple connections.
Example ¶
package main import ( "log" "net/http" "github.com/jaschaephraim/lrserver" "gopkg.in/fsnotify.v1" ) // html includes the client JavaScript const html = `<!doctype html> <html> <head> <title>Example</title> </head> <body> <script src="http://localhost:35729/livereload.js"></script> </body> </html>` func main() { // Create file watcher watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatalln(err) } defer watcher.Close() // Watch dir err = watcher.Add("/path/to/watched/dir") if err != nil { log.Fatalln(err) } // Start LiveReload server lr, err := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort) if err != nil { log.Fatalln(err) } go lr.ListenAndServe() // Start goroutine that requests reload upon watcher event go func() { for { select { case event := <-watcher.Events: lr.Reload(event.Name) case err := <-watcher.Errors: log.Println(err) } } }() // Start serving html http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { rw.Write([]byte(html)) }) http.ListenAndServe(":3000", nil) }
Output:
Index ¶
- Constants
- type Server
- func (s *Server) Addr() string
- func (s *Server) Alert(msg string)
- func (s *Server) ErrorLog() *log.Logger
- func (s *Server) Host() string
- func (s *Server) ListenAndServe() error
- func (s *Server) LiveCSS() bool
- func (s *Server) Name() string
- func (s *Server) Port() uint16
- func (s *Server) Reload(file string)
- func (s *Server) SetErrorLog(l *log.Logger)
- func (s *Server) SetLiveCSS(n bool)
- func (s *Server) SetStatusLog(l *log.Logger)
- func (s *Server) StatusLog() *log.Logger
Examples ¶
Constants ¶
const ( DefaultName string = "LiveReload" DefaultHost string = "" DefaultPort uint16 = 35729 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func (*Server) ErrorLog ¶
ErrorLog gets the server's error logger, which writes to os.Stderr by default
func (*Server) ListenAndServe ¶
func (*Server) SetErrorLog ¶
SetErrorLog sets the server's error logger, which can be set to nil
func (*Server) SetLiveCSS ¶
SetLiveCSS sets the live CSS preference
func (*Server) SetStatusLog ¶
SetStatusLog sets the server's status logger, which can be set to nil