This commit is contained in:
gauthiier
2019-01-02 16:11:22 +01:00
commit 816c2b593c
11 changed files with 890 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
<!doctype html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<style type="text/css">
#sortable { list-style-type: none; margin: 10; padding: 10; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; height: 18px; }
#sortable li span { position: absolute; margin-left: -1.3em; }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#sortable" ).sortable({
update: function (e, u) {
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")
}
sock.onclose = function() {
console.log("ppop closed")
sock = new WebSocket("ws://localhost:8718/ppop");
}
sock.onmessage = function(msg) {
console.log(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": "update", "id": id, "index": index}));
}
}
</script>
<title>Radiodiodio</title>
</head>
<body>
<h1>Playlist: {{.NAME}}</h1>
<h2>Current Track: {{.CTRACK.NAME}} </h2>
<h2>Current Album : {{.CALBUM.NAME}} - {{.CALBUM.MAKER}}</h2>
<ul id="sortable">
{{range $.LIST}}
<li class="ui-state-default" id="{{.ID}}"><span>{{.MAKER}} - {{.ALBUM}} - {{.NAME}}</span></li>
{{end}}
</ul>
</body>
</html>
+81
View File
@@ -0,0 +1,81 @@
package www
import (
"log"
"html/template"
"net/http"
// "golang.org/x/net/websocket"
"github.com/gorilla/websocket"
"../playlist"
)
// todo: hub - https://stackoverflow.com/questions/31532652/go-websocket-send-all-clients-a-message
var pp *playlist.PrettyPlaylist
var pln = log.Println
type op_t struct {
OP string `json:"op"`
ID string `json:"id"`
INDEX int `json:"index"`
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 512,
WriteBufferSize: 512,
}
func Init(p *playlist.Playlist) {
pp = p.Pretty()
http.HandleFunc("/pp", pp_handler)
http.HandleFunc("/ppop", pp_operations)
}
func pp_handler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("./www/tmpl/playlist.html")
if err != nil {
pln(err)
return
}
pp.Print()
t.Execute(w, pp)
}
func pp_operations(w http.ResponseWriter, r *http.Request) {
pln("x")
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
pln(err)
return
}
go readop(c)
}
func readop(c *websocket.Conn) {
for {
opdata := &op_t{}
if err := c.ReadJSON(&opdata); err != nil {
pln(err)
return //connection lost?
}
pln(opdata.OP)
pln(opdata.ID)
pln(opdata.INDEX)
if err := c.WriteJSON(opdata); 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)
// }