From 2183e70bd819f2521c2d89642d482571fc3771d8 Mon Sep 17 00:00:00 2001 From: Logsol Date: Thu, 3 Jan 2013 21:32:24 +0100 Subject: [PATCH] join success roundtrip added --- app/Game/Client/Networker.js | 9 +++--- app/Game/Server/Channel.js | 25 +++++++++-------- app/Game/Server/User.js | 54 +++++++++++++----------------------- app/Lobby/PipeToChannel.js | 2 +- app/Lobby/User.js | 11 ++++++-- 5 files changed, 48 insertions(+), 53 deletions(-) mode change 100755 => 100644 app/Game/Server/User.js diff --git a/app/Game/Client/Networker.js b/app/Game/Client/Networker.js index 1f6a60c..c7868e9 100755 --- a/app/Game/Client/Networker.js +++ b/app/Game/Client/Networker.js @@ -19,11 +19,12 @@ function (ProtocolHelper, GameController) { this.socketLink.on('connect', function () { self.onConnect(); }); -/* + this.socketLink.on('message', function (message) { - self.onMessage(message); + //self.onMessage(message); + console.log('Message from server: ', message); }); -*/ + this.socketLink.on('disconnect', function () { self.onDisconnect(); }); @@ -48,7 +49,7 @@ function (ProtocolHelper, GameController) { Networker.prototype.onJoinSuccess = function (options) { - this.gameController = new GameController(options.id); + this.gameController = new GameController(); this.gameController.loadLevel("default.json"); /* console.log("Joined " + options.channelName); diff --git a/app/Game/Server/Channel.js b/app/Game/Server/Channel.js index 2c5cd9c..694f8ef 100755 --- a/app/Game/Server/Channel.js +++ b/app/Game/Server/Channel.js @@ -1,9 +1,10 @@ define([ "Game/Server/GameController", - "Game/Core/NotificationCenter" + "Game/Core/NotificationCenter", + "Game/Server/User" ], - function (GameController, NotificationCenter) { + function (GameController, NotificationCenter, User) { function Channel (pipeToLobby, name) { @@ -36,12 +37,12 @@ switch(message.recipient) { case 'user': - self.forward(self.users[message.id], message.user); + self.forward(self.users[message.id], message.data); break; case 'id': // Do nothing, it is needed by the user break; case 'channel': - self.forward(self, message.channel); + self.forward(self, message.data); break; default: throw 'unknown recipient'; @@ -67,16 +68,16 @@ }; - Channel.prototype.addUser = function (user) { - //var userIds = Object.keys(this.users); - console.log('addUser', user); + Channel.prototype.addUser = function (userId) { + var user = new User(userId, this); this.users[user.id] = user; - - //user.sendCommand('joinSuccess', {channelName: this.name, id: user.id, userIds: userIds}); - //this.sendCommandToAllUsersExcept('userJoined', user.id, user); - - //NotificationCenter.trigger('user/joined', user); + NotificationCenter.trigger('user/joined', user); } +/* + Channel.prototype.send = function(recipient, message) { + + this.pipeToLobby.send(recipient, message); + }*/ /* Channel.prototype.releaseUser = function (user) { this.gameController.userIdLeft(user.id); diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js old mode 100755 new mode 100644 index 1261f34..f2e5ac9 --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -1,45 +1,31 @@ define([ - "Game/Core/User", - "Game/Core/Protocol/Helper", - "Game/Core/NotificationCenter" -], + "Game/Core/User", + "Game/Core/NotificationCenter", + "Game/Core/Protocol/Helper" +], + +function(Parent, NotificationCenter, ProtocolHelper) { + + function User(id, channel) { + Parent.call(this, id); -function (Parent, ProtocolHelper, NotificationCenter) { + this.channel = channel; + var self = this; - function User (socketLink, coordinator) { - Parent.call(this, socketLink.id); - this.id = socketLink.id; - this.socketLink = socketLink; - this.coordinator = coordinator; - this.channel = null; - - this.init(socketLink); + NotificationCenter.on('user/joined', function(user) { + self.sendCommand("joined", true); + }); } User.prototype = Object.create(Parent.prototype); - User.prototype.init = function (socketLink) { + User.prototype.sendCommand = function(command, options) { + var recipient = "user/" + this.id; + var data = ProtocolHelper.encodeCommand(command, options); - var self = this; - - } -/* - User.prototype.setChannel = function (channel) { - this.channel = channel; - } - - User.prototype.sendCommand = function (command, options) { - - var message = ProtocolHelper.encodeCommand(command, options); - this.socketLink.send(message); - } - - - - User.prototype.toString = function () { - return "[User " + this.id + "]"; + NotificationCenter.trigger("net/send", recipient, data); }; -*/ + return User; - + }); \ No newline at end of file diff --git a/app/Lobby/PipeToChannel.js b/app/Lobby/PipeToChannel.js index 82761d3..c653ef9 100755 --- a/app/Lobby/PipeToChannel.js +++ b/app/Lobby/PipeToChannel.js @@ -36,7 +36,7 @@ function (NotificationCenter, childProcess) { } PipeToChannel.prototype.onMessage = function (message) { - NotficationCenter.trigger(message.recipient + '/message', message.data); + NotificationCenter.trigger(message.recipient + '/message', message.data); } return PipeToChannel; diff --git a/app/Lobby/User.js b/app/Lobby/User.js index ab9e891..1dbb622 100755 --- a/app/Lobby/User.js +++ b/app/Lobby/User.js @@ -1,9 +1,10 @@ define([ "Game/Core/User", - "Game/Core/Protocol/Helper" + "Game/Core/Protocol/Helper", + "Game/Core/NotificationCenter" ], -function (Parent, ProtocolHelper) { +function (Parent, ProtocolHelper, NotificationCenter) { function User (socketLink, coordinator) { Parent.call(this, socketLink.id); @@ -20,6 +21,8 @@ function (Parent, ProtocolHelper) { socketLink.on('disconnect', function () { self.onDisconnect(); }); + + NotificationCenter.on("user/" + this.socketLink.id + "/message", this.onChannelMessage, this); } User.prototype = Object.create(Parent.prototype); @@ -62,6 +65,10 @@ function (Parent, ProtocolHelper) { } } + User.prototype.onChannelMessage = function(message) { + this.socketLink.send(message); + }; + return User; }); \ No newline at end of file