Wednesday, May 12, 2010

Broadcast Comet

‹prev | My Chain | next›

Picking back up with my (fab) game, I need to figure out why it is not broadcasting movements. To be precise, some movements are being broadcast by fab.js, but not all of these broadcasts are reaching the iframe serving up the comet.

To illustrate, only one of these windows sees the movement of the other:

video

If this were working, both browsers would be able to see movement in the other (and all attached clients). My guess is that my fab.js code is only broadcasting to one client. To verify this, I leave my clients connected, then fire up curl:
cstrom@whitefall:~/repos/my_fab_game$ curl http://localhost:4011/comet_view

<script type="text/javascript">
var attrs = {"id":"Bob","x":386,"y":108};
window.parent.player_list.add_player(attrs);
var player = window.parent.player_list.get_player(attrs.id);if (typeof(player) != 'undefined') {
player.stop();
player.walk_to(attrs.x, attrs.y);
}</script>

<script type="text/javascript">
var attrs = {"id":"fred","x":358,"y":71};
window.parent.player_list.add_player(attrs);
var player = window.parent.player_list.get_player(attrs.id);if (typeof(player) != 'undefined') {
player.stop();
player.walk_to(attrs.x, attrs.y);
}</script>

<script type="text/javascript">
var attrs = {"id":"Bob","x":205,"y":63};
window.parent.player_list.add_player(attrs);
var player = window.parent.player_list.get_player(attrs.id);if (typeof(player) != 'undefined') {
player.stop();
player.walk_to(attrs.x, attrs.y);
}</script>
Weird. It is broadcasting. Even if I open up a second curl session, I still see the broadcast output. So why does it not reach the browser?

Although I am not exactly sure why, it turns out this works if two different hostnames are used (even if both refer to localhost):

video

So it worked all along! I am not going to concern myself with the vagaries of comet and single host broadcasting at this point. Instead, I take some time to reduce that broadcast payload to this:
<script type="text/javascript">
window.parent.player_list.walk_player({"id":"bob","x":101,"y":168});
</script>
I can do that by defining the walk_player method on PlayerList:
PlayerList.prototype.walk_player = function(attrs) {
this.add_player(attrs);
var player = this.get_player(attrs.id);
if (player) {
player.stop();
player.walk_to(attrs.x, attrs.y);
}
};
I clean up a few more minor things and call it a night. I still want to get to testing and raphaël, but first I think I'd like to improve state in my fab.js backend a bit. Specifically, I would like to remember everyone's position so that new players see old ones right away (currently pre-existing players would need to move first). It would also be good to remove players once the comet session ends. I will get started on that tomorrow.

Day #101

No comments:

Post a Comment