Skip to content

Commit

Permalink
feat: websocket parse JSON resolves #44
Browse files Browse the repository at this point in the history
  • Loading branch information
tympanix committed Jun 27, 2018
1 parent b55ef8d commit e418289
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
14 changes: 11 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions api/api_subtitle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions api/api_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions notify/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
25 changes: 17 additions & 8 deletions web/js/websocket.js
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit e418289

Please sign in to comment.