Browse Source

add swarm code

Kieran Gibb 2 years ago
parent
commit
fd005cfb76
No known key found for this signature in database
1 changed files with 17 additions and 3 deletions
  1. 17
    3
      index.js

+ 17
- 3
index.js View File

@@ -2,6 +2,9 @@ const Base = require('cobox-group-base')
2 2
 const swarm = require('cobox-swarm')
3 3
 const assert = require('assert')
4 4
 const thunky = require('thunky')
5
+const debug = require('debug')('cobox-blind-replicator')
6
+const pump = require('pump')
7
+const hyperswarm = require('hyperswarm')
5 8
 
6 9
 module.exports = (storage, address, opts = {}) => (
7 10
   new BlindReplicator(storage, address, opts)
@@ -18,9 +21,20 @@ class BlindReplicator extends Base {
18 21
   }
19 22
 
20 23
   swarm (opts = {}) {
21
-    this._connection = swarm(this, Object.assign(opts, {
22
-      logger: this.config.logger
23
-    }))
24
+    var swarm = hyperswarm()
25
+
26
+    swarm.on('connection', (socket, details) => {
27
+      pump(socket, this.replicate(details.client), socket)
28
+      if (details.peer) debug(`connected ${util.inspect(details, null, 4)}`)
29
+    })
30
+
31
+    swarm.on('disconnection', (socket, details) => {
32
+      if (details.peer) debug(`disconnected from peer: ${details.peer.host}`)
33
+    })
34
+
35
+    swarm.join(this.discoveryKey, { lookup: true, announce: true })
36
+
37
+    this.connection = swarm
24 38
     this._isSwarming = true
25 39
     return true
26 40
   }