我已经建立了一个简单的node.js应用程序作为概念验证,在这里我希望本地网络上的对等方使用gun同步数据库。
我是新手,所以我不确定自己是否正确执行此操作,但这是我的代码:
var Gun = require('gun')
const address = require('network-address')
const hashToPort = require('hash-to-port')
// get username from arg eg. node index myname
const username = process.argv[2]
// create GUN server on it's own port
var server = require('http').createServer().listen(hashToPort(username))
var gun = Gun({web: server})
// listen for input from the console
process.stdin.on('data', (data) => {
gun.get('hello').put({ word: data.toString(), user: username })
});
// Output input update
gun.get('hello').on(function(data, key) {
console.log(data.user + ' said: ' + data.word.toString())
})
这个想法是,同级可以退出并重新连接并同步到最新版本的数据库。
我在2个不同的本地网络计算机上运行该应用程序,并且运行良好。数据库正在同步。
如果我关闭一个应用程序,然后在打开的应用程序上更新数据库,然后重新启动第二个应用程序,则第二个应用程序不会与已经打开的应用程序同步。
当有新的对等连接时,是否有办法与更新的数据库同步?
我希望一切都有意义。请提出这是否是错误的解决方法。
@@ CUGreen,我很高兴本地多播同步正在运行!
如果我正确理解您的问题,那是您要同步旧数据吗?
gun.get('hello').put(data)
和.on(cb)
update相同的对象。因此,从技术上讲,您正在同步整个数据库,只是始终获得最新状态。 (除非还有其他错误?请让我知道。)>.set(data)
而不是.put(
,这会将新记录添加到hello
上的表中,然后您可以使用[]查询所有记录(将来会插入旧记录和活动记录) C0]我不知道这是否相关,但是您可能会发现gun.get('hello').map().on(cb)
是https://gun.eco/docs/Graph-Guide / put
等的不错的介绍
当然,如果需要帮助,set
聊天室将建立一个超级友好和活跃的社区!
如果有错误,请通过http://chat.gun.eco报告它>