mirror of
				https://github.com/sigcn/pg.git
				synced 2025-10-31 18:42:42 +08:00 
			
		
		
		
	peer/peermap: add NewURL func
This commit is contained in:
		
							
								
								
									
										12
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								README.md
									
									
									
									
									
								
							| @@ -17,7 +17,7 @@ Another p2p network library in Go | |||||||
| #### 1. Run the pgmap daemon | #### 1. Run the pgmap daemon | ||||||
| ``` | ``` | ||||||
| $ pgmap -l 127.0.0.1:9987 --secret-key 5172554832d76672d1959a5ac63c5ab9 \ | $ pgmap -l 127.0.0.1:9987 --secret-key 5172554832d76672d1959a5ac63c5ab9 \ | ||||||
|     --stun stun.qq.com:3478 --stun stun.miwifi.com:3478 |     --stun stun.miwifi.com:3478 --stun stunserver.stunprotocol.org:3478 | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| #### 2. Wrap pgmap as an https server | #### 2. Wrap pgmap as an https server | ||||||
| @@ -51,15 +51,13 @@ if err != nil { | |||||||
|     panic(err) |     panic(err) | ||||||
| } | } | ||||||
|  |  | ||||||
| pmap, err := peermap.New(peermapURL, networkSecret) | peermapServer, err := peermap.NewURL(peermapURL, networkSecret) | ||||||
| if err != nil { | if err != nil { | ||||||
|     panic(err) |     panic(err) | ||||||
| } | } | ||||||
|  |  | ||||||
| packetConn, err := p2p.ListenPacket( | // peerID is a unique string (less than 256bytes) | ||||||
|     pmap, | packetConn, err := p2p.ListenPacket(peermapServer, p2p.ListenPeerID("uniqueString")) | ||||||
|     p2p.ListenPeerID("uniqueString"), // any unique string (less than 256bytes) |  | ||||||
| ) |  | ||||||
| if err != nil { | if err != nil { | ||||||
|     panic(err) |     panic(err) | ||||||
| } | } | ||||||
| @@ -83,7 +81,7 @@ for { | |||||||
| ```go | ```go | ||||||
| ... | ... | ||||||
|  |  | ||||||
| packetConn, err := p2p.ListenPacket(pmap) | packetConn, err := p2p.ListenPacket(peermapServer) | ||||||
| if err != nil { | if err != nil { | ||||||
|     panic(err) |     panic(err) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -30,6 +30,14 @@ func New(server *url.URL, store peer.SecretStore) (*Peermap, error) { | |||||||
| 	}, nil | 	}, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func NewURL(serverURL string, store peer.SecretStore) (*Peermap, error) { | ||||||
|  | 	sURL, err := url.Parse(serverURL) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, fmt.Errorf("invalid peermap url: %w", err) | ||||||
|  | 	} | ||||||
|  | 	return New(sURL, store) | ||||||
|  | } | ||||||
|  |  | ||||||
| func (s *Peermap) SecretStore() peer.SecretStore { | func (s *Peermap) SecretStore() peer.SecretStore { | ||||||
| 	return s.store | 	return s.store | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 rkonfj
					rkonfj