>static var mixIt = EventDispatcher.initalize(SocketManager.prototype);
That is at all not nescessary


I rewrote a part of your code, wich is pasted below, here the code to
test:

<?xml version="1.0" encoding="utf-8"?>

<mx:Application
    verticalGap="0"
    backgroundColor="#7D8FA8"
    xmlns:mx="http://www.macromedia.com/2003/mxml"; xmlns="*"
    pageTitle="Things"
        width="690" height="484" initialize="start()"
        >
        <mx:Script>
        <![CDATA[
                public function start()
                {
                        var sm:SocketManager =
SocketManager.getInstance();
                        sm.addEventListener("onConnectTest", this);
                        sm.connect();
                
                };
                
                public function onConnectTest(event_obj)
                {
                        mx.core.Application.alert("onConnectTest");
                };
        ]]>
        </mx:Script>
</mx:Application>

And here the class:


import mx.utils.Delegate;
import mx.events.*;

class SocketManager
{
        static private var _instance:SocketManager;
        
        private var _socket:XMLSocket;
        private var _isSocketConnected_bool:Boolean = false;
        
        public var addEventListener:Function;
        public var removeEventListener:Function;
        private var dispatchEvent:Function;

        private function SocketManager()
        {       
                _socket = new XMLSocket();
                _socket.onConnect = Delegate.create(this,
_handleConnect);
                _socket.onClose = Delegate.create(this, _handleClose);
                _socket.onXML = Delegate.create(this, _handleIncoming);
                EventDispatcher.initialize(this);
        };
        
        static public function getInstance():SocketManager
        {
                if (!_instance)
                {
                        _instance = new SocketManager();
                };
                return _instance;
        }
        
        public function connect(url_str, port_num)
        {
                //for the test:
                dispatchEvent({type: "onConnectTest"});
                
                if(!isSocketConnected)
                {
                        if (!_socket.connect(url_str, port_num)) 
                        {
                                throw new Error("SocketManager: socket
connection attempt failed early");
                        };
                } else
                {
                        throw new Error("SocketManager: connect method
called while connected");
                };
        };
        
        public function get isSocketConnected():Boolean
        {
                return _isSocketConnected_bool;
        };
        
        public function set
isSocketConnected(isSocketConnected_bool:Boolean):Void
        {
                //read only
        };
        
        private function _handleConnect(success_bool:Boolean) 
        {
                if(success_bool)
                {
                        trace("SocketManager: Socket connection attempt
succeeded");
                        _isSocketConnected_bool = true;
                        dispatchEvent({type:"onSocketOpen"});
                };
        };
        
        private function _handleClose () 
        {
                trace("SocketManager: server closed connection");
                _isSocketConnected_bool = false;
                dispatchEvent({type:"onSocketClose"});
        };
        
        private function _handleIncoming(messageObj) 
        {
                // display the received xml data in the output window
                trace(">>" + messageObj.toString() + "<<");
        };
};


Greetz Erik


-----Original Message-----
From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of JesterXL
Sent: zaterdag 30 april 2005 0:48
To: flexcoders@yahoogroups.com
Subject: Re: [flexcoders] dispatchEvent in AS class?

Add this as a static member:

static var mixIt = EventDispatcher.initalize(SocketManager.prototype);

----- Original Message -----
From: "Sean McKibben" <[EMAIL PROTECTED]>
To: <flexcoders@yahoogroups.com>
Sent: Friday, April 29, 2005 5:49 PM
Subject: [flexcoders] dispatchEvent in AS class?


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


 
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/
 



Reply via email to