-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivereload.go
127 lines (111 loc) · 3.02 KB
/
livereload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"flag"
"net/http"
"websocket"
"fmt"
"log"
"exp/fsnotify"
)
const (
API_VERSION = "1.6"
)
var (
hostname = flag.String("hostname", "", "host name")
port = flag.Uint("port", 35729, "port number")
directories = flag.Args()
// http channels
subscriptionChannel = make(chan subscriptionMessage)
messageChannel = make(chan string)
watcher, watcher_err = fsnotify.NewWatcher()
)
type subscriptionMessage struct {
websocket *websocket.Conn
active bool
url string
}
type livereloadClientUpdateMessage struct {
path string
apply_js_live bool
apply_css_live bool
apply_images_live bool
}
func eventHub() {
connections := make(map[*websocket.Conn]string)
for {
select {
case subscriptionMessage := <-subscriptionChannel:
if subscriptionMessage.active { // on connection
connections[subscriptionMessage.websocket] = subscriptionMessage.url
} else { // on disconnection
delete(connections, subscriptionMessage.websocket)
}
case message := <-messageChannel:
if len(message) == 0 {
fmt.Printf("close ws requested")
}
fmt.Printf("Incomming Message: %s", message)
case ev := <-watcher.Event:
//update := livereloadClientUpdateMessage{ev.Name, true, true, true}
for k, _ := range connections {
websocket.Message.Send(k, fmt.Sprintf("[\"refresh\": {\"path\": \"%s\"}]", ev.Name))
//websocket.JSON.Send(k, update)
}
/*'["refresh", {
"path" : %s,
"apply_js_live": true,
"apply_css_live" : true
"apply_images_live" : true
}]'*/
log.Println("fsevent:", ev)
case err := <-watcher.Error:
log.Println("fserror:", err)
}
}
}
func liveReload16ConnectionHandler(ws *websocket.Conn) {
defer func() {
subscriptionChannel <- subscriptionMessage{ws, false, ""}
fmt.Printf("Browser disconnected")
ws.Close()
}()
websocket.Message.Send(ws, fmt.Sprintf("!!ver:%s", API_VERSION))
fmt.Printf("Browser Connected")
// on connection it's the client url
var onConnectionMessage string
websocket.Message.Receive(ws, &onConnectionMessage)
subscriptionChannel <- subscriptionMessage{ws, true, onConnectionMessage}
fmt.Printf("Browser URL: %s", onConnectionMessage)
// websocket messages from the clients get pushed though the eventhub
for {
var msg string
websocket.Message.Receive(ws, &msg)
messageChannel <- msg
}
}
func websocketHandshakeHandler(w http.ResponseWriter, req *http.Request) {
websocket.Handler(liveReload16ConnectionHandler).ServeHTTP(w, req)
}
func main() {
if watcher_err != nil {
log.Fatal("Unable to create file monitor.")
}
flag.Parse()
// register directories to monitory
if len(directories) == 0 {
directories = make([]string, 1, 1)
directories[0] = "."
}
for _, directory := range directories {
err := watcher.Watch(directory)
if err != nil {
log.Fatal(err)
}
}
go eventHub()
http.HandleFunc("/websocket", websocketHandshakeHandler)
err := http.ListenAndServe(fmt.Sprintf("%s:%d", *hostname, *port), nil)
if err != nil {
log.Fatal("http_server.Start.ListenAndServe: ", err)
}
}