add websocket support

This commit is contained in:
Richard Ramos 2021-04-22 09:07:22 -04:00
parent b60ee0fe5c
commit f978071043
No known key found for this signature in database
GPG Key ID: 80D4B01265FDFE8F
2 changed files with 19 additions and 2 deletions

View File

@ -17,6 +17,7 @@ import (
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-peerstore/pstoreds"
"github.com/multiformats/go-multiaddr"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/status-im/go-waku/waku/persistence"
@ -52,6 +53,8 @@ var rootCmd = &cobra.Command{
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
port, _ := cmd.Flags().GetInt("port")
enableWs, _ := cmd.Flags().GetBool("ws")
wsPort, _ := cmd.Flags().GetInt("ws-port")
relay, _ := cmd.Flags().GetBool("relay")
key, _ := cmd.Flags().GetString("nodekey")
store, _ := cmd.Flags().GetBool("store")
@ -90,6 +93,11 @@ var rootCmd = &cobra.Command{
node.WithHostAddress([]net.Addr{hostAddr}),
}
if enableWs {
wsMa, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d/ws", wsPort))
nodeOpts = append(nodeOpts, node.WithMultiaddress([]multiaddr.Multiaddr{wsMa}))
}
libp2pOpts := node.DefaultLibP2POptions
if useDB {
@ -174,14 +182,16 @@ func init() {
cobra.OnInitialize(initConfig)
rootCmd.Flags().Int("port", 9000, "Libp2p TCP listening port (0 for random)")
rootCmd.Flags().Bool("ws", false, "Enable websockets support")
rootCmd.Flags().Int("ws-port", 9001, "Libp2p TCP listening port for websocket connection (0 for random)")
rootCmd.Flags().String("nodekey", "", "P2P node private key as hex (default random)")
rootCmd.Flags().StringSlice("topics", []string{string(node.DefaultWakuTopic)}, fmt.Sprintf("List of topics to listen (default %s)", node.DefaultWakuTopic))
rootCmd.Flags().StringSlice("staticnodes", []string{}, "Multiaddr of peer to directly connect with. Argument may be repeated")
rootCmd.Flags().Bool("relay", true, "Enable relay protocol")
rootCmd.Flags().Bool("store", false, "Enable store protocol")
rootCmd.Flags().Bool("use-db", true, "Store messages and peers in a DB, (default: true, use false for in-memory only)")
rootCmd.Flags().String("dbpath", "./store.db", "Path to DB file")
rootCmd.Flags().String("storenode", "", "Multiaddr of peer to connect with for waku store protocol")
rootCmd.Flags().Bool("relay", true, "Enable relay protocol")
}

View File

@ -39,12 +39,19 @@ func WithHostAddress(hostAddr []net.Addr) WakuNodeOption {
multiAddresses = append(multiAddresses, hostAddrMA)
}
params.multiAddr = multiAddresses
params.multiAddr = append(params.multiAddr, multiAddresses...)
return nil
}
}
func WithMultiaddress(addresses []ma.Multiaddr) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.multiAddr = append(params.multiAddr, addresses...)
return nil
}
}
func WithPrivateKey(privKey *ecdsa.PrivateKey) WakuNodeOption {
return func(params *WakuNodeParameters) error {
privk := crypto.PrivKey((*crypto.Secp256k1PrivateKey)(privKey))