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