RTMFPChat(NetGroup.sendToNearest) のSpeed Test


NetGroupではpostよりsend系の方が早いよ、という話を聞いたので試してみた。sendToNearest編

内容的には前のエントリーのsendToAllNeighborsをsendToNearestに換えただけなのだが、動作確認の説明がめんどくさいのでエントリーを分けた。

RTMFPChat(sendToNearest) with Speed Test – wonderfl build flash online

同じLANに繋がっているPCで、同時に立ち上げてSpeed Testすると、往復の時間がわかるはず。
制作環境だと、450msec前後かかる。postだと同じ環境で1000msec以上かかるので、半分の速度になった。すぐ隣にPCを置いて動作確認をしても、「体感としてほぼ同時」と言える速度になった。

◆使い方。

0:動作確認するのは、かならず1種類ずつ(sendToNearest同士など)。
1:同じLANに繋がった、二台のPCでこのページを開く。
2:ピアアシスト設定のダイアログが出るので、二台とも許可をクリック。
3:両方のStart Connectボタンを押す。
4:自動的に、お互いを見つけに行く。接続完了。
5:どちらかのPCのSpeed Testボタンを押すと数字がでる。
この数字が、往復のミリ秒。

[sourcecode language=”as3″]
// forked from umhr’s RTMFPChat(sendToNeighbor) with Speed Test
// forked from umhr’s RTMFPChat(sendToAllNeighbors) with Speed Test
// forked from umhr’s RTMFPChat with Speed Test
package {
import com.bit101.components.*;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.net.GroupSpecifier;
import flash.net.NetConnection;
import flash.net.NetGroup;

[SWF(width = "465", height = "465", backgroundColor = "0", frameRate = "60")]
public class Main extends Sprite{
private const GROUP_NAME:String = "RTMFPChat/SpeedTest";
private var _netConnection:NetConnection;
private var _netGroup:NetGroup;
private var _groupSpecifier:GroupSpecifier;
private var _messageTextArea:TextArea;
private var _inputTxt:InputText;
private var _speedTxt:InputText;
private var _startScreen:StartScreen;
private var _time:Number;
private var _numericStepper:NumericStepper;
private var _peerID:String;

public function Main() {
//
Style.embedFonts = false;
Style.fontName = "_typewriter";
Style.fontSize = 12;
new PushButton(this, 359, 20, "Send Message", sendMsg);
_messageTextArea = new TextArea(this, 10, 50);
_messageTextArea.width = 448;
_messageTextArea.height = 250;
_inputTxt = new InputText(this, 10, 20);
_inputTxt.width = 340;
_inputTxt.height = 20;
new Label(this, 10, 0, "Send Message");

// Speed Test
new PushButton(this, 10, 310, "Speed Test", speedTest);
_speedTxt = new InputText(this, 114, 310);
_speedTxt.width = 100;
_speedTxt.height = 20;
new Label(this, 220, 310, "msec");

setFPSStepper();

// Start Screen
_startScreen = new StartScreen();
_startScreen.pushButton.addEventListener(MouseEvent.CLICK, doConnect);
addChild(_startScreen);

}

private function speedTest(event:MouseEvent):void
{
_time = new Date().time;
_netGroup.sendToNearest({ mode:"echoRequest", time:_time }, _netGroup.convertPeerIDToGroupAddress(_peerID));
}

private function doConnect(event:MouseEvent):void {
_startScreen.pushButton.removeEventListener(MouseEvent.CLICK, doConnect);
removeChild(_startScreen);
//
_netConnection = new NetConnection();
_netConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
_netConnection.connect("rtmfp:");
}

private function netStatusHandler(event:NetStatusEvent):void {

if (event.info.code == "NetGroup.Posting.Notify" || event.info.code == "NetGroup.SendTo.Notify") {
if(event.info.message.mode == "echoRequest") {
_netGroup.post( { mode:"echoReply", time:new Date().time } );
return;
}else if (event.info.message.mode == "echoReply") {
_speedTxt.text = String(new Date().time – _time);
return;
}
}

_messageTextArea.text = event.info.code + "\n" + _messageTextArea.text;
switch(event.info.code){
case "NetConnection.Connect.Success":
addNetGroup()
break;
case "NetGroup.Neighbor.Connect":
_peerID = event.info.peerID;
break;
case "NetGroup.Connect.Success":
break;
case "NetStream.Connect.Success":
break;
case "NetStream.Publish.Start":
break;
case "NetGroup.Posting.Notify":
case "NetGroup.SendTo.Notify":
_messageTextArea.text = event.info.message.text + "\n\n" +_messageTextArea.text;
break;
}
}

// NetGroup
private function addNetGroup():void{
_groupSpecifier = new GroupSpecifier(GROUP_NAME);
_groupSpecifier.routingEnabled = true;
_groupSpecifier.postingEnabled = true;
_groupSpecifier.ipMulticastMemberUpdatesEnabled = true;

_groupSpecifier.addIPMulticastAddress("225.225.0.1:30303");
_netGroup = new NetGroup(_netConnection, _groupSpecifier.groupspecWithAuthorizations());
_netGroup.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
}

private function sendMsg(event:MouseEvent):void{
var message:Object = new Object();
message.text = _inputTxt.text;
message.sender = _netGroup.convertPeerIDToGroupAddress(_netConnection.nearID);
_netGroup.sendToNearest(message, _netGroup.convertPeerIDToGroupAddress(_peerID));
}

private function setFPSStepper():void
{
new Label(this, 320, 440, "FPS:");
_numericStepper = new NumericStepper(this, 350, 440, onNumericStepper);
_numericStepper.minimum = 1;
_numericStepper.maximum = 120;
_numericStepper.value = 60;
}

private function onNumericStepper(event:Event):void
{
stage.frameRate = int(_numericStepper.value);
}
}
}

import com.bit101.components.*;
import flash.display.Sprite;
class StartScreen extends Sprite{
public var pushButton:PushButton;
public function StartScreen(){
graphics.beginFill(0, .7);
graphics.drawRect(0, 0, 465, 465);
pushButton = new PushButton(this, 180, 230, "Start Connect", null);
new Label(this, 95, 260, "To begin, please click the connections");
}
}
[/sourcecode]