wsport

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: MIT Imports: 20 Imported by: 9

README

wsport — WebSocket transport for go-libp2p

go.dev reference License Ask DeepWiki

A fork of go-ws-transport with support for mounting the WebSocket upgrade handler on an existing http.ServeMux.

Usage

import "github.com/btwiuse/wsport"
As a libp2p transport
host, _ := libp2p.New(
    libp2p.Transport(wsport.New),
)
Share a port with HTTP

Mount the WebSocket handler on the same http.ServeMux as your API routes:

mux := http.NewServeMux()
mux.HandleFunc("/api", apiHandler)

host, _ := libp2p.New(
    libp2p.Transport(wsport.New, wsport.WithMux(mux, "/p2p")),
)

ln, _ := net.Listen("tcp", ":8080")
host.Network().Listen(ma.StringCast("/ip4/0.0.0.0/tcp/8080/x-parity-ws/%2Fp2p"))
http.Serve(ln, mux)
Or with WebSocketHandler for full control
mux.Handle("/p2p", tpt.WebSocketHandler())

See cmd/muxp2p for a complete example.

Documentation

Overview

Package wsport implements a websocket based transport for go-libp2p.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertWebsocketMultiaddrToNetAddr

func ConvertWebsocketMultiaddrToNetAddr(maddr ma.Multiaddr) (net.Addr, error)

func FromString added in v0.0.1

func FromString(s string) (ma.Multiaddr, error)

func FromURL added in v0.0.1

func FromURL(a *url.URL) (ma.Multiaddr, error)

func ListenAddrStrings added in v0.0.1

func ListenAddrStrings(s ...string) libp2p.Option

ListenAddrStrings configures libp2p to listen on the given (unparsed) ws:// or wss:// addresses.

func ParseWebsocketNetAddr

func ParseWebsocketNetAddr(a net.Addr) (ma.Multiaddr, error)

Types

type Addr

type Addr struct {
	*url.URL
}

Addr is an implementation of net.Addr for WebSocket.

func NewAddr

func NewAddr(host string) *Addr

NewAddr creates an Addr with `ws` scheme (insecure).

Deprecated. Use NewAddrWithScheme.

func NewAddrWithScheme

func NewAddrWithScheme(host string, isSecure bool) *Addr

NewAddrWithScheme creates a new Addr using the given host string. isSecure should be true for WSS connections and false for WS.

func (*Addr) Network

func (addr *Addr) Network() string

Network returns the network type for a WebSocket, "websocket".

type ConnAddr added in v0.0.7

type ConnAddr struct {
	Conn net.Conn
	Addr string
}

type Mux added in v0.1.3

type Mux interface {
	Handle(pattern string, handler http.Handler)
}

Mux is any type that accepts http.Handler registration by pattern. http.ServeMux implements this interface.

type MyConn

type MyConn struct {
	net.Conn
	Addr   string
	Secure bool
}

func (*MyConn) LocalMultiaddr

func (c *MyConn) LocalMultiaddr() ma.Multiaddr

func (*MyConn) RemoteMultiaddr

func (c *MyConn) RemoteMultiaddr() ma.Multiaddr

fix websocket/unknown-unknown

type Option

type Option func(*WebsocketTransport) error

func WithMux added in v0.1.3

func WithMux(mux Mux, path string) Option

WithMux is a convenience option that calls WebSocketHandler and registers the returned handler on the given Mux at the given path.

func WithTLSClientConfig

func WithTLSClientConfig(c *tls.Config) Option

WithTLSClientConfig sets a TLS client configuration on the WebSocket Dialer. Only relevant for non-browser usages.

Some useful use cases include setting InsecureSkipVerify to `true`, or setting user-defined trusted CA certificates.

func WithTLSConfig

func WithTLSConfig(conf *tls.Config) Option

WithTLSConfig sets a TLS configuration for the WebSocket listener.

type WebsocketTransport

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

WebsocketTransport is the actual go-libp2p transport

func (*WebsocketTransport) CanDial

func (t *WebsocketTransport) CanDial(a ma.Multiaddr) bool

func (*WebsocketTransport) Dial

func (*WebsocketTransport) Listen

func (*WebsocketTransport) Protocols

func (t *WebsocketTransport) Protocols() []int

func (*WebsocketTransport) Proxy

func (t *WebsocketTransport) Proxy() bool

func (*WebsocketTransport) Resolve

func (t *WebsocketTransport) Resolve(_ context.Context, maddr ma.Multiaddr) ([]ma.Multiaddr, error)

func (*WebsocketTransport) SkipResolve added in v0.1.1

func (t *WebsocketTransport) SkipResolve(_ context.Context, _ ma.Multiaddr) bool

func (*WebsocketTransport) WebSocketHandler added in v0.1.3

func (t *WebsocketTransport) WebSocketHandler() http.Handler

WebSocketHandler returns an http.Handler that upgrades HTTP connections to WebSocket for use with this transport. Use it to mount the WebSocket upgrade handler on your own HTTP server or mux:

mux.Handle("/p2p", tpt.WebSocketHandler())
http.Serve(ln, mux)

The upgrade pipeline starts immediately — connections work even without a subsequent Listen call. Listen is optional and only registers the listener address for libp2p peer discovery.

Directories

Path Synopsis
cmd module

Jump to

Keyboard shortcuts

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