Advertisement
Guest User

Untitled

a guest
Aug 9th, 2016
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. var n3xt = require("n3xt");
  2. var fs = require('fs');
  3. var wstream = fs.createWriteStream('test.txt');
  4.  
  5. var users = {
  6. data: {},
  7. getUserById: function (id) {
  8. return this.data[id];
  9. },
  10. remoteUserById: function (id) {
  11. delete this.data[id];
  12. },
  13. addUser: function (user) {
  14. this.data[user.id] = user;
  15. }
  16. };
  17.  
  18. var messageHandler = function (client, data) {
  19. console.log(client.user, data);
  20. client.write("[reply] " + data);
  21. wstream.write(client.id + "_" + data + "\n");
  22. };
  23.  
  24. var serverTask = function () {
  25. require("net").createServer(1337, function (client) {
  26. client.id = client.remoteAddress + client.remotePort;
  27. client.user = {username: "", password: "", auth: false};
  28. users.addUser(client);
  29. console.log("[USER-CONNECTED]", client.id);
  30.  
  31. client.on("data", function (data) {
  32. messageHandler(client, data.toString());
  33. });
  34. }).listen(1337, function () {
  35. console.log("[SERVER-TASK]", "Ready");
  36. });
  37. };
  38.  
  39. n3xt([serverTask]);
  40.  
  41. process.stdin.resume();
  42.  
  43. var exitHandler = function (options, err) {
  44. wstream.end();
  45. if (options.cleanup) console.log('clean');
  46. if (err) console.log(err.stack);
  47. if (options.exit) process.exit();
  48. };
  49.  
  50. //do something when app is closing
  51. process.on('exit', exitHandler.bind(null,{cleanup:true}));
  52.  
  53. //catches ctrl+c event
  54. process.on('SIGINT', exitHandler.bind(null, {exit:true}));
  55.  
  56. //catches uncaught exceptions
  57. process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement