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
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