PDA

View Full Version : 4.0.6 API code bug



ress5609
12-30-2008, 06:00 AM
This seems like a very blatent issue that would effect everyone attempting to use 4.0.6, so maybe I'm doing it wrong, or it's already been found.

I used the ConnectAndLogin example that came with the installation and noticed that the onConnect event in the example code never gets triggered.

After some investigation, I found that in ElectroServer.as, no event is dispatched if the connection is successful.

Here's the function in ElectroServer.as:



public function onConnect(ev:Object):void {

isConnected = false;
var con:AbstractConnection;
for (var i:Number=0;i<connections.length;++i) {
con = connections[i];
if (con.getIsConnected()) {
isConnected = true;
break;
}
}

if (ev.success) {
} else {
var cone:ConnectionEvent = new ConnectionEvent();
cone.setAccepted(false);
cone.setEsError(Errors.FailedToConnect);
dispatchEvent(cone);
//notifyListeners(MessageType.ConnectionEvent, {target:this, success:false});
}
}


The part in question is the if statement at the end of the function. If event.success is true, then nothing happens, and a ConnectionEvent is only dispatched if the connection wasn't successful.

I tested this theory by modifying the code here to be this:


if(ev.success) {
var cone:ConnectionEvent = new ConnectionEvent();
cone.setAccepted(true);
cone.dispatchEvent(cone);
} else {
// unchanged
}


After making that change, the ConnectAndLogin example worked. Unless I'm doing something wrong, this seems to be a pretty big issue if no one can connect to the server without modifying the provided API manually.

MikeParks
12-30-2008, 11:51 PM
This is the first time since 4.0.6 was released that anyone had encountered such a problem. The interesting thing is that I had tested and retested all of the examples before 4.0.6 was released and everything worked as it should. I just successfully built and tested this against 4.0.6 on my sandbox and it works fine without making those changes you noted. Are you seeing these problems in your own code or just the examples?

ress5609
12-31-2008, 12:31 AM
At first it was just the examples, and then I built my own basic test with the same results. I successfully get the event if there is an error connecting (wrong port specified for example), but if there is no error it stops and never dispatches an event.

I put trace actions in along the path starting from es.createConnection(ip, port) to see where it was stopping, and the last trace action called was in ElectroServer.onConnect().

So I then looked at what the ElectroServer.onConnect method does and that's where I noticed that the function is setup to only dispatch an event if event.success is false.

That's why I was wondering if I was either out of my mind or doing something wrong because it seems like it would either have to be dispatching the successful connection event somewhere else, or it would be failing for everyone, but I don't see anywhere else in the class where it dispatches a ConnectionEvent except onClose.

MikeParks
12-31-2008, 01:19 AM
I checked a few previous versions of the API and even those show the same code for onConnect. Nothing has changed for that particular function. Are you sure you're making a connection? If so, could I see your basic test code (not the example that comes with ES)?

ress5609
12-31-2008, 01:54 AM
Sure. Here's my test code. Main is the document class of my SWF.




package
{

import com.electrotank.electroserver4.ElectroServer;
import com.electrotank.electroserver4.message.event.Conne ctionEvent;
import com.electrotank.electroserver4.message.MessageType ;

public class Main extends Sprite
{
private var server:ElectroServer;

public function Main ()
{
server = new ElectroServer();
server.setDebug(true);
server.addEventListener(MessageType.ConnectionEven t, "onConnectionEvent", this);

var ip:String = "192.168.1.79";
var port:Number = 8080;

trace("connect to server: " + ip + ":" + port);
server.createConnection(ip, port);
}


public function onConnectionEvent (e:ConnectionEvent):void
{
trace("connection event");

if (e.getAccepted())
{
trace("successfully connected to the server.");
}
else
{
trace("connection attempt failed: " + e.getEsError().getDescription());
}
}
}
}


I verified that the IP and Port numbers are correct in the ES config. If I change the ip or port in my code, then I get the connection attempt failed trace action. Otherwise I get no trace actions out of the onConnectionEvent function with the original code.

With the code I modified I get the successful connect trace action.

ress5609
12-31-2008, 02:06 AM
Oh also to add, I traced out ElectroServer.isConnected and it traces out as true.

MikeParks
12-31-2008, 02:17 AM
Looks like you're trying to connect to the web admin listener defined in the XML config file. This won't work as it's for configuring the rest of the options in ElectroServer. Navigate to that IP and port in your browser and check to make sure you have proper listeners configured under the Gateway tab. You should be connecting to one of those instead.

ress5609
12-31-2008, 02:26 AM
ah jeez. I feel dumb. Sorry for wasting your time. Although I do find it interesting that it still set isConnected to true and ev.success to true.

MikeParks
12-31-2008, 02:47 AM
Yeah, same here. I'm making a note of it for the next release. Thanks.