streaming
This commit is contained in:
+44
-15
@@ -19,13 +19,10 @@
|
||||
console.log("update")
|
||||
list = Array.from(document.querySelectorAll('#sortable>li'));
|
||||
var i = list.indexOf(u.item[0])
|
||||
console.log(u.item.attr("id") + " is now " + i)
|
||||
on_update_operation(u.item.attr("id"), i)
|
||||
}
|
||||
});
|
||||
$( "#sortable" ).disableSelection();
|
||||
|
||||
// connect to ppop
|
||||
sock = new WebSocket("ws://localhost:8718/ppop");
|
||||
sock.onopen = function() {
|
||||
console.log("ppop open")
|
||||
@@ -34,31 +31,63 @@
|
||||
console.log("ppop closed")
|
||||
sock = new WebSocket("ws://localhost:8718/ppop");
|
||||
}
|
||||
|
||||
sock.onmessage = function(msg) {
|
||||
console.log(msg.data)
|
||||
check_fix_list(JSON.parse(msg.data))
|
||||
}
|
||||
|
||||
// setInterval(function() {
|
||||
// sock.send(JSON.stringify({"op": "tick", "id": "0", "index": 0}));
|
||||
// },3000);
|
||||
|
||||
} );
|
||||
var sock = null;
|
||||
function on_update_operation(id, index) {
|
||||
if(sock) {
|
||||
console.log("sending")
|
||||
sock.send(JSON.stringify({"op": "move", "id": id, "index": index}));
|
||||
sock.send(JSON.stringify({"op": "move", "id": id, "index": index}))
|
||||
}
|
||||
}
|
||||
function check_fix_list(playlist) {
|
||||
|
||||
console.log(playlist)
|
||||
|
||||
$("#pname").text("Playlist: " + playlist.name)
|
||||
$("#tname").text("Current Track: " + playlist.ctrack)
|
||||
$("#aname").text("Current Album: " + playlist.calbum)
|
||||
|
||||
|
||||
list = Array.from(document.querySelectorAll('#sortable>li'))
|
||||
|
||||
|
||||
console.log("length ul: " + list.length)
|
||||
console.log("length playlist: " + playlist.list.length)
|
||||
|
||||
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
if(i >= playlist.list.length) {
|
||||
break
|
||||
} else if (list[i].id != playlist.list[i]["id"]) {
|
||||
console.log("difference at index: " + i)
|
||||
list[i].setAttribute("id", playlist.list[i]["id"])
|
||||
list[i].innerHTML = "<span>" + playlist.list[i]["maker"] +
|
||||
" - " + playlist.list[i]["album"] +
|
||||
" - " + playlist.list[i]["name"] + "</span>" ;
|
||||
}
|
||||
}
|
||||
|
||||
var ul = document.getElementById('sortable');
|
||||
if(list.length > playlist.list.length) {
|
||||
for(let i = playlist.list.length; i < list.length; i++) {
|
||||
ul.removeChild(list[i])
|
||||
}
|
||||
} else if (list.length < playlist.list.length) {
|
||||
|
||||
}
|
||||
console.log("synched!!")
|
||||
}
|
||||
</script>
|
||||
<title>Radiodiodio</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Playlist: {{.NAME}}</h1>
|
||||
|
||||
<h2>Current Track: {{.CTRACK.NAME}} </h2>
|
||||
<h2>Current Album : {{.CALBUM.NAME}} - {{.CALBUM.MAKER}}</h2>
|
||||
<h1 id="pname">Playlist: {{.NAME}}</h1>
|
||||
<audio controls src="http://192.168.1.80:8088/wwww"></audio>
|
||||
<h2 id="tname">Current Track: {{.CTRACK.NAME}}</h2>
|
||||
<h2 id="aname">Current Album : {{.CALBUM.NAME}} - {{.CALBUM.MAKER}}</h2>
|
||||
|
||||
<ul id="sortable">
|
||||
{{range $.LIST}}
|
||||
|
||||
+19
-17
@@ -10,9 +10,11 @@ import (
|
||||
)
|
||||
|
||||
// todo: hub - https://stackoverflow.com/questions/31532652/go-websocket-send-all-clients-a-message
|
||||
var wsc *websocket.Conn
|
||||
|
||||
var pp *playlist.PrettyPlaylist
|
||||
var p *playlist.Playlist
|
||||
var pp *playlist.PrettyPlaylist
|
||||
var mp *playlist.MinimalPlaylist
|
||||
|
||||
var pln = log.Println
|
||||
|
||||
@@ -29,7 +31,9 @@ var upgrader = websocket.Upgrader{
|
||||
|
||||
func Init(playlist *playlist.Playlist) {
|
||||
p = playlist
|
||||
p.CALLB = pop_callback
|
||||
pp = p.Pretty()
|
||||
mp = p.Minimal()
|
||||
http.HandleFunc("/pp", pp_handler)
|
||||
http.HandleFunc("/ppop", pp_operations)
|
||||
}
|
||||
@@ -51,10 +55,11 @@ func pp_operations(w http.ResponseWriter, r *http.Request) {
|
||||
pln(err)
|
||||
return
|
||||
}
|
||||
wsc = c // this should be hub
|
||||
go readop(c)
|
||||
}
|
||||
|
||||
func readop(c *websocket.Conn) {
|
||||
func readop(c *websocket.Conn) {
|
||||
for {
|
||||
opdata := &op_t{}
|
||||
if err := c.ReadJSON(&opdata); err != nil {
|
||||
@@ -66,27 +71,24 @@ func readop(c *websocket.Conn) {
|
||||
if id, err := strconv.ParseUint(opdata.ID, 10, 32); err == nil {
|
||||
p.Move(uint32(id), opdata.INDEX)
|
||||
pp = p.Pretty()
|
||||
pp.Print()
|
||||
mp = p.Minimal()
|
||||
// pp.Print()
|
||||
}
|
||||
}
|
||||
|
||||
pln(opdata.OP)
|
||||
pln(opdata.ID)
|
||||
pln(opdata.INDEX)
|
||||
if err := c.WriteJSON(opdata); err != nil {
|
||||
if err := c.WriteJSON(mp); err != nil {
|
||||
pln(err)
|
||||
return //connection lost?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// func pp_operations(ws *websocket.Conn) {
|
||||
// opdata := &op_t{}
|
||||
// if err := websocket.JSON.Receive(ws, &opdata); err != nil {
|
||||
// pln(err)
|
||||
// }
|
||||
// // websocket.JSON.Send(ws, "ok")
|
||||
// pln(opdata.OP)
|
||||
// pln(opdata.ID)
|
||||
// pln(opdata.INDEX)
|
||||
// }
|
||||
func pop_callback(list *playlist.Playlist) {
|
||||
if list == p {
|
||||
pp = p.Pretty()
|
||||
mp = p.Minimal()
|
||||
if wsc != nil {
|
||||
wsc.WriteJSON(mp) // this should be hub broadcasted
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user