Skip to content

Commit

Permalink
servers and clients must now be started by the game, which will suppl…
Browse files Browse the repository at this point in the history
…y the address/port/transport
  • Loading branch information
bioid committed Jul 18, 2015
1 parent 1a7fd78 commit 9fcc86a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
11 changes: 3 additions & 8 deletions scripts/systems/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
36 changes: 23 additions & 13 deletions scripts/systems/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
];
Expand Down Expand Up @@ -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));
}

})

Expand Down
5 changes: 5 additions & 0 deletions scripts/things/shooter_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 9fcc86a

Please sign in to comment.