Monthly Archives: September 2009

FMS blocked port detection – ActionScript 3

Written by . Filed under ActionScript 3. Tagged , , , , , , , , , , , . .

FMS block port detection – ActionScript 3

The fms port (1935) closed on many office firewalls, the problem
is that flash had 30 sec timeout before tunnle it to port 80.
Becouse that i write little script that use socket object to test the
port and tunnle it to http when port 1935 is closed.

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.events.Event;
import flash.net.Socket;
import flash.events.IOErrorEvent
import flash.events.SecurityErrorEvent

/**
* @author Garry Lachman
*
* Usage:
* FMSPortTester.TestPorts(onSuccess)
* function onSuccess(_fmsURL:String):void   {
* }
*/

public class FMSPortTester
{
public static const FMS_URL:String = "127.0.0.1";
public static const PREFIX_1935:String = "rtmp://";
public static const PREFIX_80:String = "rtmpt://";

public static const SOCKET_TIMEOUT:Number = 3;

public function FMSPortTester() {   trace("static only class"); }

public static function TestPorts(_resultURLCallBack:Function):void  {
// Create socket connection to fms to check the ports
var socketTest:Socket = new Socket();
socketTest.connect(FMS_URL, 1935);
socketTest.addEventListener(Event.CONNECT, onSocketConnected);
socketTest.addEventListener(IOErrorEvent.IO_ERROR, onSocketError);
socketTest.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSocketError);
socketTest.timeout = (SOCKET_TIMEOUT * 1000);

// is connected, fms ports ok
function onSocketConnected(_e:Event):void   {
_resultURLCallBack(PREFIX_1935 + "" + FMS_URL);
}

// on error, fms ports closed
function onSocketError(_e:*):void   {
_resultURLCallBack(PREFIX_80 + "" + FMS_URL);
}
}
}
}

Example of useing Custom Screen Manager in ActionScript 3

Written by . Filed under ActionScript 3. Tagged , , , , , , , , , , . .

Screen Manager.
When i write window based systems i always use a Screen Manager.
when i can control and manage and display object on my screen.

Every display object need to extends ScreenContent class, then
he can attached to the screen manager.

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
package {
import flash.display.Sprite;
// screen content Abstract

public class ScreenContentAbstract extends Sprite {

private var _contentContainer:Sprite;
public function ScreenContentAbstract():void
{

}

public function hideContent():void  {
// hide content
}

public function showContent():void  {
// show content
}

public function set content(_s:Sprite):void {   _contentContainer = s;      }
public function get content():Sprite        {   return _contentContainer;   }

}
}

Screen Manager class:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

public class ScreenManager extends Event
{
private var _contentContainer:Sprite;

public function ScreenManager(_f:Enforcer) {
_contentContainer = new Sprite();
}

// Add content to display
public function addContent(_content:ScreenContentAbstract):void {
_contentContainer.addChild(_content);
_content.addEventListener(MouseEvent.MOUSE_DOWN, contentMouseHandler);
}

// Remove content from display
public function removeContent(_content:ScreenContentAbstract):void  {
if (_contentContainer.contains(_content))
_contentContainer.removeChild(_content);
}

// Hide all content on display
public function hideAllContent():void   {
for (var i:int = 0; i < _contentContainer.numChildren; i++) {
if (_contentContainer.getChildAt(i) is ScreenContentAbstract) {
(_contentContainer.getChildAt(i) as ScreenContentAbstract).hideContent();
}
}
}

// Show all content on display
public function hideAllContent():void   {
for (var i:int = 0; i < _contentContainer.numChildren; i++) {
if (_contentContainer.getChildAt(i) is ScreenContentAbstract) {
(_contentContainer.getChildAt(i) as ScreenContentAbstract).showContent();
}
}
}

// Set focus on object on the display
public function setFocus(_content:ScreenContentAbstract):void   {
_contentContainer.setChildIndex(_content, _contentContainer.numChildren - 1);
}

// mouse handler for content
private function _contentMouseHandler(_e:MouseEvent):void   {
switch(_e.type) {
case MouseEvent.MOUSE_DOWN:
setFocus(_e.target as ScreenContentAbstract);
break;
}
}

// SINGLETON PATTERN
private static var instance:ScreenManager;
public static function getInstance():ScreenManager  {
if (instance == null)
instance = new ScreenManager(new Enforcer());
return instance;
}

}

}
class Enforcer{public function Enforcer()}

As you can see, you can add more functionality this is example only.

Thanks,
Garry Lachman

Simple MVC in ActionScript 3

Written by . Filed under ActionScript 3, Flex. Tagged , , , , , , , , , , , . .

I always used to apply this pattern in most simple way.
Its help me to create few view states use same functionality.

First i create interfaces to Module, Controller and View state:

1
2
3
4
5
6
//Module Interface
package {
flash.events.IEventDispatcher;
public interface IModule extends IEventDispatcher {
}
}
1
2
3
4
5
//Controller Interface
package {
public interface IController {
}
}
1
2
3
4
5
//View Interface
package {
public interface IView {
}
}

Than lets create the MVC:

1
2
3
4
5
6
7
8
9
//Module
package {
flash.events.EventDispatcher;

public class TestModule extends EventDispatcher implements IModule {
public function TestModule(){
}
}
}
1
2
3
4
5
6
7
8
9
10
//Controller
package {
public class TestController implements IController {
private var testModule:IModule;

public function TestController(_module:IModule){
testModule = _module;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//View
package {
import flash.display.Sprite;

public class TestView extends Sprite implements IView {
private var testModule:IModule;
private var testController:IController;

public function TestController(_controller:IController, _module:IModule){
testController = _controller;
testModule = _module;
}
}
}

Now lets connect them all:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Test MVC
package {
import flash.display.Sprite;

public class TestApp extends Sprite {

public function TestApp(){
var testModule:IModule = new TestModule();
var testController:IController = new TestControler(testModule);
var testView:IView = new TestView(testController, testModule);
addChild(testView as Sprite);
}
}
}

Singleton pattern in ActionScript 3

Written by . Filed under ActionScript 3. Tagged , , , , , , , , . .

Singleton pattern in ActionScript 3

the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object.
This is useful when exactly one object is needed to coordinate actions across the system.

Useing of singleton in AS3 code:

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
package
{

public class as3Singleton
{

public function as3Singleton(_e:Enforcer)
{

}

/**
*   singleton static instance holder
*/

private static var instance:as3Singleton;

/**
*
* @return as3Singleton Instance (Signleton)
*
*/

public static function getInstance():as3Singleton
{
if (instance == null)
instance = new as3Singleton(new Enforcer(), _stage);

return instance;
}
}

}
class Enforcer{public function Enforcer(){}}