Giter Club home page Giter Club logo

lrserver's Introduction

lrserver LiveReload server for Go

Golang package that implements a simple LiveReload server as described in the LiveReload protocol.

Using the recommended default port 35729:

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.

Full Documentation: GoDoc

Basic Usage

Get Package

go get github.com/jaschaephraim/lrserver

Import Package

import "github.com/jaschaephraim/lrserver"

Instantiate Server

lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)

Start Server

go func() {
    err := lr.ListenAndServe()
    if err != nil {
        // Handle error
    }
}()

Send Messages to the Browser

lr.Reload("file")
lr.Alert("message")

Example

import (
    "log"
    "net/http"

	"github.com/fsnotify/fsnotify"
	"github.com/jaschaephraim/lrserver"
)

// 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 Example() {
    // Create file watcher
    watcher, err := fsnotify.NewWatcher()
    if err != nil {
        log.Fatalln(err)
    }
    defer watcher.Close()

    // Add dir to watcher
    err = watcher.Add("/path/to/watched/dir")
    if err != nil {
        log.Fatalln(err)
    }

    // Create and 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)
}

lrserver's People

Contributors

cryptix avatar deankarn avatar gregmagolan avatar jaschaephraim avatar nochso avatar zhuharev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

lrserver's Issues

Keeps Requesting new Webosckets

Hi,

first: thanks for this package, I was looking for soemthing like this for a long time! I'm also glad that you didn't integrate the watching yourself but let the users choose how they want to notify clients to reload.

Now to my confusion.. It seems to me that the javascript code tries to create a new websocket all the time. From my experience with websockets, there should only be one request and than an upgrade and further communication is done through frames on that connection.

I see it on both firefox30 and chromium 35.
keepscalling101_ff
keepscalling101_ch

Firefox also throws an NS_ERROR_NOT_CONNECTED error and points to livereload.js:211 which is here in the javascript.go file.

I'm not sure what's going on and to be clear: Triggering reloads works. It's just that these messages are a bit annoying while debbugging other requests.

Have you seen this before? If not it would be great if you could try it with the small markdown reloader i wrote: livefriday. Could also be that I messed something up during setup.

Thanks.

License?

Thanks for a useful bit of software! What license do you intend to publish this under? I'm guessing that it might be the same as the included livereload Javascript (MIT), but it would be great to make it explicit.

Middleware support?

Hey thanks for creating this :-)

I was wondering if it makes sense in the scope of this project for the livereload server to implement an http.Handler interface?

It would allow less port binding and would be a bit easier to integrate into existing dev servers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.