|
4 | 4 | <title>IPFS in the Browser</title>
|
5 | 5 | <script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
|
6 | 6 | <script type="text/javascript">
|
7 |
| - // Set this if you have a libp2p-webrtc-star server |
8 |
| - // Something like |
9 |
| - // var SIGNALING_SERVER = '/libp2p-webrtc-star/ip4/127.0.0.1/tcp/9090/ws/ipfs/' |
| 7 | + // We provide a hosted signalling endpoint that you can use to discover |
| 8 | + // and dial to other nodes. It is hosted at `star-signal.cloud.ipfs.team` |
| 9 | + // If you run your own signalling, you can change this multiaddr. |
| 10 | + const SIGNALING_SERVER = '/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss/ipfs/' |
10 | 11 |
|
11 |
| - // This is our signalling server that can be used for practical demos and experimentation. |
12 |
| - // It should not be used for apps in production. |
13 |
| - var SIGNALING_SERVER = '/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss/ipfs/' |
| 12 | + const repoPath = 'ipfs-' + Math.random() |
14 | 13 |
|
15 | 14 | // Create an IPFS node
|
16 |
| - var node = new Ipfs() |
| 15 | + const node = new Ipfs({ |
| 16 | + repo: repoPath |
| 17 | + }) |
17 | 18 |
|
18 | 19 | // Init the node
|
19 | 20 | node.init(handleInit)
|
20 | 21 |
|
21 | 22 | function handleInit (err) {
|
22 |
| - // The node exited with an error |
23 |
| - if (err && err.message !== 'repo already exists') { |
| 23 | + if (!err) { // The repo was initialized for the first time, we need to configure it |
| 24 | + addWebRTCMultiaddr() |
| 25 | + } else if (err && err.message !== 'repo already exists') { // The repo already existed, let's just load it |
| 26 | + loadRepo() |
| 27 | + } else { |
24 | 28 | throw err
|
25 | 29 | }
|
26 |
| - |
27 |
| - // The repo was initialized for the first time, we need to configure it |
28 |
| - if (!err) configNode() |
29 |
| - // The repo already existed, let's just load it |
30 |
| - loadRepo() |
31 | 30 | }
|
32 | 31 |
|
33 |
| - function configNode() { |
| 32 | + function addWebRTCMultiaddr() { |
| 33 | + // Addj the WebrTCStar Multiaddr to your node |
34 | 34 | node.config.get(function (err, config) {
|
35 | 35 | if (err) {
|
36 | 36 | throw err
|
37 | 37 | }
|
38 |
| - // Add at least one libp2p-webrtc-star address. |
39 |
| - var star_addr = (SIGNALING_SERVER + config.Identity.PeerID) |
40 |
| - node.config.set('Addresses.Swarm[1]', star_addr, function (err) { |
41 |
| - if (err) { |
42 |
| - throw err |
43 |
| - } |
44 |
| - // Load the newly created repo |
45 |
| - loadRepo() |
46 |
| - }) |
| 38 | + |
| 39 | + const starAddr = (SIGNALING_SERVER + config.Identity.PeerID) |
| 40 | + |
| 41 | + node.config.set('Addresses.Swarm[1]', starAddr, loadRepo) |
47 | 42 | })
|
48 | 43 | }
|
49 | 44 |
|
50 | 45 | function loadRepo() {
|
51 |
| - node.load(() => { |
52 |
| - // Go online and connect to things |
53 |
| - node.goOnline(() => { |
54 |
| - console.log('Online status: ', node.isOnline() ? 'online' : 'offline') |
55 |
| - document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline') |
56 |
| - // TODO: Write your code here! |
57 |
| - // Use methods like node.files.add, node.files.get, and so on |
58 |
| - // Methods requiring buffers can use node.types.Buffer |
59 |
| - }) |
60 |
| - }) |
| 46 | + node.load(() => node.goOnline(() => { |
| 47 | + console.log('Online status: ', node.isOnline() ? 'online' : 'offline') |
| 48 | + |
| 49 | + document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline') |
| 50 | + |
| 51 | + // \o/ Now you have an IPFS node using WebRTC to find other nodes! |
| 52 | + // You can write more code here to use it. Use methods like |
| 53 | + // node.files.add, node.files.get. See the API docs here: |
| 54 | + // https://github.com/ipfs/interface-ipfs-core/tree/master/API |
| 55 | + })) |
61 | 56 | }
|
62 | 57 | </script>
|
63 | 58 | </head>
|
|
0 commit comments