mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
fixed #58
This commit is contained in:
parent
d83376d5c7
commit
810a74a28b
13 changed files with 445 additions and 281 deletions
|
|
@ -5,48 +5,65 @@ define([
|
|||
|
||||
function (Nc, ProtocolHelper) {
|
||||
|
||||
function Api(coordinator) {
|
||||
this.coordinator = coordinator;
|
||||
this.isError = false;
|
||||
this.output = null;
|
||||
}
|
||||
function Api(coordinator) {
|
||||
this.coordinator = coordinator;
|
||||
this.isError = false;
|
||||
this.output = null;
|
||||
}
|
||||
|
||||
Api.prototype.handleCall = function(queryParameters) {
|
||||
Api.prototype.handleCall = function(queryParameters) {
|
||||
|
||||
var command;
|
||||
try {
|
||||
var message = JSON.parse(queryParameters);
|
||||
command = message.command;
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
}
|
||||
var command,
|
||||
output = null;
|
||||
|
||||
try {
|
||||
var message = JSON.parse(queryParameters);
|
||||
command = message.command;
|
||||
} catch(e) {
|
||||
this.isError = true;
|
||||
output = "JSON syntax error";
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
var output = null;
|
||||
switch(command) {
|
||||
case "getChannels":
|
||||
output = this.coordinator.getChannels();
|
||||
break;
|
||||
default:
|
||||
this.isError = true;
|
||||
output = "Command not found";
|
||||
break;
|
||||
}
|
||||
switch(command) {
|
||||
case "getChannels":
|
||||
output = this.coordinator.getChannels();
|
||||
break;
|
||||
case "createChannel":
|
||||
// FIXME: sanitize input
|
||||
output = this.createChannel(message.options);
|
||||
break;
|
||||
default:
|
||||
this.isError = true;
|
||||
output = "Command not found";
|
||||
break;
|
||||
}
|
||||
|
||||
this.output = output;
|
||||
}
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
Api.prototype.getOutput = function() {
|
||||
var output = {};
|
||||
var key = this.isError ? "error" : "success";
|
||||
output[key] = this.output;
|
||||
return JSON.stringify(output);
|
||||
|
||||
};
|
||||
Api.prototype.createChannel = function(options) {
|
||||
var result = this.coordinator.createChannel(options);
|
||||
if(result !== false) {
|
||||
return result;
|
||||
} else {
|
||||
this.isError = true;
|
||||
return "Could not create channel, name might already exist.";
|
||||
}
|
||||
};
|
||||
|
||||
Api.prototype.getContentType = function() {
|
||||
return "application/json";
|
||||
};
|
||||
Api.prototype.getOutput = function() {
|
||||
var output = {};
|
||||
var key = this.isError ? "error" : "success";
|
||||
output[key] = this.output;
|
||||
return JSON.stringify(output);
|
||||
|
||||
};
|
||||
|
||||
Api.prototype.getContentType = function() {
|
||||
return "application/json";
|
||||
};
|
||||
|
||||
return Api;
|
||||
return Api;
|
||||
|
||||
});
|
||||
112
app/Server/Coordinator.js
Executable file → Normal file
112
app/Server/Coordinator.js
Executable file → Normal file
|
|
@ -2,106 +2,64 @@ define([
|
|||
"Server/User",
|
||||
"Game/Channel/Channel",
|
||||
"Server/PipeToChannel",
|
||||
"Lib/Utilities/NotificationCenter"
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Game/Config/Settings"
|
||||
],
|
||||
|
||||
function (User, Channel, PipeToChannel, Nc) {
|
||||
function (User, Channel, PipeToChannel, Nc, Settings) {
|
||||
|
||||
function Coordinator () {
|
||||
this.channelPipes = {};
|
||||
this.lobbyUsers = {};
|
||||
function Coordinator() {
|
||||
this.channelPipes = {};
|
||||
|
||||
Nc.on('coordinator/message', this.onMessage, this);
|
||||
|
||||
console.checkpoint('create Coordinator');
|
||||
}
|
||||
|
||||
Coordinator.prototype.createUser = function (socketLink) {
|
||||
var user = new User(socketLink, this);
|
||||
console.checkpoint('creating user');
|
||||
this.assignUserToLobby(user);
|
||||
}
|
||||
|
||||
Coordinator.prototype.assignUserToLobby = function (user) {
|
||||
if(user.channelPipe) {
|
||||
//user.channel.releaseUser(user); -> generate message
|
||||
}
|
||||
this.lobbyUsers[user.id] = user;
|
||||
console.checkpoint('assign user to lobby');
|
||||
new User(socketLink, this);
|
||||
}
|
||||
|
||||
Coordinator.prototype.assignUserToChannel = function (user, channelName) {
|
||||
|
||||
if(user.channelPipe) {
|
||||
//user.channel.releaseUser(user); -> generate message
|
||||
}
|
||||
|
||||
if(!Channel.validateName(channelName)) {
|
||||
//TODO send validation error
|
||||
return false;
|
||||
}
|
||||
|
||||
var channelPipe = this.channelPipes[channelName];
|
||||
if(!channelPipe) {
|
||||
this.createPipe(channelName);
|
||||
}
|
||||
|
||||
//channel.addUser(user);
|
||||
//user.setChannel(channel);
|
||||
Nc.trigger('user/joined', user);
|
||||
|
||||
delete this.lobbyUsers[user.id];
|
||||
var channelPipe = this.channelPipes[channelName];
|
||||
user.setChannelPipe(channelPipe);
|
||||
}
|
||||
|
||||
Coordinator.prototype.removeUser = function (user) {
|
||||
|
||||
Nc.trigger('user/left', user);
|
||||
//NotificationCenter.off('channel/' + user.channel.channelName + '/user/' + user.id);
|
||||
|
||||
delete this.lobbyUsers[user.id];
|
||||
Coordinator.prototype.onDestroyPipe = function(channelName) {
|
||||
delete this.channelPipes[channelName];
|
||||
}
|
||||
|
||||
Coordinator.prototype.createPipe = function(channelName) {
|
||||
|
||||
var channelPipe = new PipeToChannel(channelName);
|
||||
this.channelPipes[channelName] = channelPipe;
|
||||
|
||||
|
||||
Nc.on('channel/' + channelName + '/message', function (data) {
|
||||
channelPipe.send('channel', data);
|
||||
}, this);
|
||||
|
||||
// sending info to user
|
||||
Nc.on('user/joined', function (user) {
|
||||
/*
|
||||
Nc.on('channel/' + channelName + '/user/' + user.id, function (recipient, data) {
|
||||
channelPipe.send(recipient, data);
|
||||
}, this);
|
||||
*/
|
||||
|
||||
channelPipe.send('channel', { addUser: user.id });
|
||||
|
||||
}, this);
|
||||
|
||||
Nc.on('user/left', function (user) {
|
||||
channelPipe.send('channel', { releaseUser: user.id });
|
||||
}, this);
|
||||
|
||||
Nc.on('user/controlCommand', function (userId, data) {
|
||||
channelPipe.sendToUser(userId, data);
|
||||
}, this);
|
||||
|
||||
return channelPipe;
|
||||
};
|
||||
|
||||
Coordinator.prototype.getChannels = function(options) {
|
||||
var list = [];
|
||||
var list = [];
|
||||
for (var channelName in this.channelPipes) {
|
||||
list.push({
|
||||
name: channelName
|
||||
});
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
Coordinator.prototype.createChannel = function(options) {
|
||||
if(this.channelPipes[options.channelName]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var channelPipe = new PipeToChannel(options);
|
||||
this.channelPipes[options.channelName] = channelPipe;
|
||||
return {
|
||||
channelName: options.channelName,
|
||||
link: "#" + options.channelName,
|
||||
timeout: Settings.CHANNEL_DESTRUCTION_TIME
|
||||
}
|
||||
};
|
||||
|
||||
return Coordinator;
|
||||
Coordinator.prototype.onMessage = function(message) {
|
||||
if(message.destroy) {
|
||||
delete this.channelPipes[message.destroy];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return Coordinator;
|
||||
|
||||
});
|
||||
|
|
@ -7,21 +7,21 @@ function (Nc, childProcess) {
|
|||
|
||||
var fork = childProcess.fork;
|
||||
|
||||
function PipeToChannel (channelName) {
|
||||
function PipeToChannel (options) {
|
||||
|
||||
this.channelPipe = null;
|
||||
this.fork = null;
|
||||
|
||||
try {
|
||||
this.channelPipe = fork('channel.js');
|
||||
this.fork = fork('channel.js');
|
||||
} catch (err) {
|
||||
throw 'Failed to fork channel! (' + err + ')';
|
||||
}
|
||||
|
||||
console.checkpoint('creating channel process for ' + channelName);
|
||||
console.checkpoint('creating channel process for ' + options.channelName);
|
||||
|
||||
this.send('channel/' + channelName, { CREATE: channelName });
|
||||
this.send('channel/' + options.channelName, { CREATE: true, options: options });
|
||||
|
||||
this.channelPipe.on('message', this.onMessage.bind(this));
|
||||
this.fork.on('message', this.onMessage.bind(this));
|
||||
|
||||
var self = this;
|
||||
}
|
||||
|
|
@ -33,7 +33,7 @@ function (Nc, childProcess) {
|
|||
data: data
|
||||
}
|
||||
|
||||
this.channelPipe.send(message);
|
||||
this.fork.send(message);
|
||||
}
|
||||
|
||||
// If user already created
|
||||
|
|
@ -43,7 +43,7 @@ function (Nc, childProcess) {
|
|||
data: data
|
||||
}
|
||||
|
||||
this.channelPipe.send(message);
|
||||
this.fork.send(message);
|
||||
}
|
||||
|
||||
PipeToChannel.prototype.onMessage = function (message) {
|
||||
|
|
|
|||
135
app/Server/User.js
Executable file → Normal file
135
app/Server/User.js
Executable file → Normal file
|
|
@ -1,60 +1,77 @@
|
|||
define([
|
||||
"Game/Core/User",
|
||||
"Lib/Utilities/Protocol/Helper",
|
||||
"Lib/Utilities/NotificationCenter"
|
||||
],
|
||||
|
||||
function (Parent, ProtocolHelper, Nc) {
|
||||
|
||||
function User (socketLink, coordinator) {
|
||||
Parent.call(this, socketLink.id);
|
||||
|
||||
this.coordinator = coordinator;
|
||||
this.channelProcess = null;
|
||||
this.socketLink = socketLink;
|
||||
|
||||
socketLink.on('message', this.onMessage.bind(this));
|
||||
socketLink.on('disconnect', this.onDisconnect.bind(this));
|
||||
|
||||
Nc.on("user/" + this.socketLink.id + "/message", this.socketLink.send, this.socketLink);
|
||||
}
|
||||
|
||||
User.prototype = Object.create(Parent.prototype);
|
||||
|
||||
|
||||
// Socket callbacks
|
||||
|
||||
User.prototype.onMessage = function (message) {
|
||||
ProtocolHelper.applyCommand(message, this);
|
||||
}
|
||||
|
||||
User.prototype.onDisconnect = function () {
|
||||
this.coordinator.removeUser(this);
|
||||
}
|
||||
|
||||
|
||||
// User command callbacks
|
||||
// Remember: control commands are coordinator relevant commands
|
||||
|
||||
User.prototype.onJoin = function(options) {
|
||||
this.coordinator.assignUserToChannel(this, options);
|
||||
};
|
||||
|
||||
User.prototype.onLeave = function(options) {
|
||||
this.coordinator.assignUserToLobby(this);
|
||||
};
|
||||
|
||||
User.prototype.onGameCommand = function(options) {
|
||||
// repacking for transport via pipe
|
||||
var message = ProtocolHelper.encodeCommand("gameCommand", options);
|
||||
Nc.trigger("user/controlCommand", this.id, message);
|
||||
};
|
||||
|
||||
User.prototype.onPing = function(timestamp) {
|
||||
var message = ProtocolHelper.encodeCommand("pong", timestamp);
|
||||
Nc.trigger("user/" + this.socketLink.id + "/message", message);
|
||||
};
|
||||
|
||||
return User;
|
||||
|
||||
define([
|
||||
"Game/Core/User",
|
||||
"Lib/Utilities/Protocol/Helper",
|
||||
"Lib/Utilities/NotificationCenter"
|
||||
],
|
||||
|
||||
function (Parent, ProtocolHelper, Nc) {
|
||||
|
||||
function User (socketLink, coordinator) {
|
||||
Parent.call(this, socketLink.id);
|
||||
|
||||
this.coordinator = coordinator;
|
||||
this.socketLink = socketLink;
|
||||
this.channelPipe = null;
|
||||
|
||||
socketLink.on('message', this.onMessage.bind(this));
|
||||
socketLink.on('disconnect', this.onDisconnect.bind(this));
|
||||
|
||||
Nc.on("user/" + this.socketLink.id + "/message", this.socketLink.send, this.socketLink);
|
||||
}
|
||||
|
||||
User.prototype = Object.create(Parent.prototype);
|
||||
|
||||
User.prototype.setChannelPipe = function(channelPipe) {
|
||||
if(channelPipe) {
|
||||
this.channelPipe = channelPipe;
|
||||
} else {
|
||||
var message = ProtocolHelper.encodeCommand("joinError", {message:"Channel not found"});
|
||||
this.socketLink.send(message);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Socket callbacks
|
||||
|
||||
User.prototype.onMessage = function (message) {
|
||||
ProtocolHelper.applyCommand(message, this);
|
||||
}
|
||||
|
||||
User.prototype.onDisconnect = function () {
|
||||
if(!this.channelPipe) {
|
||||
console.warn("Disconnecting user without a channel.");
|
||||
return;
|
||||
}
|
||||
|
||||
this.channelPipe.send('channel', { releaseUser: this.id });
|
||||
}
|
||||
|
||||
|
||||
// User command callbacks
|
||||
// Remember: control commands are coordinator relevant commands
|
||||
|
||||
User.prototype.onJoin = function(options) {
|
||||
this.coordinator.assignUserToChannel(this, options.channelName);
|
||||
|
||||
if(!this.channelPipe) {
|
||||
console.warn("Can not join user because channel (" + options.channelName + ") does not exist.")
|
||||
return;
|
||||
}
|
||||
|
||||
this.channelPipe.send('channel', { addUser: this.id });
|
||||
};
|
||||
|
||||
User.prototype.onGameCommand = function(options) {
|
||||
// repacking for transport via pipe
|
||||
var message = ProtocolHelper.encodeCommand("gameCommand", options);
|
||||
this.channelPipe.sendToUser(this.id, message);
|
||||
};
|
||||
|
||||
User.prototype.onPing = function(timestamp) {
|
||||
var message = ProtocolHelper.encodeCommand("pong", timestamp);
|
||||
Nc.trigger("user/" + this.socketLink.id + "/message", message);
|
||||
};
|
||||
|
||||
return User;
|
||||
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue