Public feed
#889254
package hypernumbers.remoting
{
import flash.events.Event;
/**
* This custom Event class adds a message property to a basic Event.
*/
public class RemotingEvent extends Event
{
/**
* The name of the new RemotingEvent type.
*/
public static const REMOTING:String = "newRemotingMsg";
/**
* A text message that can be passed to an event handler
* with this event object.
*/
public var message:String;
/**
* Constructor.
* @param message The text to display when the alarm goes off.
*/
public function RemotingEvent(message:String)
{
super(REMOTING);
this.message = message;
}
/**
* Creates and returns a copy of the current instance.
* @return A copy of the current instance.
*/
public override function clone():Event
{
return new RemotingEvent(message);
}
/**
* Returns a String containing all the properties of the current
* instance.
* @return A string representation of the current instance.
*/
public override function toString():String
{
return formatToString("RemotingEvent", "type", "bubbles", "cancelable", "eventPhase", "message");
}
}
}
package hypernumbers.remoting
{
import flash.events.*;
import flash.net.Socket;
import flash.system.Security;
import flash.utils.ByteArray;
public class RemotingSocket extends Socket
{
private const CTRLJ:int = 10;
private var _queue:Array = new Array();
private var returnMsg:String = "";
public function RemotingSocket(server:String,port:int)
{
this.addEventListener(ProgressEvent.SOCKET_DATA, dataHandler);
this.addEventListener(Event.CONNECT, connectHandler);
try {
this.connect(server, port);
}catch (error:Error) {
this.close();
}
}
/**
* Helper function to write to a socket
*/
public function write(ba:String):void
{
if(this.connected)
{
this.writeUTFBytes(ba);
this.flush();
}
else
{
_queue.push(ba);
}
}
/**
* When socket connects, send out any buffered messages
*/
private function connectHandler(event:Event):void
{
if (connected)
{
var count:int = _queue.length;
for (var i:int = 0; i < count; i++)
{
var tmp:String = _queue.pop();
this.writeUTFBytes(tmp);
this.flush();
}
}
}
/**
* Received data, create a new event to send it on
*/
private function dataHandler(event:ProgressEvent):void
{
var n:int = this.bytesAvailable;
while (--n >= 0)
{
var b:int = this.readUnsignedByte();
if (b == CTRLJ)
{
var remotingEvent:RemotingEvent = new RemotingEvent(this.returnMsg);
this.dispatchEvent(remotingEvent);
returnMsg="";
}
else if (b != CTRLJ)
{
returnMsg += String.fromCharCode(b);
}
}
}
}
}
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="init();">
<mx:Script><![CDATA[
import flash.system.Security;
import flash.external.ExternalInterface;
import hypernumbers.remoting.*;
private var _socket:RemotingSocket;
private var connect_cb:String = null;
private var disconnect_cb:String = null;
private var recieve_cb:String = null;
private var ioerror_cb:String = null;
private var securityerror_cb:String = null;
private var progressevent_cb:String = null;
private function connect(server:String,port:int):void
{
if(_socket == null || !_socket.connected)
{
_socket = new RemotingSocket(server,port);
_socket.addEventListener("newRemotingMsg",remotingSocketHandler);
_socket.addEventListener(Event.CONNECT, function(event:Event):void
{
if(connect_cb != null)
ExternalInterface.call(connect_cb);
});
_socket.addEventListener(Event.CLOSE, function(event:Event):void
{
if(disconnect_cb != null)
ExternalInterface.call(disconnect_cb);
});
_socket.addEventListener(IOErrorEvent.IO_ERROR, function(event:Event):void
{
if(ioerror_cb != null)
ExternalInterface.call(ioerror_cb);
});
_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(event:Event):void
{
if(securityerror_cb != null)
ExternalInterface.call(securityerror_cb);
});
_socket.addEventListener(ProgressEvent.SOCKET_DATA, function(event:Event):void
{
if(progressevent_cb != null)
ExternalInterface.call(progressevent_cb);
});
ExternalInterface.addCallback("write",write);
}
}
private function remotingSocketHandler(event:RemotingEvent):void
{
if(recieve_cb != null)
ExternalInterface.call(recieve_cb,event.message);
}
private function write(str:String):void
{
_socket.write(str);
}
private function setCallBack(str:String,fun:String):void
{
switch (str)
{
case "connect" : connect_cb = fun; break;
case "disconnect" : disconnect_cb = fun; break;
case "recieve" : recieve_cb = fun; break;
case "ioerror" : ioerror_cb = fun; break;
case "securityerror" : securityerror_cb = fun; break;
case "progressevent" : progressevent_cb = fun; break;
}
}
private function init():void
{
ExternalInterface.addCallback("setCallBack",setCallBack);
ExternalInterface.addCallback("connect",connect);
ExternalInterface.call("jssocket_init");
}
]]></mx:Script>
</mx:Application>