RoomListSnapshot Example

Reactor's "snapshots" provide a traditional request/response mechanism for retrieving data on demand from Union Server. For example, the following application uses a RoomListSnapshot object to retrieve the list of rooms on tryunion.com.

The room list generated by the RoomListSnapshot class is static; it reflects the rooms on Union Server at a single point in time only (in this example, the time this web page was loaded).

To maintain a dynamically synchronized room list that is automatically updated as rooms are added to or removed from Union Server, use RoomManager's watchForRooms() method rather than the RoomListSnapshot class. For an example showing watchForRooms() usage, see the Union Lobby Example.

The Code

Here's the code for the preceding room list example. For complete snapshot documentation, see the Reactor API Reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package {
  import flash.display.Sprite;
  import flash.text.TextField;
 
  import net.user1.logger.Logger;
  import net.user1.reactor.Reactor;
  import net.user1.reactor.ReactorEvent;
  import net.user1.reactor.snapshot.RoomListSnapshot;
  import net.user1.reactor.snapshot.SnapshotEvent;
 
  public class UnionRoomListSnapshot extends Sprite {
    protected var reactor:Reactor
    protected var snapshot:RoomListSnapshot;
    protected var output:TextField;
   
    public function UnionRoomListSnapshot () {
      // Make the output text field
      output = new TextField();
      output.border = true;
      output.background = true;
      output.width = 499;
      output.height = 249;
      addChild(output);
     
      // Connect to Union
      reactor = new Reactor();
      reactor.addEventListener(ReactorEvent.READY, readyListener);
      reactor.getLog().setLevel(Logger.DEBUG);
      reactor.connect("tryunion.com", 80);
    }

    private function readyListener (e:ReactorEvent):void {
      // Request the snapshot
      snapshot = new RoomListSnapshot(null, true);
      snapshot.addEventListener(SnapshotEvent.LOAD, snapshotLoadListener);
      reactor.updateSnapshot(snapshot);
    }
   
    public function snapshotLoadListener (e:SnapshotEvent):void {
      // Display the snapshot's content
      var list:Array = snapshot.getRoomList();
      output.text = "Snapshot loaded: \n" + list.join("\n");
    }
  }
}