client

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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

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)
	}
}

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection with a normal closure.

func (*Conn) ReadEvent

func (c *Conn) ReadEvent(ctx context.Context) (Event, error)

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

type ServerError struct {
	Detail protocol.Error
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL