mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
extended client server communication
This commit is contained in:
parent
94f63fc7b2
commit
81d2aa4ddc
12 changed files with 156 additions and 86 deletions
|
|
@ -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;
|
||||
});
|
||||
17
lib/Chuck/Game.js
Normal file
17
lib/Chuck/Game.js
Normal file
|
|
@ -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;
|
||||
});
|
||||
|
|
@ -104,5 +104,9 @@ define(requires, function(PhysicsEngine, Player, InputControlUnit, Settings, Box
|
|||
}
|
||||
}
|
||||
|
||||
Processor.prototype.destruct = function() {
|
||||
|
||||
}
|
||||
|
||||
return Processor;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
define(function() {
|
||||
|
||||
function Consumer() {
|
||||
}
|
||||
|
||||
Consumer.prototype.init = function(){
|
||||
return null;
|
||||
}
|
||||
|
||||
return Consumer;
|
||||
|
||||
});
|
||||
73
lib/Client/Networker.js
Normal file
73
lib/Client/Networker.js
Normal file
|
|
@ -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;
|
||||
|
||||
});
|
||||
25
lib/Protocol/Helper.js
Normal file
25
lib/Protocol/Helper.js
Normal file
|
|
@ -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;
|
||||
|
||||
});
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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){
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue