mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
Added communication structure
This commit is contained in:
parent
980af70259
commit
94f63fc7b2
10 changed files with 292 additions and 129 deletions
22
lib/Server/Channel.js
Normal file
22
lib/Server/Channel.js
Normal 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
45
lib/Server/Coordinator.js
Normal 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
66
lib/Server/HttpServer.js
Normal 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
29
lib/Server/Socket.js
Normal 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
66
lib/Server/User.js
Normal 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;
|
||||
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue