81 lines
1.5 KiB
Go
81 lines
1.5 KiB
Go
|
|
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)
|
||
|
|
// }
|