Pārlūkot izejas kodu

Write to websocket only on plugins change

Gildas Chabot 8 gadi atpakaļ
vecāks
revīzija
6728e851bc
1 mainītis faili ar 8 papildinājumiem un 3 dzēšanām
  1. 8 3
      main.go

+ 8 - 3
main.go

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