From e4182896ecd769faac2ec3c8be7f62d1b4bbb6ce Mon Sep 17 00:00:00 2001 From: tympanix Date: Thu, 28 Jun 2018 00:46:02 +0200 Subject: [PATCH] feat: websocket parse JSON resolves #44 --- api/api.go | 14 +++++++++++--- api/api_subtitle.go | 3 +-- api/api_websocket.go | 4 ---- notify/entry.go | 5 +++++ web/js/websocket.js | 25 +++++++++++++++++-------- 5 files changed, 34 insertions(+), 17 deletions(-) diff --git a/api/api.go b/api/api.go index ad4f063..92b61f3 100644 --- a/api/api.go +++ b/api/api.go @@ -3,9 +3,10 @@ package api import ( "encoding/json" "errors" - "fmt" + "math/rand" "net/http" + "github.com/apex/log" "github.com/fatih/set" "github.com/gorilla/mux" "github.com/tympanix/supper/notify" @@ -117,11 +118,18 @@ func NewError(err error, status int) Error { } } -func (a *API) sendToWebsocket() chan<- *notify.Entry { +func (a *API) asyncSendToWebsocket() chan<- *notify.Entry { c := make(chan *notify.Entry) + job := rand.Uint32() go func() { for v := range c { - fmt.Println(v) + v.Context = v.Context.WithField("job", job) + data, err := json.Marshal(v) + if err != nil { + log.WithError(err).Error("Websocket error") + continue + } + a.Broadcast(data) } }() return c diff --git a/api/api_subtitle.go b/api/api_subtitle.go index 56b01fd..0392d2e 100644 --- a/api/api_subtitle.go +++ b/api/api_subtitle.go @@ -216,9 +216,8 @@ func (a *API) downloadSubtitles(w http.ResponseWriter, r *http.Request) interfac return NewError(errors.New("subtitle already satisfied"), http.StatusAccepted) } - c := a.sendToWebsocket() - go func() { + c := a.asyncSendToWebsocket() defer close(c) subs, err := a.DownloadSubtitles(media, langs, c) if err != nil { diff --git a/api/api_websocket.go b/api/api_websocket.go index bc3090d..8cda7a6 100644 --- a/api/api_websocket.go +++ b/api/api_websocket.go @@ -58,10 +58,6 @@ func (api *API) serveWebsocket(w http.ResponseWriter, r *http.Request) { return } api.Register(conn) - go func() { - time.Sleep(1 * time.Second) - api.Broadcast([]byte("hello websocket!")) - }() } // Register registers a new client in the hub diff --git a/notify/entry.go b/notify/entry.go index 225cfc6..3be56aa 100644 --- a/notify/entry.go +++ b/notify/entry.go @@ -35,6 +35,11 @@ func (l Level) String() string { } } +// MarshalJSON returns the notification level as a string +func (l Level) MarshalJSON() ([]byte, error) { + return []byte("\"" + l.String() + "\""), nil +} + // Fields is a collection of key, value pairs type Fields map[string]interface{} diff --git a/web/js/websocket.js b/web/js/websocket.js index baefe6c..0704a0c 100644 --- a/web/js/websocket.js +++ b/web/js/websocket.js @@ -1,14 +1,23 @@ -var loc = window.location, new_uri; -if (loc.protocol === "https:") { - new_uri = "wss:"; -} else { - new_uri = "ws:"; -} -new_uri += "//" + loc.host; -new_uri += loc.pathname + "ws"; +import Snackbar from './comp/Snackbar' var ws = new WebSocket("ws://" + document.location.host + "/api/ws") ws.onmessage = function(event) { console.log(event.data) + var data + try { + data = JSON.parse(event.data) + } catch (e) { + console.log(e) + return Snackbar.error("Websocket", "Could not read websocket message") + } + if (data.level == "info") { + Snackbar.notify("Update", data.message) + } else if (data.level == "error") { + Snackbar.error("Update", data.message) + } else if (data.level == "warn") { + Snackbar.warning("Update", data.message) + } else { + Snackbar.error("Update", data.message) + } }