navidrome/plugins/host/go/nd_host_websocket.go
2025-12-31 17:06:29 -05:00

168 lines
5.3 KiB
Go

// Code generated by hostgen. DO NOT EDIT.
//
// This file contains client wrappers for the WebSocket host service.
// It is intended for use in Navidrome plugins built with TinyGo.
//
//go:build wasip1
package main
import (
"encoding/json"
"errors"
"github.com/extism/go-pdk"
)
// websocket_connect is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user websocket_connect
func websocket_connect(uint64, uint64, uint64) uint64
// websocket_sendtext is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user websocket_sendtext
func websocket_sendtext(uint64, uint64) uint64
// websocket_sendbinary is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user websocket_sendbinary
func websocket_sendbinary(uint64, uint64) uint64
// websocket_closeconnection is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user websocket_closeconnection
func websocket_closeconnection(uint64, int32, uint64) uint64
// WebSocketConnectResponse is the response type for WebSocket.Connect.
type WebSocketConnectResponse struct {
NewConnectionID string `json:"newConnectionID,omitempty"`
Error string `json:"error,omitempty"`
}
// WebSocketConnect calls the websocket_connect host function.
// Connect establishes a WebSocket connection to the specified URL.
//
// Plugins that use this function must also implement the WebSocketCallback capability
// to receive incoming messages and connection events.
//
// Parameters:
// - url: The WebSocket URL to connect to (ws:// or wss://)
// - headers: Optional HTTP headers to include in the handshake request
// - connectionID: Optional unique identifier for the connection. If empty, one will be generated
//
// Returns the connection ID that can be used to send messages or close the connection,
// or an error if the connection fails.
func WebSocketConnect(url string, headers map[string]string, connectionID string) (*WebSocketConnectResponse, error) {
urlMem := pdk.AllocateString(url)
defer urlMem.Free()
headersBytes, err := json.Marshal(headers)
if err != nil {
return nil, err
}
headersMem := pdk.AllocateBytes(headersBytes)
defer headersMem.Free()
connectionIDMem := pdk.AllocateString(connectionID)
defer connectionIDMem.Free()
// Call the host function
responsePtr := websocket_connect(urlMem.Offset(), headersMem.Offset(), connectionIDMem.Offset())
// Read the response from memory
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
// Parse the response
var response WebSocketConnectResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return nil, err
}
return &response, nil
}
// WebSocketSendText calls the websocket_sendtext host function.
// SendText sends a text message over an established WebSocket connection.
//
// Parameters:
// - connectionID: The connection identifier returned by Connect
// - message: The text message to send
//
// Returns an error if the connection is not found or if sending fails.
func WebSocketSendText(connectionID string, message string) error {
connectionIDMem := pdk.AllocateString(connectionID)
defer connectionIDMem.Free()
messageMem := pdk.AllocateString(message)
defer messageMem.Free()
// Call the host function
responsePtr := websocket_sendtext(connectionIDMem.Offset(), messageMem.Offset())
// Read the response from memory
responseMem := pdk.FindMemory(responsePtr)
errStr := string(responseMem.ReadBytes())
if errStr != "" {
return errors.New(errStr)
}
return nil
}
// WebSocketSendBinary calls the websocket_sendbinary host function.
// SendBinary sends binary data over an established WebSocket connection.
//
// Parameters:
// - connectionID: The connection identifier returned by Connect
// - data: The binary data to send
//
// Returns an error if the connection is not found or if sending fails.
func WebSocketSendBinary(connectionID string, data []byte) error {
connectionIDMem := pdk.AllocateString(connectionID)
defer connectionIDMem.Free()
dataMem := pdk.AllocateBytes(data)
defer dataMem.Free()
// Call the host function
responsePtr := websocket_sendbinary(connectionIDMem.Offset(), dataMem.Offset())
// Read the response from memory
responseMem := pdk.FindMemory(responsePtr)
errStr := string(responseMem.ReadBytes())
if errStr != "" {
return errors.New(errStr)
}
return nil
}
// WebSocketCloseConnection calls the websocket_closeconnection host function.
// CloseConnection gracefully closes a WebSocket connection.
//
// Parameters:
// - connectionID: The connection identifier returned by Connect
// - code: WebSocket close status code (e.g., 1000 for normal closure)
// - reason: Optional human-readable reason for closing
//
// Returns an error if the connection is not found or if closing fails.
func WebSocketCloseConnection(connectionID string, code int32, reason string) error {
connectionIDMem := pdk.AllocateString(connectionID)
defer connectionIDMem.Free()
reasonMem := pdk.AllocateString(reason)
defer reasonMem.Free()
// Call the host function
responsePtr := websocket_closeconnection(connectionIDMem.Offset(), code, reasonMem.Offset())
// Read the response from memory
responseMem := pdk.FindMemory(responsePtr)
errStr := string(responseMem.ReadBytes())
if errStr != "" {
return errors.New(errStr)
}
return nil
}