mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
extended client server communication
This commit is contained in:
parent
94f63fc7b2
commit
81d2aa4ddc
12 changed files with 156 additions and 86 deletions
|
|
@ -3,9 +3,11 @@ define(function() {
|
|||
function Channel(name) {
|
||||
this.name = name;
|
||||
this.users = {};
|
||||
|
||||
// create game here
|
||||
}
|
||||
|
||||
Channel.prototype.validateName = function(name){
|
||||
Channel.validateName = function(name){
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ define(["Server/User", "Server/Channel"], function(User, Channel) {
|
|||
}
|
||||
|
||||
Coordinator.prototype.assignUserToChannel = function(user, channelName){
|
||||
|
||||
if(user.channel) {
|
||||
user.channel.releaseUser(user);
|
||||
}
|
||||
|
|
@ -39,6 +40,12 @@ define(["Server/User", "Server/Channel"], function(User, Channel) {
|
|||
delete this.lobbyUsers[user.id];
|
||||
}
|
||||
|
||||
Coordinator.prototype.removeUser = function(user){
|
||||
delete this.lobbyUsers[user.id];
|
||||
if(user.channel) {
|
||||
user.channel.releaseUser(user);
|
||||
}
|
||||
}
|
||||
|
||||
return Coordinator;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ define(['http', 'node-static'], function(http, nodeStatic) {
|
|||
|
||||
function HttpServer(options) {
|
||||
options.port = options.port || 1234;
|
||||
options.caching = options.caching || true;
|
||||
options.caching = typeof options.caching != 'undefined' ? options.caching : true;
|
||||
options.rootDirectory = options.rootDirectory || './';
|
||||
|
||||
this.server = null;
|
||||
|
|
@ -31,6 +31,10 @@ define(['http', 'node-static'], function(http, nodeStatic) {
|
|||
fileServer.serveFile('./node_modules/requirejs/require.js', 200, {}, req, res);
|
||||
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):
|
||||
fileServer.serve(req, res, function(){
|
||||
self.handleFileError(res)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
define(["Protocol/Parser"], function(Parser) {
|
||||
define(["Protocol/Helper"], function(ProtocolHelper) {
|
||||
|
||||
function User(socketLink, coordinator) {
|
||||
|
||||
|
|
@ -12,33 +12,36 @@ define(["Protocol/Parser"], function(Parser) {
|
|||
|
||||
User.prototype.init = function(socketLink){
|
||||
|
||||
var self = this;
|
||||
|
||||
socketLink.on('message', function(message){
|
||||
this.onMessage(message);
|
||||
self.onMessage(message);
|
||||
});
|
||||
|
||||
socketLink.on('disconnect', function(){
|
||||
this.onDisconnect();
|
||||
self.onDisconnect();
|
||||
});
|
||||
}
|
||||
|
||||
User.prototype.setChannel = function(channel){
|
||||
this.channel = channel;
|
||||
this.sendCommand('joinSuccess', channel.name);
|
||||
}
|
||||
|
||||
User.prototype.send = function(message){
|
||||
message = Parser.encode(message);
|
||||
User.prototype.sendCommand = function(command, options) {
|
||||
var message = ProtocolHelper.encodeCommand(command, options);
|
||||
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.onMessage = function(message){
|
||||
var self = this;
|
||||
ProtocolHelper.runCommands(message, function(command, options){
|
||||
self.processControlCommand(command, options);
|
||||
});
|
||||
}
|
||||
|
||||
User.prototype.onDisconnect = function(){
|
||||
return null;
|
||||
this.coordinator.removeUser(this);
|
||||
}
|
||||
|
||||
User.prototype.processControlCommand = function(command, options){
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue