diff --git a/scripts/systems/client.js b/scripts/systems/client.js index 9819e80..f06bcd8 100644 --- a/scripts/systems/client.js +++ b/scripts/systems/client.js @@ -12,19 +12,14 @@ elation.extend("engine.systems.client", function(args) { this.system_attach = function(ev) { console.log('INIT: networking client'); this.world = this.engine.systems.world; - // this.connection = new elation.engine.systems.client.connection({ - // transport: 'websocket', - // host: 'dev.brandonhinshaw.us', - // port: '9001' - // }); - elation.events.add(this.connection.socket, 'new_message', elation.bind(this, this.onNewMessage)); - elation.events.add(this.world, 'world_thing_add', elation.bind(this, this.onNewThing)); - // elation.events.add(this.world, 'world_thing_remove', elation.bind(this, this.onThingRemove)); }; this.connect = function(args) { if (this.connection) return; this.connection = new elation.engine.systems.client.connection(args); + console.log(this.connection); + elation.events.add(this.connection.socket, 'new_message', elation.bind(this, this.onNewMessage)); + elation.events.add(this.world, 'world_thing_add', elation.bind(this, this.onNewThing)); } this.onNewThing = function(ev) { diff --git a/scripts/systems/server.js b/scripts/systems/server.js index 7c37260..ff5921b 100644 --- a/scripts/systems/server.js +++ b/scripts/systems/server.js @@ -10,14 +10,18 @@ elation.extend("engine.systems.server", function(args) { this.system_attach = function(ev) { console.log('INIT: networking server'); this.world = this.engine.systems.world; - this.server = new elation.engine.systems.server.websocket; - this.adminServer = new elation.engine.systems.server.adminserver; + // this.adminServer = new elation.engine.systems.server.adminserver; + }; + + this.start = function(args) { + this.server = new elation.engine.systems.server.websocket; + this.server.start(args.port); var events = [ [this.server, 'client_disconnected', this.onClientDisconnect], [this.server, 'client_connected', this.onClientConnect], [this.world, 'world_thing_add', this.onThingAdd], - [this.adminServer, 'admin_client_connected', this.onAdminClientConnect], + // [this.adminServer, 'admin_client_connected', this.onAdminClientConnect], [this.world, 'world_thing_remove', this.onThingRemove], // [this.world, 'thing_change', this.onThingChange] ]; @@ -255,17 +259,23 @@ elation.extend("engine.systems.server.client", function(args) { // FIXME - servers should take args for port/etc elation.extend("engine.systems.server.websocket", function() { - var wsServer = require('ws').Server, - wss = new wsServer({ port: 9001 }); - console.log('websocket server running on 9001') - wss.on('connection', function(ws) { - console.log('game server websocket conn'); - var id = Date.now(); - elation.events.fire({element: this, type: 'client_connected', data: {id: id, channel: ws}}); - ws.on('close', function() { - elation.events.fire({element: this, type: 'client_disconnected', data: {id: id}}); + var wsServer = require('ws').Server; + + this.start = function(port) { + if (wss) return; + + var wss = new wsServer({ port: port }); + console.log('websocket server running on', port); + + wss.on('connection', function(ws) { + console.log('game server websocket conn'); + var id = Date.now(); + elation.events.fire({element: this, type: 'client_connected', data: {id: id, channel: ws}}); + ws.on('close', function() { + elation.events.fire({element: this, type: 'client_disconnected', data: {id: id}}); + }.bind(this)); }.bind(this)); - }.bind(this)); + } }) diff --git a/scripts/things/shooter_server.js b/scripts/things/shooter_server.js index ecb1689..0eb8537 100644 --- a/scripts/things/shooter_server.js +++ b/scripts/things/shooter_server.js @@ -13,9 +13,14 @@ var _reqs = [ elation.component.add('engine.things.shooter_server', function() { this.players = {}; + this.initNetwork = function(args) { + this.server.start({ port: 9001 }); + }; + this.postinit = function() { // network events this.server = this.engine.systems.server; + this.initNetwork(); elation.events.add(this.server, 'add_player', elation.bind(this, this.spawnRemotePlayer)); elation.events.add(this.server, 'remote_thing_change', elation.bind(this, this.remoteThingChange)); elation.events.add(this.server, 'add_thing', elation.bind(this, this.spawnNewThing));