尝试浏览 hyperswarm 库以从浏览器连接到主题
Trying to Browserify the hyperswarm library to make a connection to a topic from a browser
我正在尝试在多个节点服务器(工作正常)和浏览器实例(不工作)之间创建一个 hyperswarm 网络。
我认为我可以 browserify
server.js 代码,就像我想在浏览器中 运行 express.js
那样,但我得到了奇怪的错误:bundle.js:12852 Uncaught SyntaxError: Unexpected token '{'
这是我浏览过的 server.js 代码:
var Datastore = require('nedb')
, db = new Datastore({ filename: 'spsn_db/usersdb.json', autoload: true });
const hyperswarm = require('hyperswarm')
const crypto = require('crypto')
const swarm = hyperswarm()
// look for peers listed under this topic
const topic = crypto.createHash('sha256')
.update('mycoolstuff') // some topic to connect
.digest()
swarm.join(topic, {
lookup: true, // find & connect to peers
announce: true // optional- announce self as a connection target
})
swarm.on('connection', (socket, info) => {
const {
priority,
status,
retries,
peer,
client
} = info
if (client) {
let doc
socket.on("data", data => {
doc = {
"name":data.toString()
}
db.insert(doc, function (err, newDoc) {
//writes the received data
});
})
}
else {
socket.write(Buffer.from("send some data to write", 'utf8'))
}
})
这里是index.html:
<script src="bundle.js"></script>
我认为它在节点服务器和 html 实例之间的工作方式相同,因为 server.js 是浏览器化的。我错过了什么?
我正在尝试在多个节点服务器(工作正常)和浏览器实例(不工作)之间创建一个 hyperswarm 网络。
我认为我可以 browserify
server.js 代码,就像我想在浏览器中 运行 express.js
那样,但我得到了奇怪的错误:bundle.js:12852 Uncaught SyntaxError: Unexpected token '{'
这是我浏览过的 server.js 代码:
var Datastore = require('nedb')
, db = new Datastore({ filename: 'spsn_db/usersdb.json', autoload: true });
const hyperswarm = require('hyperswarm')
const crypto = require('crypto')
const swarm = hyperswarm()
// look for peers listed under this topic
const topic = crypto.createHash('sha256')
.update('mycoolstuff') // some topic to connect
.digest()
swarm.join(topic, {
lookup: true, // find & connect to peers
announce: true // optional- announce self as a connection target
})
swarm.on('connection', (socket, info) => {
const {
priority,
status,
retries,
peer,
client
} = info
if (client) {
let doc
socket.on("data", data => {
doc = {
"name":data.toString()
}
db.insert(doc, function (err, newDoc) {
//writes the received data
});
})
}
else {
socket.write(Buffer.from("send some data to write", 'utf8'))
}
})
这里是index.html:
<script src="bundle.js"></script>
我认为它在节点服务器和 html 实例之间的工作方式相同,因为 server.js 是浏览器化的。我错过了什么?