#!/usr/bin/env node var fs = require('fs'), http = require('http'), websocket = require('websocket'); config = require('./config'); game = require('./game'); try { game.init(); } catch (error) { console.error("Game cannot be loaded.", error); process.exit(1); } var webserver = http.createServer(function(request, response) {}); webserver.on('error', function(error) { console.error("Unknown webserver error", error); }); // create the server var websocketserver = new websocket.server({ httpServer: webserver }); var socketConnections = []; // WebSocket server websocketserver.on('request', function(request) { var connection = request.accept(null, request.origin); var key = request.httpRequest.headers["sec-websocket-key"]; game.addConnection(key, connection); connection.on('message', function(message) { var key = request.httpRequest.headers["sec-websocket-key"]; if (message.type === 'utf8') { try { var receivedData = JSON.parse(message.utf8Data); switch(receivedData.action) { case "join": var newPlayer = game.addPlayer(receivedData.data, key); this.send(JSON.stringify({action: "connect", playerid: newPlayer.id, update: game.getUpdate()})); break; case "move": game.movePlayer(receivedData.playerid, receivedData.data); break; case "tick": this.send(JSON.stringify({action: "tickresponse", data: game.getUpdate()})); break; case "sync": game.updatePlayer(receivedData.playerid, receivedData.data); break; case "chat": game.handleChat(receivedData); break; default: console.warn("Unhandled event:", receivedData.action, receivedData.data); game.sendToAll(JSON.stringify(receivedData)); break; } } catch (error) { console.error("Text Error", error); } } else { //receiving data from socket } }); connection.on('close', function(connection) { //TODO: close connection on server var key = request.httpRequest.headers["sec-websocket-key"]; game.removePlayer(key); //game.takeOverPlayer(key); game.removeConnection(key); }); }); var buffersEqual = function(buffer1, buffer2) { return 0 == Buffer.compare(buffer1, buffer2); } webserver.listen(config.websocketport, config.websocketlisten, function() { console.log("Websocket server listening on " + config.websocketlisten + ":" + config.websocketport); }); process.stdin.resume(); process.stdin.setEncoding('utf8'); process.stdin.on('data', function (data) { var input = data.trim().split(" "); switch(input.shift()) { case "exit": case "quit": case "stop": //TODO: shutdown server process.exit(0); break; case "mobiles": var mobileData = game.getUpdate(); console.log(prettyPrintObject(mobileData)); break; case "restart": game.shutdown(); game.init(); break; } }); var prettyPrintObject = function(displayObject, tabs) { var tabChar = " "; if(tabs == null) { tabs = ""; } if(Array.isArray(displayObject)) { var displayString = "["; for(var index in displayObject) { var subObj = displayObject[index]; displayString += "\n" + tabs + tabChar + index + ": " + prettyPrintObject(subObj, tabs + tabChar); } return displayString + "\n" + tabs + "]"; } if(typeof displayObject == 'object') { var displayString = "{"; for(var index in displayObject) { var subObj = displayObject[index]; displayString += "\n" + tabs + tabChar + index + ": " + prettyPrintObject(subObj, tabs + tabChar); } return displayString + "\n" + tabs + "}"; } return "'" + displayObject + "'"; }