Hello people here,
I’m new here, so maybe i’m posting in the wrong channel. Please tell me if it is the case…
I’m trying to get a simple client socket class to work with events.
I have tried each example found with google with no luck, i just tried the “radio listeners” example from here, which worked fine until i put the broadcaster object within my class…
Main code:
import mx.events.EventDispatcher;
// A listener object for server events.
this.srvListener = new Object();
this.srvListener.onSrvConnect = function(eObject:Object):Void {
trace("From Listener: onConnect()");
};
// Instanciate the main client socket class.
var cMainCon:CServerConnexion = CServerConnexion.GetInstance();
// Add the main listener object to it...
this.cMainCon.cBroadcaster.addEventListener("onSrvConnect", this.srvListener);
// Let's connect now.
this.cMainCon.Connect("localhost", 8081);
Class code:
import mx.events.EventDispatcher;
class CServerConnexion {
// The Singleton instance.
private static var cInstance :CServerConnexion;
// The main socket.
private var cMainSock :XMLSocket;
public var cBroadcaster :Object;
// Main Constructor.
private function CServerConnexion() {
// Set up this class to be event aware.
this.cBroadcaster = new Object();
EventDispatcher.initialize(this.cBroadcaster);
// Instanciate the main server socket.
this.cMainSock = new XMLSocket();
// Use those events handlers on connexion.
this.cMainSock.onConnect = this.onConnect;
this.cMainSock.onXML = this.onXML;
this.cMainSock.onClose = this.onClose;
}
// Connects to the server.
public function Connect(szHost:String, iPort:Number) {
this.cMainSock.connect(szHost, iPort);
}
// Event handler, on connection.
public function onConnect(bSuccess:Boolean) {
trace("From Main class: onConnect()");
this.cBroadcaster.dispatchEvent({type:"onSrvConnect"});
}
public function onXML(szData:XML) {}
public function onClose() {}
public static function GetInstance():CServerConnexion {
if (CServerConnexion.cInstance==undefined)
CServerConnexion.cInstance = new CServerConnexion();
return CServerConnexion.cInstance;
}
}
When i start the flash, i only get the class message (“From Main class: onConnect()”)…
Any kind of help (Clues, links, incomplete sentenses :te:) greatly appreciated. Thanks
Kind regards,
Pierre