I have a class called socket.as and is responsible for socket connection etc. It has an event listener onSocketData, which is called whenever data is received. In my main class, Main.as, i have instantiated an object of socket class.
How can I get data from socket class? Do i have to create a custom event in Main.as that should be triggered by socket class?
Thank you.
below is the event listener from socket class:
private function onSocketData(event:ProgressEvent):void <br>
{
//string sent over serial port.
var data:String = _socket.readUTFBytes( _socket.bytesAvailable );
var direction:int = 0;
buffer += data;
while((index = buffer.indexOf(EOL_DELIMITER)) > -1)
{
msg = buffer.substring(0, index-1);
len = (buffer.indexOf(EOL_DELIMITER)) - 1;
//remove the message from the buffer
buffer = buffer.substring(index + 1);
if ( msg != "off" )
{
button = parseInt(msg.substr(6, (len-8)));
trace("Socket Data: " + msg + ", Button: " + button);
}
}
}
As you mention , you could create a CustomEvent that will be triggered by the Socket class or you could dispatch an Event from the Socket class , whenever data is available and use a getter to retrieve the variable.
I’ve answered a similar question here:
AS3 Passing data between objects/classes