After taking some time to explore faye in some depth, I am back to working through my (fab) game today.
When last I was working on it, I had ripped out a lot of custom written comet code in favor of faye pub-sub with no apparent loss in functionality. Taking the game for a quick spin tonight, I notice immediately that there is a problem: new players in the game are not told about already existing players. I specifically wrote a faye channel for handling this situation, so I ought to start there for investigation.
Instead, I head back to the node.js REPL to see if some of the skills that I accumulated might be of some help. I know that I can subscribe to all faye channels in node-repl using a wildcard subscription. Since messages can be of any type, I iterate over the properties to see what I can see:
node> var wc = client.subscribe('/**', function(message) {When I enter the room as player "foo", this is what I see in the node-repl:
for (var prop in message) {
puts('['+prop+']' + message[prop]);
}
});
node> [0]aDang. That is not going to be quite as much help as I had hoped.
[1]l
[2]l
[id]foo
[x]250
[y]250
There are two messages represented in that output. The first is the string "all" (represented as an array of three characters here). This is the request on the "/players/query" channel for the list of all players. It should elicit a response listing those players, but that response is conspicuously missing. The other message is the announcement of the new player "foo" (along with current x-y coordinates) to all previously play game members.
Also missing from that output is any information regarding the channel on which the message was sent. Since I am subscribed to all channels, that would definitely help. I have to admit that I was surprised that the channel information was not an attribute on the message. Surprised and even a little disappointed... Until I remembered that this is pub-sub and that faye does it right™. Faye is no more, no less than a message transport. That is all I get when subscribing to a channel—the message.
Ah well, good to be reminded of that. I will file that away (and hopefully remember it). But I am still left with wondering what is going on with my currently-playing query. That turns out to be a relatively simple solution—once I look at the actual code. When I converted my player list from a global array to an object, I left a reference to the global
players
array in place:this.faye.subscribe("/players/query", function(q) {Easy enough, I replace the global with the attribute (
var ret = [];
for (var id in players) {
ret.push(players[id].status);
}
self.faye.publish("/players/all", ret);
});
_
) that holds the list of players on the new object:this.faye.subscribe("/players/query", function(q) {That is a fine stopping point for tonight. Up tomorrow, I need to make sure that the idle timeout / quit player functionality is still working.
var ret = [];
for (var id in self._) {
ret.push(self._[id].status);
}
self.faye.publish("/players/all", ret);
});
Day #192
No comments:
Post a Comment