1. Victor Kotseruba
  2. FlSock

Commits

Victor Kotseruba  committed 07f9624

externals callbacks "flsock" prefix added and reconnect tested in some cases

  • Participants
  • Parent commits e23c23a
  • Branches default

Comments (0)

Files changed (1)

File FlSock.as

View file
  • Ignore whitespace
     
     import flash.display.LoaderInfo;
     import flash.display.Sprite;
-    import flash.events.ErrorEvent;
     import flash.events.Event;
     import flash.events.IOErrorEvent;
     import flash.events.ProgressEvent;
     import flash.events.SecurityErrorEvent;
     import flash.external.ExternalInterface;
     import flash.net.Socket;
+    import flash.utils.setTimeout;
     
     public class FlSock extends Sprite {
         
         protected var roomStart:String = 'r';
         protected var sock:Socket;
         protected var data:String = '';
+        protected var connectScheduled:Boolean = true;
         
         public function FlSock() {
             super();
-            sock = new Socket();
-            sock.addEventListener(Event.CONNECT, connectHandle);
-            sock.addEventListener(Event.CLOSE, closeHandle);
-            sock.addEventListener(ErrorEvent.ERROR, errorHandle);
-            sock.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandle);
-            sock.addEventListener(ProgressEvent.SOCKET_DATA, dataHandle);
-            sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityHandle);
             loadParameters();
             connect();
         }
         
         protected function connect():void {
-        	try {
-        		sock.connect(host, port);
-        	} catch (e:SecurityError) {
-        	}
+            sock = new Socket();
+            sock.addEventListener(Event.CONNECT, connectHandle);
+            sock.addEventListener(Event.CLOSE, closeHandle);
+            sock.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandle);
+            sock.addEventListener(ProgressEvent.SOCKET_DATA, dataHandle);
+            sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityHandle);
+            connectScheduled = false;
+            sock.connect(host, port);
         }
         
         protected function loadParameters():void {
-            try {
-                var params:Object = LoaderInfo(loaderInfo).parameters;
-                if (params.hasOwnProperty('host')) {
-                    host = String(params['host']);
-                }
-                if (params.hasOwnProperty('port')) {
-                    port = int(params['port']);
-                }
-            } catch (e:Error) {
+            var params:Object = LoaderInfo(loaderInfo).parameters;
+            if (params.hasOwnProperty('host')) {
+                host = String(params['host']);
+            }
+            if (params.hasOwnProperty('port')) {
+                port = int(params['port']);
             }
         }
         
+        protected function timedConnect():void {
+            if (connectScheduled) return;
+            connectScheduled = true;
+            setTimeout(connect, 1000);
+        }
+        
         protected function securityHandle(e:SecurityErrorEvent):void {
-        	
+            if (sock.connected) return;
+            ExternalInterface.call('flsock_securityHandle');
+            timedConnect();
         }
         
         protected function connectHandle(e:Event):void {
             ExternalInterface.addCallback('sendMessage', sendMessage);
             ExternalInterface.addCallback('joinRoom', joinRoom);
-            ExternalInterface.call('connectHandle');
+            ExternalInterface.call('flsock_connectHandle');
         }
         
         protected function closeHandle(e:Event):void {
-            connect();
+            ExternalInterface.call('flsock_closeHandle');
+            timedConnect();
         }
-        
-        protected function errorHandle(e:ErrorEvent):void {
-            connect();
-        }
-        
+                
         protected function ioErrorHandle(e:IOErrorEvent):void {
-            connect();
+            ExternalInterface.call('flsock_ioErrorHandle');
+            if (sock.connected) return;
+            timedConnect();
         }
         
         protected function dataHandle(e:ProgressEvent):void {
         }
         
         protected function messageHandle(message:String):void {
-            ExternalInterface.call('handleMessage', message);
+            ExternalInterface.call('flsock_handleMessage', message);
+        }
+        
+        protected function push(data:String):void {
+            if (sock.connected) {
+                try {
+                    this.sock.writeUTFBytes(data + term);
+                } catch(e:Error) {
+                }
+            }
         }
         
         protected function sendMessage(message:String):void {
-            this.sock.writeUTFBytes(messageStart + message + term);
+            push(messageStart + message);
         }
         
         protected function joinRoom(room:String):void {
-            this.sock.writeUTFBytes(roomStart + room + term);
+            push(roomStart + room);
         }
     }
 }