UnionStats
Unionの統計情報を表示するクラスです。
/**
* Copyright o8que ( http://wonderfl.net/user/o8que )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/kLgJ
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import net.user1.reactor.IClient;
import net.user1.reactor.Reactor;
import net.user1.reactor.ReactorEvent;
import net.user1.reactor.Room;
import net.wonderfl.utils.WonderflAPI;
[SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "0x000000")]
public class Main extends Sprite {
private var _reactor:Reactor;
private var _room:Room;
public function Main() {
_reactor = new Reactor();
_reactor.addEventListener(ReactorEvent.READY, initialize);
_reactor.connect("tryunion.com", 80);
addChild(new UnionStats(_reactor));
}
private function initialize(event:ReactorEvent):void {
_room = _reactor.getRoomManager().createRoom(String(new WonderflAPI(root.loaderInfo.parameters).appID));
_room.join();
addEventListener(Event.ENTER_FRAME, update);
}
private function update(event:Event):void {
_reactor.self().setAttribute("x", mouseX.toFixed(1));
_reactor.self().setAttribute("y", mouseY.toFixed(1));
graphics.clear();
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
for each(var client:IClient in _room.getOccupants()) {
graphics.beginFill(0xFFFFFF);
graphics.drawCircle(Number(client.getAttribute("x")), Number(client.getAttribute("y")), 10);
graphics.endFill();
}
}
}
}
/* ------------------------------------------------------------------------------------------------
* UnionStats
* ------------------------------------------------------------------------------------------------
*/
//package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.Timer;
import net.user1.reactor.Reactor;
import net.user1.reactor.Statistics;
//public
class UnionStats extends Sprite {
private var _reactor:Reactor;
private var _fpsCount:int;
private var _timer:Timer;
private var _fps:TextField, _stageFps:TextField;
private var _mem:TextField, _peakMem:TextField;
private var _ping:TextField;
private var _recv:TextField, _peakRecv:TextField;
private var _send:TextField, _peakSend:TextField;
private var _msg:TextField, _peakMsg:TextField;
public function UnionStats(reactor:Reactor) {
_reactor = reactor;
_reactor.enableStatistics();
_fpsCount = 0;
addEventListener(Event.ENTER_FRAME, function(event:Event):void { _fpsCount++; } );
_timer = new Timer(1000, 0);
_timer.addEventListener(TimerEvent.TIMER, update);
_timer.start();
graphics.beginFill(0x404040);
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
graphics.lineStyle(1, 0xFFFFFF, 1, true, "normal", "none", "bevel");
graphics.moveTo(5, 34);
graphics.lineTo(95, 34);
addChild(createTextField("FPS ", 0, 0, 30, TextFieldAutoSize.LEFT));
addChild(createTextField("Mem ", 0, 16, 30, TextFieldAutoSize.LEFT));
addChild(createTextField("Ping ", 0, 36, 30, TextFieldAutoSize.LEFT));
addChild(createTextField("Down ", 0, 52, 30, TextFieldAutoSize.LEFT));
addChild(createTextField("Up ", 0, 68, 30, TextFieldAutoSize.LEFT));
addChild(createTextField("Msg ", 0, 84, 30, TextFieldAutoSize.LEFT));
addChild(_fps = createTextField("0", 30, 0, 30, TextFieldAutoSize.RIGHT));
addChild(_mem = createTextField("0", 30, 16, 30, TextFieldAutoSize.RIGHT));
addChild(_recv = createTextField("0", 30, 52, 30, TextFieldAutoSize.RIGHT));
addChild(_send = createTextField("0", 30, 68, 30, TextFieldAutoSize.RIGHT));
addChild(_msg = createTextField("0", 30, 84, 30, TextFieldAutoSize.RIGHT));
addChild(createTextField("/", 60, 0, 7, TextFieldAutoSize.CENTER));
addChild(createTextField("/", 60, 16, 7, TextFieldAutoSize.CENTER));
addChild(_ping = createTextField("0", 30, 36, 70, TextFieldAutoSize.CENTER));
addChild(createTextField("/", 60, 52, 7, TextFieldAutoSize.CENTER));
addChild(createTextField("/", 60, 68, 7, TextFieldAutoSize.CENTER));
addChild(createTextField("/", 60, 84, 7, TextFieldAutoSize.CENTER));
addChild(_stageFps = createTextField("0", 67, 0, 30, TextFieldAutoSize.LEFT));
addChild(_peakMem = createTextField("0", 67, 16, 30, TextFieldAutoSize.LEFT));
addChild(_peakRecv = createTextField("0", 67, 52, 30, TextFieldAutoSize.LEFT));
addChild(_peakSend = createTextField("0", 67, 68, 30, TextFieldAutoSize.LEFT));
addChild(_peakMsg = createTextField("0", 67, 84, 30, TextFieldAutoSize.LEFT));
}
private function update(event:TimerEvent):void {
_fps.text = _fpsCount.toString();
_fpsCount = 0;
if (stage) { _stageFps.text = stage.frameRate.toString(); }
if (!_reactor.isReady()) { return; }
_ping.text = _reactor.self().getPing().toString();
var stats:Statistics = _reactor.getStatistics();
_mem.text = stats.getTotalMemoryMB().toFixed(2);
_recv.text = stats.getKBReceivedPerSecond().toFixed(2);
_send.text = stats.getKBSentPerSecond().toFixed(2);
_msg.text = stats.getMessagesPerSecond().toString();
_peakMem.text = stats.getPeakMemoryMB().toFixed(2);
_peakRecv.text = stats.getPeakKBReceivedPerSecond().toFixed(2);
_peakSend.text = stats.getPeakKBSentPerSecond().toFixed(2);
_peakMsg.text = stats.getPeakMessagesPerSecond().toString();
}
private function createTextField(text:String, x:int, y:int, width:int, autoSize:String):TextField {
var result:TextField = new TextField();
result.x = x;
result.width = width;
result.autoSize = autoSize;
result.defaultTextFormat = new TextFormat("_sans", 9, 0xFFFFFF, true);
result.text = text;
result.y = y;
result.mouseEnabled = result.selectable = false;
return result;
}
}
//}