js通过as完成socket通信

[ 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



转载于:https://www.cnblogs.com/cly84920/archive/2012/01/12/4426556.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值