node_vwf.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/lcs-reflector/blob/master/LICENSE.md)
  4. Virtual World Framework Apache 2.0 license (https://github.com/NikolaySuslov/lcs-reflector/blob/master/licenses/LICENSE_VWF.md)
  5. */
  6. var http = require('http'),
  7. https = require('https'),
  8. argv = require('yargs').argv,
  9. sio = require('socket.io'),
  10. config = require('./server/readConfig')
  11. // Basic logging function.
  12. global.log = function () {
  13. var args = Array.prototype.slice.call(arguments);
  14. var level = args.splice(args.length - 1)[0];
  15. if (!isNaN(parseInt(level))) {
  16. level = parseInt(level);
  17. } else {
  18. args.push(level)
  19. level = 1;
  20. };
  21. if (level <= global.logLevel) {
  22. console.log.apply(this, args);
  23. }
  24. };
  25. function consoleNotice(string) {
  26. var brown = '\u001b[33m';
  27. var reset = '\u001b[0m';
  28. global.log(brown + string + reset);
  29. }
  30. function consoleError(string) {
  31. var red = '\u001b[31m';
  32. var reset = '\u001b[0m';
  33. global.log(red + string + reset);
  34. }
  35. //Start the VWF server
  36. function startVWF(reflector) {
  37. config.readConfigFile();
  38. global.logLevel = ((argv.l || argv.log) ? (argv.l || argv.log) : 1);
  39. global.instances = {};
  40. function serve(request, response) {
  41. response.writeHead(200, {
  42. "Content-Type": "application/json"
  43. });
  44. var inst = Object.keys(global.instances);
  45. var jsonobject = {
  46. "reflector": "v0.0.2"
  47. //"instances": inst
  48. }
  49. response.write(JSON.stringify(jsonobject), "utf8");
  50. response.end();
  51. //console.log("Serve here")
  52. }
  53. function OnRequest(request, response) {
  54. try {
  55. serve(request, response);
  56. // vwf.Serve( request, response );
  57. } catch (e) {
  58. response.writeHead(500, {
  59. "Content-Type": "text/plain"
  60. });
  61. response.write(e.toString(), "utf8");
  62. response.end();
  63. }
  64. } // close onRequest
  65. consoleNotice('LogLevel = ' + global.logLevel);
  66. //create the server
  67. var conf = config.parseConfigOptions();
  68. var srv = conf.ssl ? https.createServer(conf.sslOptions, OnRequest).listen(conf.port) : http.createServer(OnRequest).listen(conf.port);
  69. consoleNotice('Serving on port ' + conf.port);
  70. var socketManager = sio.listen(srv,
  71. {
  72. log: false
  73. //pingTimeout: 3600
  74. });
  75. socketManager.set('transports', ['websocket']);
  76. socketManager.sockets.on('connection', reflector.OnConnection);
  77. }
  78. exports.startVWF = startVWF;