2019-03-12 04:15:42 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-03-14 20:21:53 +01:00
|
|
|
"encoding/json"
|
2019-03-12 04:15:42 +01:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dennwc/dom/js"
|
2019-03-13 06:09:24 +01:00
|
|
|
"github.com/zorchenhimer/MovieNight/common"
|
2019-03-12 04:15:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func recieve(v []js.Value) {
|
2019-03-13 06:09:24 +01:00
|
|
|
if len(v) == 0 {
|
2019-03-14 20:21:53 +01:00
|
|
|
fmt.Println("No data received")
|
2019-03-13 06:09:24 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 20:21:53 +01:00
|
|
|
chat, err := common.DecodeData(v[0].String())
|
2019-03-13 06:09:24 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error decoding data: %s\n", err)
|
|
|
|
js.Call("appendMessages", v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 20:21:53 +01:00
|
|
|
data, err := chat.GetData()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error parsing DataInterface: %v", err)
|
|
|
|
js.Call("appendMessages", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch chat.Type {
|
|
|
|
case common.DTEvent:
|
|
|
|
// on join or leave, update list of possible user names
|
|
|
|
fallthrough
|
|
|
|
case common.DTChat, common.DTError:
|
|
|
|
if !chat.Hidden {
|
|
|
|
js.Call("appendMessages", data.HTML())
|
|
|
|
}
|
2019-03-14 02:24:14 +01:00
|
|
|
case common.DTCommand:
|
2019-03-13 18:42:38 +01:00
|
|
|
dc := data.(common.DataCommand)
|
|
|
|
|
|
|
|
switch dc.Command {
|
2019-03-14 02:24:14 +01:00
|
|
|
case common.CmdPlaying:
|
2019-03-13 18:42:38 +01:00
|
|
|
if dc.Arguments == nil || len(dc.Arguments) == 0 {
|
|
|
|
js.Call("setPlaying", "", "")
|
|
|
|
|
|
|
|
} else if len(dc.Arguments) == 1 {
|
|
|
|
js.Call("setPlaying", dc.Arguments[0], "")
|
|
|
|
|
|
|
|
} else if len(dc.Arguments) == 2 {
|
|
|
|
js.Call("setPlaying", dc.Arguments[0], dc.Arguments[1])
|
|
|
|
}
|
2019-03-14 02:24:14 +01:00
|
|
|
case common.CmdRefreshPlayer:
|
2019-03-13 18:42:38 +01:00
|
|
|
js.Call("initPlayer", nil)
|
2019-03-14 02:24:14 +01:00
|
|
|
case common.CmdPurgeChat:
|
2019-03-13 18:42:38 +01:00
|
|
|
fmt.Println("//TODO: chat purge command received.")
|
2019-03-14 02:24:14 +01:00
|
|
|
case common.CmdHelp:
|
2019-03-14 20:21:53 +01:00
|
|
|
if !chat.Hidden {
|
|
|
|
js.Call("appendMessages", data.HTML())
|
|
|
|
}
|
2019-03-13 18:42:38 +01:00
|
|
|
// TODO: open window
|
|
|
|
//js.Call("")
|
|
|
|
}
|
2019-03-13 06:09:24 +01:00
|
|
|
}
|
2019-03-12 04:15:42 +01:00
|
|
|
}
|
|
|
|
|
2019-03-14 20:21:53 +01:00
|
|
|
func websocketSend(msg string, dataType common.ClientDataType) error {
|
|
|
|
data, err := json.Marshal(common.ClientData{
|
|
|
|
Type: dataType,
|
|
|
|
Message: msg,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not marshal data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
js.Call("websocketSend", string(data))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func send(this js.Value, v []js.Value) interface{} {
|
2019-03-12 04:15:42 +01:00
|
|
|
if len(v) != 1 {
|
2019-03-14 20:21:53 +01:00
|
|
|
showSendError(fmt.Errorf("expected 1 parameter, got %d", len(v)))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
err := websocketSend(v[0].String(), common.CdMessage)
|
|
|
|
if err != nil {
|
|
|
|
showSendError(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func showSendError(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Could not send: %v\n", err)
|
|
|
|
js.Call("appendMessages", `<div><span style="color: red;">Could not send message</span></div>`)
|
2019-03-12 04:15:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
js.Set("recieveMessage", js.CallbackOf(recieve))
|
2019-03-14 20:21:53 +01:00
|
|
|
js.Set("sendMessage", js.FuncOf(send))
|
2019-03-12 04:15:42 +01:00
|
|
|
|
|
|
|
// This is needed so the goroutine does not end
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Minute)
|
|
|
|
}
|
|
|
|
}
|