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
49
client.js
49
client.js
|
|
@ -2,50 +2,11 @@ requirejs.config({
|
||||||
baseUrl: 'lib'
|
baseUrl: 'lib'
|
||||||
});
|
});
|
||||||
|
|
||||||
var Chuck;
|
var inspector = {};
|
||||||
requirejs(["Chuck/Chuck"], function(c) {
|
|
||||||
Chuck = c;
|
|
||||||
setupSocket();
|
|
||||||
});
|
|
||||||
|
|
||||||
function setupSocket(){
|
requirejs(["Client/Networker"], function(Networker) {
|
||||||
var socket = io.connect(location.href);
|
var socket = io.connect(location.href);
|
||||||
|
var networker = new Networker(socket);
|
||||||
|
|
||||||
socket.on('connect', onConnect);
|
inspector.networker = networker;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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;
|
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() {
|
define(function() {
|
||||||
|
|
||||||
function Parser() {
|
var Parser = {};
|
||||||
}
|
|
||||||
|
|
||||||
Parser.prototype.encode = function(message){
|
Parser.encode = function(message){
|
||||||
return JSON.stringify(message);
|
return JSON.stringify(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
Parser.prototype.decode = function(message){
|
Parser.decode = function(message){
|
||||||
return JSON.parse(message);
|
return JSON.parse(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,11 @@ define(function() {
|
||||||
function Channel(name) {
|
function Channel(name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.users = {};
|
this.users = {};
|
||||||
|
|
||||||
|
// create game here
|
||||||
}
|
}
|
||||||
|
|
||||||
Channel.prototype.validateName = function(name){
|
Channel.validateName = function(name){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ define(["Server/User", "Server/Channel"], function(User, Channel) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Coordinator.prototype.assignUserToChannel = function(user, channelName){
|
Coordinator.prototype.assignUserToChannel = function(user, channelName){
|
||||||
|
|
||||||
if(user.channel) {
|
if(user.channel) {
|
||||||
user.channel.releaseUser(user);
|
user.channel.releaseUser(user);
|
||||||
}
|
}
|
||||||
|
|
@ -39,6 +40,12 @@ define(["Server/User", "Server/Channel"], function(User, Channel) {
|
||||||
delete this.lobbyUsers[user.id];
|
delete this.lobbyUsers[user.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Coordinator.prototype.removeUser = function(user){
|
||||||
|
delete this.lobbyUsers[user.id];
|
||||||
|
if(user.channel) {
|
||||||
|
user.channel.releaseUser(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return Coordinator;
|
return Coordinator;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ define(['http', 'node-static'], function(http, nodeStatic) {
|
||||||
|
|
||||||
function HttpServer(options) {
|
function HttpServer(options) {
|
||||||
options.port = options.port || 1234;
|
options.port = options.port || 1234;
|
||||||
options.caching = options.caching || true;
|
options.caching = typeof options.caching != 'undefined' ? options.caching : true;
|
||||||
options.rootDirectory = options.rootDirectory || './';
|
options.rootDirectory = options.rootDirectory || './';
|
||||||
|
|
||||||
this.server = null;
|
this.server = null;
|
||||||
|
|
@ -31,6 +31,10 @@ define(['http', 'node-static'], function(http, nodeStatic) {
|
||||||
fileServer.serveFile('./node_modules/requirejs/require.js', 200, {}, req, res);
|
fileServer.serveFile('./node_modules/requirejs/require.js', 200, {}, req, res);
|
||||||
break;
|
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):
|
case new RegExp(/^\/lib/).test(req.url):
|
||||||
fileServer.serve(req, res, function(){
|
fileServer.serve(req, res, function(){
|
||||||
self.handleFileError(res)
|
self.handleFileError(res)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
define(["Protocol/Parser"], function(Parser) {
|
define(["Protocol/Helper"], function(ProtocolHelper) {
|
||||||
|
|
||||||
function User(socketLink, coordinator) {
|
function User(socketLink, coordinator) {
|
||||||
|
|
||||||
|
|
@ -12,33 +12,36 @@ define(["Protocol/Parser"], function(Parser) {
|
||||||
|
|
||||||
User.prototype.init = function(socketLink){
|
User.prototype.init = function(socketLink){
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
|
||||||
socketLink.on('message', function(message){
|
socketLink.on('message', function(message){
|
||||||
this.onMessage(message);
|
self.onMessage(message);
|
||||||
});
|
});
|
||||||
|
|
||||||
socketLink.on('disconnect', function(){
|
socketLink.on('disconnect', function(){
|
||||||
this.onDisconnect();
|
self.onDisconnect();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
User.prototype.setChannel = function(channel){
|
User.prototype.setChannel = function(channel){
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
|
this.sendCommand('joinSuccess', channel.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
User.prototype.send = function(message){
|
User.prototype.sendCommand = function(command, options) {
|
||||||
message = Parser.encode(message);
|
var message = ProtocolHelper.encodeCommand(command, options);
|
||||||
this.socketLink.send(message);
|
this.socketLink.send(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
User.prototype.onMessage = function(){
|
User.prototype.onMessage = function(message){
|
||||||
var commands = Parser.decode(message);
|
var self = this;
|
||||||
for(var command in commands) {
|
ProtocolHelper.runCommands(message, function(command, options){
|
||||||
this.processControlCommand(command, commands[command]);
|
self.processControlCommand(command, options);
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
User.prototype.onDisconnect = function(){
|
User.prototype.onDisconnect = function(){
|
||||||
return null;
|
this.coordinator.removeUser(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
User.prototype.processControlCommand = function(command, options){
|
User.prototype.processControlCommand = function(command, options){
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue