added a funtion to add a global (plugin) handler

add a function for registering "global" (plugin) handlers.
This commit is contained in:
Stefan Zörner 2022-01-16 09:49:10 +01:00
parent d0a90addfd
commit f07e669c0f

View File

@ -38,10 +38,11 @@ type Client struct {
func NewClient(ctx context.Context, params RegistrationParams) *Client {
return &Client{
ctx: ctx,
params: params,
actions: make(map[string]*Action),
done: make(chan struct{}),
ctx: ctx,
params: params,
actions: make(map[string]*Action),
handlers: make(map[string][]EventHandler),
done: make(chan struct{}),
}
}
func (client *Client) Action(uuid string) *Action {
@ -204,6 +205,10 @@ func (client *Client) SendToPlugin(ctx context.Context, payload interface{}) err
return client.send(NewEvent(ctx, SendToPlugin, payload))
}
func (client *Client) RegisterHandler(eventName string, handler EventHandler) {
client.handlers[eventName] = append(client.handlers[eventName], handler)
}
func (client *Client) Close() error {
err := client.c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {