1

Within a Flex application (not AIR, but Flash player) I have an XMLSocket running, and for the life of me I can't figure out how to detect a client-side disconnect. As in, when I connect to a remote server from my computer, then disconnect the computer from the internet, the socket believes it is still connected (no IOError or close Event thrown). Within my socket class I have it sending a message to the socket every two seconds, in the hope that once the connection is cut it would detect an IOError on XMLSocket.send(), but even this doesn't work! It still tries to send the data, and doesn't throw an exception, though surely it's failing.

When the internet connection is up, the socket server on the remote server does pick up the message from the Flex client.

I've looked into SocketMonitor and URLMonitor, but they're only available for AIR, not flash player.

I imagine there is something fundamental about sockets I haven't understood. Can anyone help?

package org.MB.components
{
import flash.errors.IOError;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.events.TimerEvent;
import flash.net.XMLSocket;
import flash.utils.Timer;

import org.MB.appData.DataHolder;
import org.MB.utils.XMLUtils;

public class MBSocket extends XMLSocket
{
    private var timer:Timer;

    private var _host:String;
    private var _port:int;

    public function MBSocket(host:String=null, port:int=0)
    {
        super(host, port);

        this._host = host;
        this._port = port;

        addListeners();
    }

    public function addListeners():void
    {
        addEventListener(Event.CLOSE, onClose);
        addEventListener(Event.CONNECT, onConnect);
        addEventListener(IOErrorEvent.IO_ERROR, onIOError);
        addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);

        addEventListener(DataEvent.DATA, onData);
    }

    //Various event handlers and code to deal with the two-second timer here

    protected function onConnectedTimer(e:TimerEvent):void
    {
        //I get this traced message, and no IOError is thrown
        trace("SENDING TEST");
        this.send(':::\0'); 
    }
}
Michael Beeson
  • 2,840
  • 2
  • 17
  • 25
  • are you saying the client doesn't realize it has no internet? or that the server doesn't realize it has no client any longer? – Jason Reeves Nov 21 '12 at 16:15
  • The first one – by disconnecting the wireless on the client computer I hope to simulate a bad connection – Michael Beeson Nov 21 '12 at 22:46
  • @MichaelBeeson I have same problem. XMLSocket timeout property works only for during connecting. Please, have you found solution for this? – Nemi May 02 '13 at 17:42

0 Answers0