Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = func() udpClient.Config { cfg := udpClient.DefaultConfig cfg.Handler = func(w *responsewriter.ResponseWriter[*udpClient.Conn], r *pool.Message) { switch r.Code() { case codes.POST, codes.PUT, codes.GET, codes.DELETE: if err := w.SetResponse(codes.NotFound, message.TextPlain, nil); err != nil { cfg.Errors(fmt.Errorf("dtls client: cannot set response: %w", err)) } } } return cfg }()
Functions ¶
func Dial ¶
Dial creates a client connection to the given target. cfg accepts either a *dtls.Config (backward-compatible legacy path) or a DTLSClientOptions value built with NewDTLSClientOptions (recommended).
Example (WithOptions) ¶
package main
import (
"context"
"fmt"
"io"
"log"
"time"
piondtls "github.com/pion/dtls/v3"
"github.com/plgd-dev/go-coap/v3/dtls"
)
func main() {
conn, err := dtls.Dial("pluggedin.cloud:5684", dtls.NewDTLSClientOptions(
piondtls.WithPSK(func(hint []byte) ([]byte, error) {
fmt.Printf("Hint: %s \n", hint)
return []byte{0xAB, 0xC1, 0x23}, nil
}),
piondtls.WithPSKIdentityHint([]byte("Pion DTLS Server")),
piondtls.WithCipherSuites(piondtls.TLS_PSK_WITH_AES_128_CCM_8),
))
if err != nil {
log.Fatal(err)
}
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
res, err := conn.Get(ctx, "/oic/res")
if err != nil {
log.Fatal(err)
}
data, err := io.ReadAll(res.Body())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v", data)
}
Output:
Types ¶
type DTLSClientConfig ¶ added in v3.5.0
type DTLSClientConfig interface {
*piondtls.Config | DTLSClientOptions
}
DTLSClientConfig is a type constraint accepted by Dial. It allows callers to pass either the legacy *piondtls.Config (backward-compatible) or the recommended DTLSClientOptions wrapper (built via NewDTLSClientOptions).
type DTLSClientOptions ¶ added in v3.5.0
type DTLSClientOptions struct {
// contains filtered or unexported fields
}
DTLSClientOptions holds DTLS client-side configuration options for use with Dial. It wraps the options-based API of pion/dtls, keeping pion/dtls option types out of go-coap function signatures.
func NewDTLSClientOptions ¶ added in v3.5.0
func NewDTLSClientOptions(opts ...piondtls.ClientOption) DTLSClientOptions
NewDTLSClientOptions creates a DTLSClientOptions from the provided pion/dtls ClientOption values (e.g. piondtls.WithPSK, piondtls.WithCertificates, …).
Most pion/dtls options implement the shared piondtls.Option interface, which satisfies both ServerOption and ClientOption, so they can be passed here directly.