Added communication structure

This commit is contained in:
logsol 2012-07-08 01:37:53 +02:00
parent 980af70259
commit 94f63fc7b2
10 changed files with 292 additions and 129 deletions

View file

@ -21,11 +21,11 @@ function onConnect () {
Chuck.init(); Chuck.init();
} }
function onMessage (packet) { function onMessage (message) {
packet = JSON.parse(packet); var commands = JSON.parse(message);
if (packet && packet.m) { for(var command in commands) {
processServerCommand(packet); processControlCommand(type, command[type]);
} }
} }
@ -33,16 +33,16 @@ function onDisconnect () {
console.log('client disconnected'); console.log('client disconnected');
} }
function processServerCommand(packet){ function processControlCommand(command, options){
switch(packet.m) { switch(command) {
case 'join': case 'joined':
break; break;
case 'nick': case 'nick':
break; break;
case 'gameCommand': case 'gameCommand':
Chuck.processGameCommand(packet.d); Chuck.processGameCommand(options);
break; break;
default: default:

View file

@ -1,33 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Chuck</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script data-main="client" src="require.js"></script>
<style>
html, body {
margin: 0;
padding: 0;
display: table;
width: 100%;
height: 100%;
background: #000;
}
#container {
text-align: center;
display: table-cell;
vertical-align: middle;
height: 100%;
}
canvas {
background: #333;
}
</style>
</head>
<body>
<div id="container">
hello chuck<br>
<canvas id="canvas" width="600" height="400"></canvas>
</div>
</body>
</html>

12
lib/Client/Consumer.js Normal file
View file

@ -0,0 +1,12 @@
define(function() {
function Consumer() {
}
Consumer.prototype.init = function(){
return null;
}
return Consumer;
});

15
lib/Protocol/Parser.js Normal file
View file

@ -0,0 +1,15 @@
define(function() {
function Parser() {
}
Parser.prototype.encode = function(message){
return JSON.stringify(message);
}
Parser.prototype.decode = function(message){
return JSON.parse(message);
}
return Parser;
});

22
lib/Server/Channel.js Normal file
View file

@ -0,0 +1,22 @@
define(function() {
function Channel(name) {
this.name = name;
this.users = {};
}
Channel.prototype.validateName = function(name){
return true;
}
Channel.prototype.addUser = function(user){
this.users[user.id] = user;
}
Channel.prototype.releaseUser = function(user){
delete this.users[user.id];
}
return Channel;
});

45
lib/Server/Coordinator.js Normal file
View file

@ -0,0 +1,45 @@
define(["Server/User", "Server/Channel"], function(User, Channel) {
function Coordinator() {
this.channels = {};
this.lobbyUsers = {};
}
Coordinator.prototype.createUser = function(socketLink){
var user = new User(socketLink, this);
this.assignUserToLobby(user);
}
Coordinator.prototype.assignUserToLobby = function(user){
if(user.channel) {
user.channel.releaseUser(user);
}
this.lobbyUsers[user.id] = user;
}
Coordinator.prototype.assignUserToChannel = function(user, channelName){
if(user.channel) {
user.channel.releaseUser(user);
}
if(!Channel.validateName(channelName)){
//TODO send validation error
return false;
}
var channel = this.channels[channelName];
if(!channel) {
channel = new Channel(channelName);
this.channels[channelName] = channel;
}
channel.addUser(user);
user.setChannel(channel);
delete this.lobbyUsers[user.id];
}
return Coordinator;
});

66
lib/Server/HttpServer.js Normal file
View file

@ -0,0 +1,66 @@
define(['http', 'node-static'], function(http, nodeStatic) {
function HttpServer(options) {
options.port = options.port || 1234;
options.caching = options.caching || true;
options.rootDirectory = options.rootDirectory || './';
this.server = null;
this.init(options);
}
HttpServer.prototype.init = function(options) {
var self = this;
var fileServer = new nodeStatic.Server(options.rootDirectory, { cache: options.caching });
this.server = http.createServer(
function(req, res){
req.addListener('end', function () {
switch(true) {
case req.url == '/':
fileServer.serveFile('./static/html/index.html', 200, {}, req, res);
break;
case req.url == '/client.js':
fileServer.serveFile('./client.js', 200, {}, req, res);
break;
case req.url == '/require.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)
});
break;
case new RegExp(/^\/static/).test(req.url):
fileServer.serve(req, res, function(){
self.handleFileError(res)
});
break;
default:
self.handleFileError(res);
break;
}
});
}
);
this.server.listen(options.port);
}
HttpServer.prototype.getServer = function(){
return this.server;
}
HttpServer.prototype.handleFileError = function (res){
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>404 not ... found</h1>');
}
return HttpServer;
});

29
lib/Server/Socket.js Normal file
View file

@ -0,0 +1,29 @@
define(['socket.io'], function(io) {
function Socket(server, coordinator) {
this.coordinator = coordinator;
this.socket = io.listen(server);
this.init(server);
}
Socket.prototype.init = function(){
var self = this;
this.socket.configure('development', function(){
this.set('log level', 0);
});
this.socket.on('connection', function(user){
self.onConnection(user);
});
}
Socket.prototype.onConnection = function(socketLink){
this.coordinator.createUser(socketLink);
}
return Socket;
});

66
lib/Server/User.js Normal file
View file

@ -0,0 +1,66 @@
define(["Protocol/Parser"], function(Parser) {
function User(socketLink, coordinator) {
this.id = socketLink.id;
this.socketLink = socketLink;
this.coordinator = coordinator;
this.channel = null;
this.init(socketLink);
}
User.prototype.init = function(socketLink){
socketLink.on('message', function(message){
this.onMessage(message);
});
socketLink.on('disconnect', function(){
this.onDisconnect();
});
}
User.prototype.setChannel = function(channel){
this.channel = channel;
}
User.prototype.send = function(message){
message = Parser.encode(message);
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.onDisconnect = function(){
return null;
}
User.prototype.processControlCommand = function(command, options){
switch(command) {
case 'join':
this.coordinator.assignUserToChannel(this, options);
break;
case 'leave':
this.coordinator.assignUserToLobby(this);
break;
case 'gameCommand':
//this.channel.game.processGameCommand(options);
break;
default:
break;
}
}
return User;
});

117
server.js
View file

@ -1,99 +1,40 @@
var http = require('http'), var requirejs = require('requirejs');
io = require('socket.io'),
nodeStatic = require('node-static') var inspector = {};
requirejs = require('requirejs');
requirejs.config({ requirejs.config({
nodeRequire: require, nodeRequire: require,
baseUrl: 'lib' baseUrl: 'lib'
}); });
var requirements = [
"Server/HttpServer",
"Server/Socket",
"Server/Coordinator"
];
requirejs(requirements, function(HttpServer, Socket, Coordinator) {
var options = {
port: 1234,
rootDirectory: './',
caching: false
};
var coordinator = new Coordinator();
var httpServer = new HttpServer(options);
var socket = new Socket(httpServer.getServer(), coordinator);
inspector.coordinator = coordinator;
});
exports = module.exports = inspector;
/*
belongs to channel.js
var chuck; var chuck;
requirejs(["Chuck/Chuck"], function(Chuck) { requirejs(["Chuck/Chuck"], function(Chuck) {
Chuck.init(); Chuck.init();
chuck = Chuck;
}); });
*/
var clients = [];
// Setting up http server
var fileServer = new nodeStatic.Server('./', { cache: false });
function handleFileError(res){
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>404 not ... found</h1>');
}
var server = http.createServer(
function(req, res){
req.addListener('end', function () {
switch(true) {
case req.url == '/':
fileServer.serveFile('./static/html/index.html', 200, {}, req, res);
break;
case req.url == '/client.js':
fileServer.serveFile('./client.js', 200, {}, req, res);
break;
case req.url == '/require.js':
fileServer.serveFile('./node_modules/requirejs/require.js', 200, {}, req, res);
break;
case new RegExp(/^\/lib/).test(req.url):
fileServer.serve(req, res, function(){
handleFileError(res)
});
break;
case new RegExp(/^\/static/).test(req.url):
fileServer.serve(req, res, function(){
handleFileError(res)
});
break;
default:
handleFileError(res);
break;
}
});
}
);
server.listen(1234);
var socket = io.listen(server);
socket.configure('development', function(){
socket.set('log level', 0);
});
socket.on('connection', function(client) {
console.log('client connected');
clients.push(client);
console.log("Total clients: " + clients.length);
client.send(JSON.stringify({"startId" : clients.length}));
client.on('message', function(packet){
packet = JSON.parse(packet);
if(packet && packet.m){
switch(packet.m){
case 'jump':
jump();
updateWorld(client);
break;
case 'ping':
pong(client, packet.d);
break;
default:
break;
}
}
//updateWorld();
});
client.on('disconnect', function(){
console.log("disconnect");
});
});