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

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;
});