Skip to content
Browse files

fixing reconnect

  • Loading branch information...
1 parent 9efc67e commit 069bb494adbba12b3bf1b9d9b8a6702a19223931 @leonlee committed Jan 29, 2014
Showing with 63 additions and 117 deletions.
  1. +1 −0 .gitignore
  2. +0 −30 amf_socket.iml
  3. +25 −78 src/amfSocket/AmfSocket.as
  4. +37 −9 src/amfSocket/RpcManager.as
View
1 .gitignore
@@ -8,3 +8,4 @@
.settings
.project
.flexLibProperties
+amf_socket.iml
View
30 amf_socket.iml
@@ -1,30 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<module type="Flex" version="4">
- <component name="FlexBuildConfigurationManager" active="amf_socket">
- <configurations>
- <configuration name="amf_socket" target-platform="Mobile" pure-as="true" output-type="Library" output-file="amf_socket.swc" output-folder="$MODULE_DIR$/../out/production/amf_socket">
- <dependencies target-player="11.8">
- <sdk name="air3-8_sdk_win" />
- </dependencies>
- <compiler-options />
- <packaging-air-desktop />
- <packaging-android />
- <packaging-ios />
- </configuration>
- </configurations>
- <compiler-options />
- </component>
- <component name="NewModuleRootManager" inherit-compiler-output="true">
- <exclude-output />
- <content url="file://$MODULE_DIR$">
- <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
- </content>
- <content url="file://$MODULE_DIR$/../bert_as3">
- <sourceFolder url="file://$MODULE_DIR$/../bert_as3/src/main/flex" isTestSource="false" />
- <sourceFolder url="file://$MODULE_DIR$/../bert_as3/src/test/flex" isTestSource="true" />
- </content>
- <orderEntry type="jdk" jdkName="air3-8_sdk_win" jdkType="Flex SDK Type (new)" />
- <orderEntry type="sourceFolder" forTests="false" />
- </component>
-</module>
-
View
103 src/amfSocket/AmfSocket.as
@@ -6,56 +6,50 @@ import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
-import flash.events.TimerEvent;
import flash.net.ObjectEncoding;
import flash.net.Socket;
import flash.utils.ByteArray;
import flash.utils.Endian;
-import flash.utils.Timer;
+
+import org.as3commons.logging.api.ILogger;
+import org.as3commons.logging.api.getLogger;
public class AmfSocket extends EventDispatcher {
- public static const CHARSET_LATIN1:String = 'iso-8859-1';
public static const FORMAT_AMF3:int = 1;
- public static const FORMAT_BERT:int = 2;
- public static const MAX_RECONNECT_TIME:int = 3;
-
- //
- // Instance variables.
- //
-
- private static var _logger:Object;
+ public static const FORMAT_MSGPACK:int = 2;
+ private static const _logger:ILogger = getLogger(AmfSocket);
- public static function get logger():Object {
- return _logger;
- }
-
- public static function set logger(value:Object):void {
- _logger = value;
+ public static function log(message:String):void {
+ if (!_logger) {
+ return;
+ }
+ _logger.debug(message);
}
public function AmfSocket(host:String = null, port:int = 0, compress:Boolean = false, format:int = FORMAT_AMF3) {
_host = host;
_port = port;
_compress = compress;
_format = format;
- if (_format == FORMAT_BERT) {
- _bert = new Bert();
+ if (_format == FORMAT_MSGPACK) {
+ throw new Error("not implemented msgpack");
}
}
private var _format:int;
- private var _bert:Bert;
private var _host:String = null;
private var _port:int = 0;
private var _socket:Socket = null;
- private var _objectLength:int = -1;
private var _buffer:ByteArray = new ByteArray();
- private var _compress:Boolean = false;
- private var _connecting:Boolean = false;
- private var _reconnectTimer:Timer = new Timer(2000, MAX_RECONNECT_TIME + 1);
+
//
// Constructor.
//
+ private var _compress:Boolean = false;
+
+ //
+ // Public methods.
+ //
public function get connected():Boolean {
if (!_socket || !_socket.connected)
@@ -64,26 +58,10 @@ public class AmfSocket extends EventDispatcher {
return true;
}
- //
- // Public methods.
- //
-
- public function log(message:String):void {
- var foo:Object = _logger;
-
- if (!_logger)
- return;
-
- _logger.debug(message);
- }
-
public function connect():void {
if (connected)
throw new Error('Can not connect an already connected socket.');
- if (_connecting)
- return;
- _connecting = true;
_socket = new Socket();
_socket.endian = Endian.BIG_ENDIAN;
_socket.objectEncoding = ObjectEncoding.AMF3;
@@ -103,44 +81,16 @@ public class AmfSocket extends EventDispatcher {
}
public function sendObject(object:Object):void {
- if (!connected) {
-// throw new Error('Can not send over a non-connected socket.');
- var reconnectHandle:Function = function (event:TimerEvent):void {
- reconnect(object, reconnectHandle);
- };
- _reconnectTimer.addEventListener(TimerEvent.TIMER, reconnectHandle);
- _reconnectTimer.start();
- return;
- }
+ if (!connected)
+ throw new Error('Can not send over a non-connected socket.');
if (_format == FORMAT_AMF3) {
var byteArray:ByteArray = encodeObject(object);
_socket.writeUnsignedInt(byteArray.length);
_socket.writeBytes(byteArray);
_socket.flush();
- } else {
- _bert.writePacket(object, _socket);
- _socket.flush();
- }
- }
-
- private function reconnect(object:Object, handle:Function):void {
- if (_reconnectTimer.currentCount >= MAX_RECONNECT_TIME) {
- _reconnectTimer.removeEventListener(TimerEvent.TIMER, handle);
- _reconnectTimer.stop();
- throw new Error('Can not send over a non-connected socket.');
- }
- if (!connected) {
- var resend:Function = function ():void {
- _socket.removeEventListener(Event.CONNECT, resend);
- if (!connected) {
- reconnect(object, handle);
- } else {
- sendObject(object);
- }
- };
- _socket.addEventListener(Event.CONNECT, resend);
- connect();
+ } else if (_format == FORMAT_MSGPACK) {
+ throw new Error("not implemented msgpack");
}
}
@@ -197,8 +147,8 @@ public class AmfSocket extends EventDispatcher {
var object:* = null;
if (_format == FORMAT_AMF3) {
object = _buffer.readObject();
- } else {
- object = _bert.read(_buffer, payloadSize);
+ } else if (_format == FORMAT_MSGPACK) {
+ throw new Error("not implemented msgpack");
}
shiftBuffer(4 + payloadSize);
@@ -221,14 +171,13 @@ public class AmfSocket extends EventDispatcher {
private function socket_connect(event:Event):void {
log('Connected.');
- _connecting = false;
+
dispatchEvent(new AmfSocketEvent(AmfSocketEvent.CONNECTED));
}
private function socket_disconnect(event:Event):void {
log('Disconnected.');
- _connecting = false;
removeEventListeners();
_socket = null;
@@ -238,14 +187,12 @@ public class AmfSocket extends EventDispatcher {
private function socket_ioError(event:IOErrorEvent):void {
log('IO Error.');
- _connecting = false;
dispatchEvent(new AmfSocketEvent(AmfSocketEvent.IO_ERROR));
}
private function socket_securityError(event:SecurityErrorEvent):void {
log('Security Error.');
- _connecting = false;
dispatchEvent(new AmfSocketEvent(AmfSocketEvent.SECURITY_ERROR));
}
View
46 src/amfSocket/RpcManager.as
@@ -7,11 +7,15 @@ import flash.events.TimerEvent;
import flash.utils.Dictionary;
import flash.utils.Timer;
-public class RpcManager extends EventDispatcher {
+import org.as3commons.logging.api.ILogger;
+import org.as3commons.logging.api.getLogger;
+public class RpcManager extends EventDispatcher {
+ private static const logger:ILogger = getLogger(RpcManager);
//
// Constructor.
//
+
public function RpcManager(host:String, port:int, options:Object = null) {
super();
@@ -22,15 +26,19 @@ public class RpcManager extends EventDispatcher {
if (options == null)
options = {};
- if (options['autoReconnect'] == null)
- options['autoReconnect'] = 2;
+ if (options['autoReconnect'] === null)
+ options['autoReconnect'] = 3;
- if (options['autoReconnect']) {
+ if (options['autoReconnect'] !== null) {
_reconnectTimer = new Timer(3000, options['autoReconnect']);
_reconnectTimer.addEventListener(TimerEvent.TIMER, reconnectTimer_timer);
_reconnectTimer.start();
}
+ if (options['reconnectHandler'] != null) {
+ _reconnectHandler = options['reconnectHandler'];
+ }
+
_compress = options['compress'] != null && options['compress'] != false;
_format = options['format'] != null ? options['format'] : AmfSocket.FORMAT_AMF3;
}
@@ -46,7 +54,8 @@ public class RpcManager extends EventDispatcher {
private var _reconnectTimer:Timer = null;
private var _requests:Dictionary = new Dictionary();
private var _compress:Boolean = false;
- private var _format = AmfSocket.FORMAT_AMF3;
+ private var _format:int = AmfSocket.FORMAT_AMF3;
+ private var _reconnectHandler:Function = null;
private var _latency:Number = 0.0;
@@ -112,12 +121,12 @@ public class RpcManager extends EventDispatcher {
public function deliver(rpcObject:RpcObject):void {
try {
- if (rpcObject.hasOwnProperty('__signalSucceeded__'))
- _requests[rpcObject.messageId] = rpcObject;
-
var object:Object = rpcObject.toObject();
_socket.sendObject(object);
+ if (rpcObject.hasOwnProperty('__signalSucceeded__'))
+ _requests[rpcObject.messageId] = rpcObject;
+
rpcObject.__signalDelivered__();
} catch (error:Error) {
dispatchEvent(new RpcManagerEvent(RpcManagerEvent.FAILED, error));
@@ -185,9 +194,13 @@ public class RpcManager extends EventDispatcher {
}
private function __connect():void {
+ var OldState:String = _state;
_state = 'connecting';
_socket = new AmfSocket(_host, _port, _compress, _format);
+ if (OldState == 'disconnected') {
+ _socket.addEventListener(AmfSocketEvent.CONNECTED, socket_reconnected);
+ }
addSocketEventListeners();
_socket.connect();
}
@@ -198,7 +211,7 @@ public class RpcManager extends EventDispatcher {
}
private function cleanUp(reason:String = null):void {
- if (!_socket) {
+ if (_socket) {
removeSocketEventListeners();
_socket.disconnect();
_socket = null;
@@ -313,6 +326,13 @@ public class RpcManager extends EventDispatcher {
return true;
}
+ private function socket_reconnected(event:AmfSocketEvent):void {
+ _socket.removeEventListener(AmfSocketEvent.CONNECTED, socket_reconnected);
+ if (_reconnectHandler) {
+ _reconnectHandler();
+ }
+ }
+
//
// Event handlers.
//
@@ -365,9 +385,17 @@ public class RpcManager extends EventDispatcher {
}
private function reconnectTimer_timer(event:TimerEvent):void {
+ logger.debug("reconnect timer...");
+ if (_reconnectTimer && _reconnectTimer.running) {
+ _reconnectTimer.stop();
+ }
if (isFailed() || isDisconnected()) {
+ logger.debug("reconnecting...");
reconnect();
}
+ if (_reconnectTimer && !_reconnectTimer.running) {
+ _reconnectTimer.start();
+ }
}
}
}

0 comments on commit 069bb49

Please sign in to comment.
Something went wrong with that request. Please try again.