[ as ] :
===================================================
import flash.external.ExternalInterface;
import flash.net.Socket;
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
var mySocket:Socket = new Socket();
mySocket.addEventListener(Event.CONNECT,connectHandler);
mySocket.addEventListener(Event.CLOSE,closeHandler);
mySocket.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
mySocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,seurityErrorHandler);
mySocket.addEventListener(ProgressEvent.SOCKET_DATA,socketDataHandler);
function connectHandler(e:Event):void{
ExternalInterface.call("flashEvent.connectHandler");
}
function closeHandler(e:Event):void{
ExternalInterface.call("flashEvent.closeHandler");
}
function ioErrorHandler(evt:IOErrorEvent):void{
ExternalInterface.call("flashEvent.ioErrorHandler");
}
function seurityErrorHandler(evt:SecurityErrorEvent):void{
ExternalInterface.call("flashEvent.seurityErrorHandler");
}
function socketDataHandler(evt:ProgressEvent):void{
var msg:String = "";
while(mySocket.bytesAvailable)
{
msg += mySocket.readMultiByte(mySocket.bytesAvailable,"utf-8");
}
ExternalInterface.call("flashEvent.socketDataHandler",msg);
}
function sendData(str:String):void{
mySocket.writeUTFBytes(str);
mySocket.flush();
}
function connectSocket(host:String,port:String):void{
mySocket.connect(host,int(port));
}
function closeSocket(str:String):void{
mySocket.close();
}
ExternalInterface.addCallback("sendDataFromAs", sendData);
ExternalInterface.addCallback("closeSocketFromAs", closeSocket);
ExternalInterface.addCallback("connectSocketFromAs", connectSocket);
ExternalInterface.call("flashEvent.asReady");
[ js ] :
==================================================================
var flashSocketNode = Y.Node.create('<embed src="flash/flash_socket.swf" width="1" height="1" allowScriptAccess="sameDomain" type="application/x-shockwave-flash">').appendTo(document.body),
flashSocketNode = Y.Node.getDOMNode(flashSocketNode),
asReady = false,
that = this;
this._flashSocket = {
isAsReady : function(){
return asReady;
},
connect : function(host,port){
flashSocketNode.connectSocketFromAs(host,port);
},
close : function(){
flashSocketNode.closeSocketFromAs();
},
send : function(data){
flashSocketNode.sendDataFromAs(data);
},
onopen : function(){},
onclose : function(){},
onmessage : function(){}
}
window.flashEvent = {
asReady : function(){
asReady = true;
},
connectHandler : function(){
that._flashSocket.onopen();
},
closeHandler : function(){
that._FlashSocket.onclose();
},
ioErrorHandler : function(){
new Y.Win().alert({"content":"flash io error"});
},
seurityErrorHandler : function(){
new Y.Win().alert({"content":"flash seurity error"});
},
socketDataHandler : function(data){
dataList = data.slice(0,-2).split("\r\n");
Y.each(dataList,function(_data){
that._flashSocket.onmessage({data:_data});
if(dataList.length>1){
new Y.Win().alert({content:_data,width:1000,height:500});
}
},that);
}
};
[ server ] :
============================================================
def __handshakeHandler(self,data):
response = ""
if "<policy-file-request" in data :
response = '''
<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" to-ports="*" />
</cross-domain-policy>\0'''.lstrip()
def __wrapResponse(self,str):
response = ""
if self.__getDraftType() == "common":
response = '%s\r\n' % str