RoomSnapshot Example

Reactor's "snapshots" provide a traditional request/response mechanism for retrieving data on demand from Union Server. The following example uses RoomManager's createRoom() method to create a room on Union Server, and then uses a RoomSnapshot object to retrieve information about that room.

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

To to create client applications that automatically stay synchronized with the state of one or more rooms, use the Room class instead of the RoomSnapshot class.

The Code

Here's the code for the preceding room snapshot 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
47
48
49
50
51
52
53
54
55
56
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.Room;
  import net.user1.reactor.snapshot.RoomSnapshot;
  import net.user1.reactor.snapshot.SnapshotEvent;
 
  public class UnionRoomSnapshot extends Sprite {
    protected var reactor:Reactor
    protected var room:Room;
    protected var snapshot:RoomSnapshot;
    protected var output:TextField;
   
    public function UnionRoomSnapshot () {
      // 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 {
      // Create a room
      room = reactor.getRoomManager().createRoom("snapshotExampleRoom");
     
      // Set a test attribute that will appear in the snapshot
      room.setAttribute("title", "Test Room");
     
      // Request the snapshot
      snapshot = new RoomSnapshot("snapshotExampleRoom");
      snapshot.addEventListener(SnapshotEvent.LOAD, snapshotLoadListener);
      reactor.updateSnapshot(snapshot);
    }
   
    public function snapshotLoadListener (e:SnapshotEvent):void {
      // Display a sampling of the snapshot's content
      var id:String = snapshot.getRoomID();
      var title: String = snapshot.getAttribute("title");
      output.text = "Snapshot loaded. \n"
                  + "ID: " + id + "\n"
                  + "title: " + title + "\n";
    }
  }
}