Home > Blockchain >  How to verify this type of variable?
How to verify this type of variable?

Time:01-01

I got the code:

this.socket.connect("127.0.0.1",1000);

And i want if The ip or the port is different to do the function securityError

Like if the code is like

this.socket.connect("129.124.7.17",1000);

or

this.socket.connect("127.0.0.1",1236);

or both of them are different to do the function securityError

I was thinking it should look something like this:

if(this.socket.connect != ("127.0.0.1",1000))
{
this.securityError
}

CodePudding user response:

And the answer is... event handling.

But first, let's learn, what are events and why do we need them.

Flash Player is a platform that runs its applications in a certain specific frame-by-frame way, endlessly repeating the same cycle:

  1. Execute scripts (frame scripts of the MovieClips and all the events occured since the previous frame).
  2. Render the display list (which includes the result of script execution as well as things that change frame-to-frame by design, like MovieClip's timelines) and display the rendered result.

The rendering phase does not start until the script execution phase is over. If you put some infinitely running script, the rendering phase will not start at all:

while (true)
{
    // Do nothing.
}

How is it relevant to sockets at all? The thing is, there are certain operations (most of which are related to the outer environment) that are not guaranteed to be completed within a given time (or completed at all):

  • Loading a file from a local drive.
  • Sending and receiving data over HTTP request.
  • (and also) Connecting to a TCP socket, sending and receiving data to/from it.

If you try to wait for these (and other similar) operations to complete while in the script execution phase, you might stop your application completely.

So, how do you avoid ending up like that?

The answer is: asynchronous operations and events. What is asynchronous? The operations that run in their own tempo in parallel with the main Flash Player cycle mentioned above.

You should think in a multi-thread-like manner and compose your AS3 logic in a small chunks of code that start the non-guaranteed operation and finish the script to allow the rendering phase to happen, and the other chunks of code are executed WHEN something comes up while these long-running non-guaranteed operations do their things. The important thing is that it is not important, WHEN. We just assume it happens some time later.

Let's see some example:

// WARNING: This one snippet is NOT a real AS3 code,
// just an example of synchronous thinking.

function loadSettings(fileName:String):void
{
    var aSettings:String = LocalFile.load(fileName);
    
    if (aSettings)
    {
        // The file was loaded successfully, process the loaded data.
    }
    else
    {
        // The file failed to load. React to it.
    }
}

Then, how it is really done in AS3:

function loadSettings(fileName:String):void
{
    // Here I let the aLoader be a local function variable,
    // yet keep in mind that you ALWAYS need to store such
    // an object attached to scope in some way. Otherwise
    // the Garbage Collector might just eat it away.
    var aLoader:URLLoader = new URLLoader;
    
    // Subscribe to events of the loading process.
    
    // Successful loading.
    aLoader.addEventListener(Event.COMPLETE, onSettings);
    
    // Error cases.
    aLoader.addEventListener(IOErrorEvent.IO_ERROR, onFailed);
    aLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onFailed);
    
    // Start asynchronous loading process.
    aLoader.load(new URLRequest(fileName));
    
    // The script is done here.
}

Let's handle the events.

function onSettings(e:Event):void
{
    // Event ALWAYS has a reference to the object,
    // but you need to typecast it to the appropriate class.
    var aLoader:URLLoader = e.target as URLLoader;
    
    // The file was loaded successfully, process the loaded data.
}

function onFailed(e:Event):void
{
    var aLoader:URLLoader = e.target as URLLoader;
    
    // The file failed to load. React to it.
}

As you can see, it is exactly the same logic in both cases, but event handling allows the file loading to take as much time as needed, without making the Flash Player stutter.

I also encourage you to search something like as3 events explained and read up all the relevant tutorials.

With your sockets, it's exactly the same. You have several possibly valid host:port pairs, you want your script to connect to the any one available, what you need is to stop thinking single-threaded-ly and start thinking with events:

var aSock:Socket;

var aList:Array = [
    {host:"127.0.0.1", port:1000},
    {host:"127.0.0.1", port:1236},
    {host:"129.124.7.17", port:1000},
];

function tryNext():void
{
    // Create a new socket instance.
    aSock = new Socket;
    
    // Subscribe to possible outcomes.
    aSock.addEventListener(Event.CONNECT, onConnected);
    aSock.addEventListener(IOErrorEvent.IO_ERROR, onFailed);
    aSock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onFailed);
    
    // Try connecting to the next server from the list.
    var aServer:Object = aList[0];
    
    // Start the asynchronous operation.
    aSock.connect(aServer.host, aServer.port);
}

// Handle the failed-to-connect case.
function onFailed(e:Event):void
{
    // Sanity check: ignore the event, if
    // it is not from the current socket object.
    var aSocket:Socket = e.target as Socket;
    if (aSocket != aSock) return;
    
    // Remove the failed server from the list.
    aList.shift();
    
    // Try next server, if there are any left.
    if (aList.length)
    {
        tryNext();
    }
    else
    {
        // None of the given servers responded.
        // Process the case here.
    }
}

// Handle the failed-to-connect case.
function onConnected(e:Event):void
{
    // Sanity check, again.
    var aSocket:Socket = e.target as Socket;
    if (aSocket != aSock) return;
    
    // If we're here, that means everything is fine and the socket aSock
    // is successfully connected to the server with the host:port pair
    // from the aList[0] object. You can start reading and writing
    // data from/to socket from here on.
}

CodePudding user response:

I was thinking it should look something like this:

if( this.socket.connect != ("127.0.0.1",1000) )
{
    this.securityError
}

If you're making an AIR project (ie: who makes SWF for browser anymore??) you may want to look at the Socket commands where something like remotePort and remoteAddress might be useful to you (make sure this.socket exists and be aware that your this.securityError code won't do anything, you need to throw an Error or do next step(s) if the connection is not correct) :

if( (this.socket.remoteAddress != "127.0.0.1") && (this.socket.remotePort != 1000) )
{
   this.securityError
}
  • Related