Question

So i am working on flex 4.6(before updating to 4.11) on a new project with php services. There is an auto-generated class called MarkerService.as which contains the following:

protected override function preInitializeService():void
{
    super.preInitializeService();
super._serviceControl.endpoint = "http://www.mydomain.gr/gateway.php";
}

However i am building a login system that changes dynamically the server in which the user will connect. So i changed the above to:

public var targetServer:String="test1";
protected override function preInitializeService():void
{
    super.preInitializeService();
super._serviceControl.endpoint = targetServer;

trace(targetServer+" started");
}

public function setTargetServer(s:String):void
{
    targetServer=""+s;
    super._serviceControl.endpoint = targetServer;
    trace(s+" targeted");
}

And then call my setter on an MXML class when the login method is called. However, it seems like the prInitializeService is called during the start of the execution and thus i get a connection error. I attach the logs from the console:

test1 started <--- when app is executed
test1 started <--- when login button is pressed and setter is called
http://www.mydomain.gr/gateway.php targeted
http://www.mydomain.gr/gateway.php started
Send failed
Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Failed: url: 'http:test1'
app:/test1

it seems like the new link is not updated after it is setted and the old test1 is used for the connection. Any ideas?

Was it helpful?

Solution

Well after a few hours of testing and changing the logic, it seems like that the assignment of the endpoint must happen before initializing the _serviceControl. I was trying to implement my logic using states which was buggy. However, when i moved the call to the setter on a new view (loginView) everything worked like a charm. From the looks of it, makes a direct call to the service and it is executed before the app is even displayed.

As a result, the solution for changing dynamically the server your gateway is located, is as follows:

Markers.as (or whatever you call it), changed From

protected override function preInitializeService():void { super.preInitializeService(); super._serviceControl.endpoint = "http://www.mydomain.com/gateway.php"; }

To

public var targetServer:String="";
protected override function preInitializeService():void
{
    super.preInitializeService();
    super._serviceControl.endpoint = "http://www.mydomain.com/gateway.php";
}

public function setTargetServer(s:String):void
{
    targetServer=s;
    super._serviceControl.endpoint = targetServer;
    //trace(s+" targeted");
    preInitializeService();
}

And in your login.mxml

var m:services.markersservice.MarkersService=new services.markersservice.MarkersService();
            m.setTargetServer("http://www.mydomain.com/gateway.php");
            navigator.pushView(HomeView);

I hope this will help more Flex developers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top