server.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env node
  2. var fs = require('fs'),
  3. http = require('http'),
  4. websocket = require('websocket');
  5. config = require('./config');
  6. game = require('./game');
  7. try {
  8. game.init();
  9. } catch (error) {
  10. console.error("Game cannot be loaded.", error);
  11. process.exit(1);
  12. }
  13. var webserver = http.createServer(function(request, response) {});
  14. webserver.on('error', function(error) {
  15. console.error("Unknown webserver error", error);
  16. });
  17. // create the server
  18. var websocketserver = new websocket.server({
  19. httpServer: webserver
  20. });
  21. var socketConnections = [];
  22. // WebSocket server
  23. websocketserver.on('request', function(request) {
  24. var connection = request.accept(null, request.origin);
  25. var key = request.httpRequest.headers["sec-websocket-key"];
  26. game.addConnection(key, connection);
  27. connection.on('message', function(message) {
  28. var key = request.httpRequest.headers["sec-websocket-key"];
  29. if (message.type === 'utf8') {
  30. try {
  31. var receivedData = JSON.parse(message.utf8Data);
  32. switch(receivedData.action) {
  33. case "join":
  34. var newPlayer = game.addPlayer(receivedData.data, key);
  35. this.send(JSON.stringify({action: "connect", playerid: newPlayer.id, update: game.getUpdate()}));
  36. break;
  37. case "move":
  38. game.movePlayer(receivedData.playerid, receivedData.data);
  39. break;
  40. case "tick":
  41. this.send(JSON.stringify({action: "tickresponse", data: game.getUpdate()}));
  42. break;
  43. case "sync":
  44. game.updatePlayer(receivedData.playerid, receivedData.data);
  45. break;
  46. case "chat":
  47. game.handleChat(receivedData);
  48. break;
  49. default:
  50. console.warn("Unhandled event:", receivedData.action, receivedData.data);
  51. game.sendToAll(JSON.stringify(receivedData));
  52. break;
  53. }
  54. } catch (error) {
  55. console.error("Text Error", error);
  56. }
  57. } else {
  58. //receiving data from socket
  59. }
  60. });
  61. connection.on('close', function(connection) {
  62. //TODO: close connection on server
  63. var key = request.httpRequest.headers["sec-websocket-key"];
  64. game.removePlayer(key);
  65. //game.takeOverPlayer(key);
  66. game.removeConnection(key);
  67. });
  68. });
  69. var buffersEqual = function(buffer1, buffer2) {
  70. return 0 == Buffer.compare(buffer1, buffer2);
  71. }
  72. webserver.listen(config.websocketport, config.websocketlisten, function() {
  73. console.log("Websocket server listening on " + config.websocketlisten + ":" + config.websocketport);
  74. });
  75. process.stdin.resume();
  76. process.stdin.setEncoding('utf8');
  77. process.stdin.on('data', function (data) {
  78. var input = data.trim().split(" ");
  79. switch(input.shift()) {
  80. case "exit":
  81. case "quit":
  82. case "stop":
  83. //TODO: shutdown server
  84. process.exit(0);
  85. break;
  86. case "mobiles":
  87. var mobileData = game.getUpdate();
  88. console.log(prettyPrintObject(mobileData));
  89. break;
  90. case "restart":
  91. game.shutdown();
  92. game.init();
  93. break;
  94. }
  95. });
  96. var prettyPrintObject = function(displayObject, tabs) {
  97. var tabChar = " ";
  98. if(tabs == null) {
  99. tabs = "";
  100. }
  101. if(Array.isArray(displayObject)) {
  102. var displayString = "[";
  103. for(var index in displayObject) {
  104. var subObj = displayObject[index];
  105. displayString += "\n" + tabs + tabChar + index + ": " + prettyPrintObject(subObj, tabs + tabChar);
  106. }
  107. return displayString + "\n" + tabs + "]";
  108. }
  109. if(typeof displayObject == 'object') {
  110. var displayString = "{";
  111. for(var index in displayObject) {
  112. var subObj = displayObject[index];
  113. displayString += "\n" + tabs + tabChar + index + ": " + prettyPrintObject(subObj, tabs + tabChar);
  114. }
  115. return displayString + "\n" + tabs + "}";
  116. }
  117. return "'" + displayObject + "'";
  118. }