Browse Source

Write to websocket only on plugins change

Gildas Chabot 8 years ago
parent
commit
6728e851bc
1 changed files with 8 additions and 3 deletions
  1. 8 3
      main.go

+ 8 - 3
main.go

@@ -10,7 +10,6 @@ import (
10
 	"os"
10
 	"os"
11
 	"path"
11
 	"path"
12
 	"plugin"
12
 	"plugin"
13
-	"time"
14
 
13
 
15
 	"github.com/fsnotify/fsnotify"
14
 	"github.com/fsnotify/fsnotify"
16
 	"golang.org/x/net/websocket"
15
 	"golang.org/x/net/websocket"
@@ -79,6 +78,7 @@ func getPlugins(pluginDir string) (map[int][]*Transformer, error) {
79
 }
78
 }
80
 
79
 
81
 var processed image.Image
80
 var processed image.Image
81
+var listeners []chan bool
82
 
82
 
83
 func processImage(events <-chan fsnotify.Event) {
83
 func processImage(events <-chan fsnotify.Event) {
84
 	for {
84
 	for {
@@ -99,6 +99,9 @@ func processImage(events <-chan fsnotify.Event) {
99
 			i = p.t(i)
99
 			i = p.t(i)
100
 		}
100
 		}
101
 		processed = i
101
 		processed = i
102
+		for _, l := range listeners {
103
+			l <- true
104
+		}
102
 
105
 
103
 		// Wait for a change in folder
106
 		// Wait for a change in folder
104
 		<-events
107
 		<-events
@@ -110,9 +113,11 @@ func handler(w http.ResponseWriter, r *http.Request) {
110
 }
113
 }
111
 
114
 
112
 func wsHandler(ws *websocket.Conn) {
115
 func wsHandler(ws *websocket.Conn) {
113
-	for true {
116
+	l := make(chan bool)
117
+	listeners = append(listeners, l) // Should have mutex
118
+	for {
114
 		fmt.Fprintf(ws, "echp")
119
 		fmt.Fprintf(ws, "echp")
115
-		time.Sleep(200 * time.Millisecond)
120
+		<-l
116
 	}
121
 	}
117
 }
122
 }
118
 
123