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 := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
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)
}
Index ¶
- Constants
- Variables
- type Server
- func (s *Server) Alert(msg string)
- func (s *Server) Close() error
- func (s *Server) ErrorLog() *log.Logger
- 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) Shutdown(ctx context.Context) error
- func (s *Server) StatusLog() *log.Logger
Examples ¶
Constants ¶
const ( // DefaultName is the livereload Server's default name DefaultName string = "LiveReload" // DefaultPort is the livereload Server's default server port DefaultPort uint16 = 35729 )
Variables ¶
var (
Js string = strings.TrimSpace(js)
)
Functions ¶
This section is empty.
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server contains a single lrserver instance's data
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