endpoint

package
v1.0.18 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client implements nethernet.Signaling using the HTTP endpoints exposed by a NetherNet server.

Example

ExampleClient demonstrates how to connect to a NetherNet server using HTTP signaling.

// Create a signaling client.
// This client is responsible for exchanging WebRTC connection details with the server over HTTP.
client := NewClient()

// Establish a NetherNet connection using the client for the signaling.
var d nethernet.Dialer
conn, err := d.DialContext(context.TODO(), "https://localhost:19132", client)
if err != nil {
	panic(fmt.Sprintf("error connecting to server: %s", err))
}
defer conn.Close()

fmt.Printf("connected, latency: %s", conn.Latency())

func NewClient

func NewClient() *Client

NewClient creates a new Client with the default ClientConfig. It is equivalent to calling ClientConfig{}.New().

func (*Client) Context

func (c *Client) Context() context.Context

Context always returns context.Background.

func (*Client) Credentials

func (c *Client) Credentials(ctx context.Context) (*nethernet.Credentials, error)

Credentials returns a nethernet.Credentials using the ClientConfig.Credentials if possible. Otherwise, it returns an empty nethernet.Credentials. It is optimal for the caller to provide ClientConfig.Credentials containing STUN/TURN servers in order to stabilize WebRTC peer negotiations.

func (*Client) DisableTrickleICE

func (c *Client) DisableTrickleICE() bool

DisableTrickleICE always returns true as it is not supported because the HTTP request-response model requires the full SDP exchange to complete within a single round trip. A peer connection should wait for all local ICE candidates to be gathered and include them as SDP attributes in the initial offer.

func (*Client) NetworkID

func (c *Client) NetworkID() string

NetworkID returns a network ID assigned for this Client. It is included to the URL path of requests sent to servers. Callers can specify this value from ClientConfig.NetworkID.

func (*Client) Notify

func (c *Client) Notify(n nethernet.Notifier) (stop func())

Notify registers n to receive incoming NetherNet signals.

func (*Client) PongData

func (c *Client) PongData([]byte)

PongData is unsupported on Client because endpoint clients only dial and do not receive server ping data.

func (*Client) Signal

func (c *Client) Signal(ctx context.Context, signal *nethernet.Signal) error

Signal sends a Signal to the remote endpoint.

Only nethernet.SignalTypeOffer is supported. The returned SDP answer is delivered to the Dialers registered to this Client.

type ClientConfig

type ClientConfig struct {
	// HTTPClient is the HTTP client used for making HTTP requests to the remote servers.
	// If nil, [http.DefaultClient] will be used instead.
	HTTPClient *http.Client

	// Credentials is an optional function that supplies ICE credentials
	// to the ICE gatherer used by the peer connection.
	// When nil, [Handler.Credentials] returns an empty [nethernet.Credentials]
	// with no STUN/TURN servers, which may reduce NAT traversal reliability.
	Credentials func(ctx context.Context) (*nethernet.Credentials, error)

	// Logger is used to log messages produced when handling requests.
	// If nil, it will be set from [slog.Default].
	Logger *slog.Logger

	// NetworkID is the identifier assigned to this Handler.
	// It is included to the URL path of requests sent to servers.
	// If empty, a random uint64 is generated and used.
	NetworkID string
}

ClientConfig represents a configuration for creating a Client.

func (ClientConfig) New

func (conf ClientConfig) New() *Client

New returns a new Client from the configuration. The resulting Client can be passed to nethernet.Dialer.DialContext.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler is an http.Handler that negotiates incoming NetherNet connections over HTTP. Callers can create a Handler from NewHandler or HandlerConfig.New, then pass it to http.ListenAndServeTLS or assign to http.Server.Handler.

Currently, Handler implements the following endpoints:

  • GET /v1/join (ping)
  • POST /v1/join/{networkID} (WebRTC negotiation)

func NewHandler

func NewHandler() *Handler

NewHandler creates a new Handler with default HandlerConfig values and returns it. It is equivalent to calling HandlerConfig{}.New().

func ServeTLS added in v1.0.16

func ServeTLS(address string, certFile, keyFile string) (*Handler, error)

ServeTLS is a utility method that set-ups an HTTP/TLS server on the specified address using the TLS certificate and key file. It is equivalent of calling HandlerConfig{}.ServeTLS().

func (*Handler) Close added in v1.0.16

func (h *Handler) Close() error

Close closes the underlying HTTP server, if one is bound. Otherwise, Close is no-op.

func (*Handler) Context

func (h *Handler) Context() context.Context

Context returns the background context of the underlying HTTP server, if one is bound to this Handler. Otherwise, Context always returns context.Background.

func (*Handler) Credentials

func (h *Handler) Credentials(ctx context.Context) (*nethernet.Credentials, error)

Credentials returns a nethernet.Credentials using the HandlerConfig.Credentials if possible. Otherwise, it returns an empty nethernet.Credentials. It is optimal for the caller to provide HandlerConfig.Credentials containing STUN/TURN servers in order to stabilize WebRTC peer negotiations.

func (*Handler) DisableTrickleICE

func (h *Handler) DisableTrickleICE() bool

DisableTrickleICE always returns true as it is not supported because the HTTP request-response model requires the full SDP exchange to complete within a single round trip. A peer connection should wait for all local ICE candidates to be gathered and include them as SDP attributes in the initial offer.

func (*Handler) NetworkID

func (h *Handler) NetworkID() string

NetworkID returns a network ID assigned for this Handler. This is never transmitted to clients and is currently only used for locally identifying this Handler.

func (*Handler) Notify

func (h *Handler) Notify(n nethernet.Notifier) (stop func())

Notify registers n to receive incoming HTTP endpoint offers.

func (*Handler) PongData

func (h *Handler) PongData([]byte)

PongData is a no-op implementation of nethernet.Signaling.PongData. It may become meaningful in the future if Mojang introduces an HTTP endpoint for serving MOTDs.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements http.Handler by delegating the given response writer and the request to the internal http.ServeMux.

func (*Handler) Signal

func (h *Handler) Signal(ctx context.Context, signal *nethernet.Signal) error

Signal delivers a signal to the pending negotiation identified by the nethernet.Signal.NetworkID and nethernet.Signal.ConnectionID.

type HandlerConfig

type HandlerConfig struct {
	// Logger is used to log messages produced when handling requests.
	// If nil, it will be set from [slog.Default].
	Logger *slog.Logger

	// NegotiationContext returns the [context.Context] that controls how long
	// the Handler waits for the listener to produce an SDP answer.
	// The resulting [context.Context] must be derived from the parent context.
	// If nil, a context with a 15-second timeout will be returned.
	NegotiationContext func(parent context.Context) (context.Context, context.CancelFunc)

	// Credentials is an optional function that supplies ICE credentials
	// to the ICE gatherer used by the peer connection.
	// When nil, [Handler.Credentials] returns an empty [nethernet.Credentials]
	// with no STUN/TURN servers, which may reduce NAT traversal reliability.
	Credentials func(ctx context.Context) (*nethernet.Credentials, error)

	// NetworkID is the identifier assigned to this Handler.
	// It is used only for identifying Handler and is never transmitted to clients.
	// If empty, a random uint64 is generated and used.
	NetworkID string
}

HandlerConfig represents a configuration for creating a Handler.

func (HandlerConfig) New

func (conf HandlerConfig) New() *Handler

New returns a new Handler from the configuration. The resulting Handler can be passed directly to http.ListenAndServeTLS or assigned to http.Server.Handler.

func (HandlerConfig) ServeTLS added in v1.0.16

func (conf HandlerConfig) ServeTLS(address string, certFile, keyFile string) (*Handler, error)

ServeTLS is a utility method that set-ups an HTTP/TLS server on the specified address using the TLS certificate and key file.

Jump to

Keyboard shortcuts

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