* add websocket output

* add tests

* update mod
This commit is contained in:
Dima Golomozy
2023-01-10 09:10:23 +03:00
committed by GitHub
parent 23825d82f5
commit 99e6fdfd60
6 changed files with 237 additions and 2 deletions
+64
View File
@@ -0,0 +1,64 @@
package main
import (
"github.com/gorilla/websocket"
"log"
"net/http"
"sync"
"testing"
)
func TestWebSocketOutput(t *testing.T) {
wg := new(sync.WaitGroup)
wsAddr := startWebsocket(func(data []byte) {
wg.Done()
})
input := NewTestInput()
output := NewWebSocketOutput(wsAddr, &WebSocketOutputConfig{Workers: 1})
plugins := &InOutPlugins{
Inputs: []PluginReader{input},
Outputs: []PluginWriter{output},
}
emitter := NewEmitter()
go emitter.Start(plugins, Settings.Middleware)
for i := 0; i < 10; i++ {
wg.Add(1)
input.EmitGET()
}
wg.Wait()
emitter.Close()
}
func startWebsocket(cb func([]byte)) string {
upgrader := websocket.Upgrader{}
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
go func(conn *websocket.Conn) {
defer conn.Close()
for {
_, msg, _ := conn.ReadMessage()
cb(msg)
}
}(c)
})
go func() {
err := http.ListenAndServe("localhost:8081", nil)
if err != nil {
log.Fatal("Can't start:", err)
}
}()
return "ws://localhost:8081/test"
}