package main
import (
"encoding/base64"
"errors"
"github.com/navidrome/navidrome/plugins/pdk/go/host"
"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
"github.com/navidrome/navidrome/plugins/pdk/go/websocket"
)
func init() {
websocket.Register(&testWebSocket{})
}
type testWebSocket struct{}
func (t *testWebSocket) OnTextMessage(input websocket.OnTextMessageRequest) error {
storeReceivedMessage("text:" + input.Message)
switch input.Message {
case "echo":
if err := host.WebSocketSendText(input.ConnectionID, "echo:"+input.Message); err != nil {
return err
}
case "close":
if err := host.WebSocketCloseConnection(input.ConnectionID, 1000, "closed by plugin"); err != nil {
return err
}
case "fail":
return errors.New("intentional test failure")
}
return nil
}
func (t *testWebSocket) OnBinaryMessage(input websocket.OnBinaryMessageRequest) error {
encoded := base64.StdEncoding.EncodeToString(input.Data)
storeReceivedMessage("binary:" + encoded)
return host.WebSocketSendBinary(input.ConnectionID, input.Data)
}
func (t *testWebSocket) OnError(input websocket.OnErrorRequest) error {
storeReceivedMessage("error:" + input.Error)
return nil
}
func (t *testWebSocket) OnClose(input websocket.OnCloseRequest) error {
storeReceivedMessage("close:" + input.Reason)
return nil
}
func storeReceivedMessage(msg string) {
if existingVar := pdk.GetVar("_received_messages"); existingVar != nil {
msg = string(existingVar) + "\n" + msg
}
pdk.SetVar("_received_messages", []byte(msg))
}
func main() {}