1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- var net = require('net'),
- fs = require('fs'),
- http = require('http'),
- websocket = require('websocket'),
- crypto = require('crypto');
- config = require('../config');
- world = require('../world');
- core = require('./core');
- WebsocketLogin = require('./websocketlogin');
- var logins = [];
- try {
- core.load();
- } catch (error) {
- console.error("Server cannot be loaded", error, error.stack);
- process.exit(1);
- }
- var webserver = http.createServer(function(request, response) {
- });
- webserver.on('error', function(error) {
- console.error("Unknown webserver error", error, error.stack);
- });
- // create the server
- var websocketserver = new websocket.server({
- httpServer: webserver
- });
- // WebSocket server
- websocketserver.on('request', function(request) {
- var connection = request.accept(null, request.origin);
- var login = new WebsocketLogin(connection);
- logins.push(login);
- login.attach();
- connection.on('message', function(message) {
- if (message.type === 'utf8') {
- try {
- login.receive(JSON.parse(message.utf8Data));
- } catch (error) {
- console.error("Unknown webserver error", error, error.stack);
- }
- } else {
- console.log(message);
- }
- });
- connection.on('close', function(connection) {
- login.disconnect();
- logins.splice(logins.indexOf(login), 1);
- });
- });
- 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":
- console.log("The server is shutting down");
- process.exit(0);
- break;
- case "flush":
- for(var i in session.players) {
- session.players[i].logout();
- }
- session.players = [];
- break;
- }
-
- });
|