PDA

View Full Version : Issue with Event.CLOSE and XMLSocket


RossMcCaughrain
02-10-2010, 05:10 PM
Hi all,

Just wondering if any of you have come across this? Basically when my Socket closes my event handler for Event.CLOSE is not firing although my other Event Listeners are working fine. My code is


package sockets {

import events.ClientRequestEvent;
import events.ServerResponseEvent;

import flash.display.Sprite;
import flash.events.*;
import flash.net.XMLSocket;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class XMLClientSocket extends Sprite {

public function XMLClientSocket() {
this.addEventListener(Event.ADDED_TO_STAGE,init);
this.addEventListener(Event.REMOVED_FROM_STAGE,cleanUp);
}

private function init(myEvent:Event):void {

// Initialise our Socket and Socket Events
_socket = new XMLSocket();
_socket.addEventListener(Event.CONNECT, connectHandler);
_socket.addEventListener(Event.CLOSE, closeHandler);
_socket.addEventListener(DataEvent.DATA, dataHandler);
_socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);

// Setup our idle Timer which will disconnect us freeing up sockets after a set period
_idleTimer = new Timer(_idleTimeout, 0);
_idleTimer.addEventListener(TimerEvent.TIMER, connectionIdleHandler);
}

private function cleanUp(myEvent:Event):void {

// Remove our Socket and Socket Events
_socket.removeEventListener(Event.CLOSE, closeHandler);
_socket.removeEventListener(Event.CONNECT, connectHandler);
_socket.removeEventListener(DataEvent.DATA, dataHandler);
_socket.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
_socket.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);


// Remove our idle Timer and free up resources
_idleTimer.stop();
_idleTimer = null;
_socket.close();
_socket = null;
}

private function connectToServer(myEvent:ClientRequestEvent):void {
_host = myEvent.parameters["host"];
_port = myEvent.parameters["port"];
if (!_socket.connected && _host && _port)
_socket.connect(_host, _port);
}

private function disconnectFromServer(myEvent:ClientRequestEvent):void {

if(_socket.connected) {
trace("XML Socket disconnecting");
_socket.close();
}
}

private function sendToServer(myEvent:ClientRequestEvent):void {
_idleTimer.reset();

if(_socket.connected)
_socket.send("");
}

private function connectHandler(event:Event):void {
_idleTimer.start();
}

private function closeHandler(event:Event):void {
trace("Close Handler");
_idleTimer.stop();
}

private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}

private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}

private function connectionIdleHandler(event:TimerEvent):void {
trace("timerHandler: " + event);
_socket.close();
}

private var _host:String;
private var _port:uint;
private var _socket:XMLSocket;

private var _idleTimer:Timer;
private var _idleTimeout:uint = 300000;
}
}



On Closing i get the "XML Socket disconnecting" trace but not the "Close Handler" message.

Any Ideas?

Cheers
Ross

RossMcCaughrain
02-10-2010, 09:54 PM
Am an idiot. Thought i had read the documentation to death but turns out i missed the one line i needed!

Closes the connection specified by the XMLSocket object. The close event is dispatched only when the server closes the connection; it is not dispatched when you call the close() method.

Oops! Sorry for wasting time!

eu4ria
02-12-2010, 04:03 PM
Glad that you fixed your problem and posted the answer so that other people that may find this topic will see the response too.