A fork of go-ws-transport with support for mounting the WebSocket upgrade handler on an existing http.ServeMux.
import "github.com/btwiuse/wsport"host, _ := libp2p.New(
libp2p.Transport(wsport.New),
)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)mux.Handle("/p2p", tpt.WebSocketHandler())Order matters: WebSocketHandler must be called before Listen.
If Listen is called first, handlerLn is nil and it falls through to
the normal net.Listener path instead.
See cmd/muxp2p for a complete example.