ClientCountSnapshot 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 ClientCountSnapshot object to retrieve the number of clients on tryunion.com.

The client count provided by the ClientCountSnapshot class is static; it reflects the number of clients on Union Server at a single point in time only (in this example, the time this web page was loaded).

The Code

Here's the code for the preceding client count 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
46
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.ClientCountSnapshot;
  import net.user1.reactor.snapshot.RoomListSnapshot;
  import net.user1.reactor.snapshot.SnapshotEvent;
 
  public class UnionClientCountSnapshot extends Sprite {
    protected var reactor:Reactor
    protected var snapshot:ClientCountSnapshot;
    protected var output:TextField;
   
    public function UnionClientCountSnapshot () {
      // Make the output text field
      output = new TextField();
      output.border = true;
      output.background = true;
      output.width = 499;
      output.height = 99;
      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 ClientCountSnapshot();
      snapshot.addEventListener(SnapshotEvent.LOAD, snapshotLoadListener);
      reactor.updateSnapshot(snapshot);
    }
   
    public function snapshotLoadListener (e:SnapshotEvent):void {
      // Display the snapshot's content
      output.text = "Snapshot loaded.\n"
                  + "Number of clients on tryunion.com: " + snapshot.getCount();
    }
  }
}