Documentation
¶
Overview ¶
Package client connects to the KeyForge daemon over its local WebSocket: it performs the hello handshake and reads the events the daemon pushes.
Plugin authors normally don't use this package directly; the plugin package builds on it and handles launch info, dispatch and shutdown.
A plugin connection may only call hello, so the client exposes no other methods. The WebSocket URL carries an auth token: the client never includes it in the errors it returns.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("client: connection closed")
ErrClosed is wrapped by the error ReadEvent returns once the daemon has closed the connection normally (status 1000 or 1001). For a plugin this is the signal to exit.
var ErrInvalidFrame = errors.New("client: invalid frame")
ErrInvalidFrame is wrapped by errors about a single frame that doesn't match the protocol. When ReadEvent returns it, the connection is still usable.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a connection to the daemon that has completed the hello handshake.
func Dial ¶
func Dial(ctx context.Context, wsURL string, hello protocol.HelloSchemaJsonParams) (*Conn, protocol.HelloSchemaJsonResult, error)
Dial connects to wsURL, sends the hello request and waits for the daemon to accept it. It returns the daemon's hello result.
Any error means there is no connection, even when it wraps ErrClosed or ErrInvalidFrame. A rejected hello wraps a *ServerError. Because wsURL carries the auth token, a failed dial is reported with the URL redacted and without wrapping the underlying transport error; only a context error (context.Canceled, context.DeadlineExceeded) is kept for errors.Is.
Example ¶
Connects to the daemon and prints every action_invoked event until the daemon closes the connection. Plugins usually rely on the plugin package, which does this (and reads the launch info) for them.
package main
import (
"context"
"encoding/json"
"errors"
"log"
"github.com/JoniDG/keyforge-protocol/go/protocol"
"github.com/JoniDG/keyforge-sdk/go/client"
)
func main() {
ctx := context.Background()
conn, _, err := client.Dial(ctx, "ws://127.0.0.1:53817/ws?token=...", protocol.HelloSchemaJsonParams{
ProtocolVersion: "1",
Client: protocol.PeerInfo{Name: "dev.jonidg.example", Version: "1.0.0"},
})
if err != nil {
log.Fatal(err)
}
defer conn.Close() //nolint:errcheck // example
for {
ev, err := conn.ReadEvent(ctx)
if errors.Is(err, client.ErrClosed) {
return
}
if errors.Is(err, client.ErrInvalidFrame) {
log.Print(err)
continue
}
if err != nil {
log.Fatal(err)
}
var inv protocol.ActionInvokedSchemaJsonData
if err := json.Unmarshal(ev.Data, &inv); err != nil {
log.Print(err)
continue
}
log.Printf("%s fired for %s", inv.Action.Id, inv.Context)
}
}
Output:
func (*Conn) ReadEvent ¶
ReadEvent blocks until the daemon pushes an event. It returns ErrClosed when the daemon closes the connection normally, and an error wrapping ErrInvalidFrame for a frame that doesn't match the protocol. Any other error means the connection is gone. Cancelling ctx aborts the read and may close the connection; call Close when done either way. A frame larger than 32 KiB (the default read limit on both ends) closes the connection.
type Event ¶
type Event struct {
// Name is the event name, e.g. "action_invoked".
Name string
// Data is the raw event payload. Decode it with json.Unmarshal into the
// generated protocol type for Name (e.g. protocol.ActionInvokedSchemaJsonData),
// whose UnmarshalJSON validates it.
Data json.RawMessage
}
Event is an event frame pushed by the daemon.
type ServerError ¶
ServerError is an error response from the daemon, e.g. a hello rejected with code FORBIDDEN or UNSUPPORTED_PROTOCOL_VERSION. Use errors.As to get it.
func (*ServerError) Error ¶
func (e *ServerError) Error() string
Error implements the error interface.