> You need access to the EventDispatcher, which is natively mixed in
> with any
> UIObject subclass (which is why all MXML components can use it.
> You can either subclass your class from UIObject, or initialize the
> Event
> Dispatcher with your class. The latter would look like this:
> import mx.events.EventDispatcher;
> function YourConstructor(){
> EventDispatcher.initialize(this);
> }
I tried that (just as you typed it), and a variation of that is in my
example code:
> > var sm:Object = SocketManager.prototype;
> > EventDispatcher.initialize(sm);//also tried
> UIEventDispatcher.initialize and EventDispatcher.initialize(this)
I still get the weird warning when my class tries to call dispatchEvent
that says "Warning: dispatchEvent is not a function".
Any more ideas?
Thanks,
Sean McKibben
>
> At 05:49 PM 4/29/2005, you wrote:
> >I'm trying to wrap an XMLSocket object in a singleton class to manage
> >my (single) XMLSocket connection, but I can't seem to get it to throw
> >any events. Is it that I need to put it into my .mxml file as a
> >component or something? Right now it is just an actionScript class.
> >I was wanting to stick to the cairngorm framework's delegate model,
> but
> >my SocketManager actually initiates commands from the server it is
> >connected to (the whole point of my XMLSocket) so this class will
> need
> >to dispatch a bunch events, preferably using the
> >addEventListener+dispatchEvent model.
> >
> >Whenever the socket connection succeeds, I get my "SocketManager:
> >Socket connection attempt succeeded" trace statement, then I get a
> >warning which says "Warning: dispatchEvent is not a function" which
> >leads me to believe that the EventDispatcher voodoo I'm doing in my
> >constructor isn't working. Is it possible to send events like this?
> >
> >Any help is appreciated.
> >
> >Thanks,
> >Sean McKibben
> >
> >(code below)
> >
> >import com.iterationtwo.cairngorm.business.*;
> >import mx.utils.Delegate;
> >import mx.events.*;
> >[Event("onSocketOpen")]
> >[Event("onSocketClose")]
> >class com.client.capture.business.SocketManager extends MovieClip
> >//(also tried extending mx.core.UIObject and nothing)
> >{
> > public static var socketManager:SocketManager;
> > private static var mySocket:XMLSocket;
> > private static var isSocketConnected:Boolean = false;
> >
> > public var addEventListener:Function;
> > public var removeEventListener:Function;
> > private var dispatchEvent:Function;
> >
> > public function SocketManager()
> > {
> > if ( SocketManager.socketManager != undefined )
> > throw new Error("Only one SocketManager should be
> > instantiated");
> > SocketManager.mySocket = new XMLSocket();
> > SocketManager.mySocket.onConnect = handleConnect;
> > SocketManager.mySocket.onClose = handleClose;
> > SocketManager.mySocket.onXML = handleIncoming;
> > SocketManager.socketManager = this;
> > var sm:Object = SocketManager.prototype;
> > EventDispatcher.initialize(sm);//also tried
> >UIEventDispatcher.initialize
> > }
> > public static function getInstance():SocketManager
> > {
> > if(SocketManager.socketManager == undefined)
> > SocketManager.socketManager = new SocketManager();
> > return SocketManager.socketManager;
> > }
> > public function connect()
> > {
> > if(!SocketManager.isSocketConnected)
> > {
> > if (!SocketManager.mySocket.connect("localhost",
> > 1249))//<--INSERT
> >PORT # HERE!!
> > throw new Error("SocketManager: socket connection
> > attempt failed
> >early");
> > }
> > else
> > throw new Error("SocketManager: connect method called
> > while
> >connected");
> > }
> > public function disconnect()
> > {
> > if(SocketManager.isSocketConnected)
> > {
> > SocketManager.mySocket.close();
> > SocketManager.isSocketConnected = false;
> > trace("SocketManager: closed socket");
> > }
> > else
> > throw new Error("SocketManager: disconnect method
> > called while not
> >connected");
> > }
> > private function handleConnect (succeeded) {
> > if(succeeded) {
> > trace("SocketManager: Socket connection attempt
> > succeeded");
> > SocketManager.isSocketConnected = true;
> > dispatchEvent({type:"onSocketOpen"});
> > } else {
> > throw new Error("SocketManager: Socket connection
> > attempt failed");
> > }
> > }
> > private function handleClose () {
> > trace("SocketManager: server closed connection");
> > SocketManager.isSocketConnected = false;
> > dispatchEvent({type:"onSocketClose"});
> > }
> > private function handleIncoming (messageObj) {
> >
> > // display the received xml data in the output window
> > trace(">>" + messageObj.toString() + "<<");
> > }
> >}
> >
> >
> >then, in other objects i have
> >SocketManager.getInstance().addEventListener("onSocketOpen",
> >mx.utils.Delegate.create(this,onSocketOpen));
> >
> >but it never gets called...
> >
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/flexcoders/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/