navidrome/plugins/capabilities/websocket_callback.go

90 lines
3.5 KiB
Go

package capabilities
// WebSocketCallback provides WebSocket message handling.
// This capability allows plugins to receive callbacks for WebSocket events
// such as text messages, binary messages, errors, and connection closures.
// Plugins that use the WebSocket host service must implement this capability
// to handle incoming events.
//
//nd:capability name=websocket
type WebSocketCallback interface {
// OnTextMessage is called when a text message is received on a WebSocket connection.
//nd:export name=nd_websocket_on_text_message
OnTextMessage(OnTextMessageInput) (OnTextMessageOutput, error)
// OnBinaryMessage is called when a binary message is received on a WebSocket connection.
//nd:export name=nd_websocket_on_binary_message
OnBinaryMessage(OnBinaryMessageInput) (OnBinaryMessageOutput, error)
// OnError is called when an error occurs on a WebSocket connection.
//nd:export name=nd_websocket_on_error
OnError(OnErrorInput) (OnErrorOutput, error)
// OnClose is called when a WebSocket connection is closed.
//nd:export name=nd_websocket_on_close
OnClose(OnCloseInput) (OnCloseOutput, error)
}
// OnTextMessageInput is the input provided when a text message is received.
type OnTextMessageInput struct {
// ConnectionID is the unique identifier for the WebSocket connection that received the message.
ConnectionID string `json:"connectionId"`
// Message is the text message content received from the WebSocket.
Message string `json:"message"`
}
// OnTextMessageOutput is the output from the text message handler.
type OnTextMessageOutput struct {
// Error is the error message if the callback failed.
// Empty or null indicates success.
Error *string `json:"error,omitempty"`
}
// OnBinaryMessageInput is the input provided when a binary message is received.
type OnBinaryMessageInput struct {
// ConnectionID is the unique identifier for the WebSocket connection that received the message.
ConnectionID string `json:"connectionId"`
// Data is the binary data received from the WebSocket, encoded as base64.
Data string `json:"data"`
}
// OnBinaryMessageOutput is the output from the binary message handler.
type OnBinaryMessageOutput struct {
// Error is the error message if the callback failed.
// Empty or null indicates success.
Error *string `json:"error,omitempty"`
}
// OnErrorInput is the input provided when an error occurs on a WebSocket connection.
type OnErrorInput struct {
// ConnectionID is the unique identifier for the WebSocket connection where the error occurred.
ConnectionID string `json:"connectionId"`
// Error is the error message describing what went wrong.
Error string `json:"error"`
}
// OnErrorOutput is the output from the error handler.
type OnErrorOutput struct {
// Error is the error message if the callback failed.
// Empty or null indicates success.
Error *string `json:"error,omitempty"`
}
// OnCloseInput is the input provided when a WebSocket connection is closed.
type OnCloseInput struct {
// ConnectionID is the unique identifier for the WebSocket connection that was closed.
ConnectionID string `json:"connectionId"`
// Code is the WebSocket close status code (e.g., 1000 for normal closure,
// 1001 for going away, 1006 for abnormal closure).
Code int32 `json:"code"`
// Reason is the human-readable reason for the connection closure, if provided.
Reason string `json:"reason"`
}
// OnCloseOutput is the output from the close handler.
type OnCloseOutput struct {
// Error is the error message if the callback failed.
// Empty or null indicates success.
Error *string `json:"error,omitempty"`
}