From 81d2aa4ddc932f3a0875db5bbacaf045d190b653 Mon Sep 17 00:00:00 2001 From: logsol Date: Sun, 8 Jul 2012 04:29:14 +0200 Subject: [PATCH] extended client server communication --- client.js | 49 +++----------------------- lib/Chuck/Chuck.js | 13 ------- lib/Chuck/Game.js | 17 +++++++++ lib/Chuck/Processor.js | 4 +++ lib/Client/Consumer.js | 12 ------- lib/Client/Networker.js | 73 +++++++++++++++++++++++++++++++++++++++ lib/Protocol/Helper.js | 25 ++++++++++++++ lib/Protocol/Parser.js | 7 ++-- lib/Server/Channel.js | 4 ++- lib/Server/Coordinator.js | 7 ++++ lib/Server/HttpServer.js | 6 +++- lib/Server/User.js | 25 ++++++++------ 12 files changed, 156 insertions(+), 86 deletions(-) delete mode 100644 lib/Chuck/Chuck.js create mode 100644 lib/Chuck/Game.js delete mode 100644 lib/Client/Consumer.js create mode 100644 lib/Client/Networker.js create mode 100644 lib/Protocol/Helper.js diff --git a/client.js b/client.js index 57fad09..bdadf31 100644 --- a/client.js +++ b/client.js @@ -2,50 +2,11 @@ requirejs.config({ baseUrl: 'lib' }); -var Chuck; -requirejs(["Chuck/Chuck"], function(c) { - Chuck = c; - setupSocket(); -}); +var inspector = {}; -function setupSocket(){ +requirejs(["Client/Networker"], function(Networker) { var socket = io.connect(location.href); + var networker = new Networker(socket); - socket.on('connect', onConnect); - socket.on('message', onMessage); - socket.on('disconnect',onDisconnect); -} - -function onConnect () { - console.log('Client connected'); - Chuck.init(); -} - -function onMessage (message) { - var commands = JSON.parse(message); - - for(var command in commands) { - processControlCommand(type, command[type]); - } -} - -function onDisconnect () { - console.log('client disconnected'); -} - -function processControlCommand(command, options){ - switch(command) { - case 'joined': - break; - - case 'nick': - break; - - case 'gameCommand': - Chuck.processGameCommand(options); - break; - - default: - break; - } -} \ No newline at end of file + inspector.networker = networker; +}); \ No newline at end of file diff --git a/lib/Chuck/Chuck.js b/lib/Chuck/Chuck.js deleted file mode 100644 index e55b729..0000000 --- a/lib/Chuck/Chuck.js +++ /dev/null @@ -1,13 +0,0 @@ -define(["Chuck/Processor"], function(Processor){ - var Chuck = {}; - - Chuck.init = function(){ - var processor = new Processor(); - } - - Chuck.processGameCommand = function(package){ - console.log(package); - } - - return Chuck; -}); \ No newline at end of file diff --git a/lib/Chuck/Game.js b/lib/Chuck/Game.js new file mode 100644 index 0000000..6ec779d --- /dev/null +++ b/lib/Chuck/Game.js @@ -0,0 +1,17 @@ +define(["Chuck/Processor"], function(Processor){ + + function Game(networker){ + this.networker = networker; + this.processor = new Processor(); + } + + Game.prototype.processGameCommand = function(command, options){ + console.log('(not implemented) processGameCommand:', command, options); + } + + Game.prototype.destruct = function(){ + this.processor.destruct(); + } + + return Game; +}); \ No newline at end of file diff --git a/lib/Chuck/Processor.js b/lib/Chuck/Processor.js index f0aa751..24b7009 100644 --- a/lib/Chuck/Processor.js +++ b/lib/Chuck/Processor.js @@ -104,5 +104,9 @@ define(requires, function(PhysicsEngine, Player, InputControlUnit, Settings, Box } } + Processor.prototype.destruct = function() { + + } + return Processor; }); diff --git a/lib/Client/Consumer.js b/lib/Client/Consumer.js deleted file mode 100644 index 8cdd227..0000000 --- a/lib/Client/Consumer.js +++ /dev/null @@ -1,12 +0,0 @@ -define(function() { - - function Consumer() { - } - - Consumer.prototype.init = function(){ - return null; - } - - return Consumer; - -}); \ No newline at end of file diff --git a/lib/Client/Networker.js b/lib/Client/Networker.js new file mode 100644 index 0000000..9d76125 --- /dev/null +++ b/lib/Client/Networker.js @@ -0,0 +1,73 @@ +define(["Protocol/Helper", "Chuck/Game"], function(ProtocolHelper, Game) { + + function Networker(socketLink) { + this.socketLink = socketLink; + this.game = null; + + this.init(); + } + + Networker.prototype.init = function(){ + + var self = this; + + this.socketLink.on('connect', function(){ + self.onConnect(); + }); + + this.socketLink.on('message', function(message){ + self.onMessage(message); + }); + + this.socketLink.on('disconnect', function(){ + self.onDisconnect(); + }); + } + + Networker.prototype.onConnect = function() { + this.join('dungeon'); + } + + Networker.prototype.onMessage = function(message) { + var self = this; + ProtocolHelper.runCommands(message, function(command, options){ + self.processControlCommand(command, options); + }); + } + + Networker.prototype.onDisconnect = function() { + this.game.destruct(); + this.game = null; + } + + Networker.prototype.join = function(channelName){ + this.sendCommand('join', channelName); + } + + Networker.prototype.sendCommand = function(command, options) { + var message = ProtocolHelper.encodeCommand(command, options); + this.socketLink.send(message); + } + + Networker.prototype.onJoinSuccess = function(channelName) { + this.game = new Game(this); + } + + Networker.prototype.processControlCommand = function(command, options){ + switch(command) { + case 'joinSuccess': + this.onJoinSuccess(options); + break; + + case 'gameCommand': + this.game.processGameCommand(options); + break; + + default: + break; + } + } + + return Networker; + +}); \ No newline at end of file diff --git a/lib/Protocol/Helper.js b/lib/Protocol/Helper.js new file mode 100644 index 0000000..a383a0e --- /dev/null +++ b/lib/Protocol/Helper.js @@ -0,0 +1,25 @@ +define(["Protocol/Parser"], function(Parser) { + + var Helper = {} + + Helper.encodeCommand = function(command, options){ + return Parser.encode(Helper.assemble(command, options)); + } + + Helper.assemble = function(command, options){ + var commands = {}; + commands[command] = options; + return commands; + } + + Helper.runCommands = function(message, callback){ + var commands = Parser.decode(message); + + for(var command in commands) { + callback(command, commands[command]); + } + } + + return Helper; + +}); \ No newline at end of file diff --git a/lib/Protocol/Parser.js b/lib/Protocol/Parser.js index d673194..fbc0426 100644 --- a/lib/Protocol/Parser.js +++ b/lib/Protocol/Parser.js @@ -1,13 +1,12 @@ define(function() { - function Parser() { - } + var Parser = {}; - Parser.prototype.encode = function(message){ + Parser.encode = function(message){ return JSON.stringify(message); } - Parser.prototype.decode = function(message){ + Parser.decode = function(message){ return JSON.parse(message); } diff --git a/lib/Server/Channel.js b/lib/Server/Channel.js index cd505f0..4ae561a 100644 --- a/lib/Server/Channel.js +++ b/lib/Server/Channel.js @@ -3,9 +3,11 @@ define(function() { function Channel(name) { this.name = name; this.users = {}; + + // create game here } - Channel.prototype.validateName = function(name){ + Channel.validateName = function(name){ return true; } diff --git a/lib/Server/Coordinator.js b/lib/Server/Coordinator.js index 1e9765b..cd5d1da 100644 --- a/lib/Server/Coordinator.js +++ b/lib/Server/Coordinator.js @@ -18,6 +18,7 @@ define(["Server/User", "Server/Channel"], function(User, Channel) { } Coordinator.prototype.assignUserToChannel = function(user, channelName){ + if(user.channel) { user.channel.releaseUser(user); } @@ -39,6 +40,12 @@ define(["Server/User", "Server/Channel"], function(User, Channel) { delete this.lobbyUsers[user.id]; } + Coordinator.prototype.removeUser = function(user){ + delete this.lobbyUsers[user.id]; + if(user.channel) { + user.channel.releaseUser(user); + } + } return Coordinator; diff --git a/lib/Server/HttpServer.js b/lib/Server/HttpServer.js index 47aa491..0192888 100644 --- a/lib/Server/HttpServer.js +++ b/lib/Server/HttpServer.js @@ -2,7 +2,7 @@ define(['http', 'node-static'], function(http, nodeStatic) { function HttpServer(options) { options.port = options.port || 1234; - options.caching = options.caching || true; + options.caching = typeof options.caching != 'undefined' ? options.caching : true; options.rootDirectory = options.rootDirectory || './'; this.server = null; @@ -31,6 +31,10 @@ define(['http', 'node-static'], function(http, nodeStatic) { fileServer.serveFile('./node_modules/requirejs/require.js', 200, {}, req, res); break; + case req.url == '/lib/SocketIO.js': + fileServer.serveFile('./node_modules/requirejs/require.js', 200, {}, req, res); + break; + case new RegExp(/^\/lib/).test(req.url): fileServer.serve(req, res, function(){ self.handleFileError(res) diff --git a/lib/Server/User.js b/lib/Server/User.js index 0395c8a..bb15c0a 100644 --- a/lib/Server/User.js +++ b/lib/Server/User.js @@ -1,4 +1,4 @@ -define(["Protocol/Parser"], function(Parser) { +define(["Protocol/Helper"], function(ProtocolHelper) { function User(socketLink, coordinator) { @@ -12,33 +12,36 @@ define(["Protocol/Parser"], function(Parser) { User.prototype.init = function(socketLink){ + var self = this; + socketLink.on('message', function(message){ - this.onMessage(message); + self.onMessage(message); }); socketLink.on('disconnect', function(){ - this.onDisconnect(); + self.onDisconnect(); }); } User.prototype.setChannel = function(channel){ this.channel = channel; + this.sendCommand('joinSuccess', channel.name); } - User.prototype.send = function(message){ - message = Parser.encode(message); + User.prototype.sendCommand = function(command, options) { + var message = ProtocolHelper.encodeCommand(command, options); this.socketLink.send(message); } - User.prototype.onMessage = function(){ - var commands = Parser.decode(message); - for(var command in commands) { - this.processControlCommand(command, commands[command]); - } + User.prototype.onMessage = function(message){ + var self = this; + ProtocolHelper.runCommands(message, function(command, options){ + self.processControlCommand(command, options); + }); } User.prototype.onDisconnect = function(){ - return null; + this.coordinator.removeUser(this); } User.prototype.processControlCommand = function(command, options){