index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // var fs = require('fs'),
  2. // argv = require('optimist').argv
  3. var config = require('./readConfig');
  4. var Gun = require('gun')
  5. require('gun/sea')
  6. require('gun/lib/path')
  7. require('gun/lib/not')
  8. require('gun/nts')
  9. require('gun/lib/bye')
  10. config.readConfigFile();
  11. var conf = config.parseConfigOptions();
  12. var config = { port: conf.port };
  13. //create the server
  14. if (conf.ssl) {
  15. config.server = require('https').createServer(conf.sslOptions, Gun.serve(__dirname));
  16. } else {
  17. config.server = require('http').createServer(Gun.serve(__dirname));
  18. }
  19. global.gun = Gun({
  20. web: config.server.listen(config.port),
  21. peers: [(`https://localhost:${config.port}/gun`)],
  22. radisk: true,
  23. axe: false}); //until: 5000, chunk: 10
  24. //Sync all (not work for now)
  25. //global.gun.on('out',{get: {'#': {'*': ''}}})
  26. console.log('Relay peer started on port ' + config.port + ' with /gun');
  27. let tickMsg = {
  28. parameters: "[]",
  29. time: 'tick',//hb
  30. sender: 'server'
  31. };
  32. global.gun.get('server').get('heartbeat').get('tick').put(tickMsg);
  33. //instance.get('heartbeat').put({ tick: "{}" }).heartbeat(0.0, 1);
  34. setInterval(function () {
  35. let message = {
  36. parameters: "[]",
  37. time: 'tick', //hb
  38. sender: 'server'
  39. };
  40. global.gun.get('server').get('heartbeat').get('tick').put(message,function(ack){
  41. if(ack.err){
  42. console.log('ERROR: ' + ack.err)
  43. }});
  44. }, 50);