zoukankan      html  css  js  c++  java
  • 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,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



  • 相关阅读:
    Cocos2d-x学习笔记(二十五)之 加速度传感器事件
    Cocos2d-x学习笔记(二十四)之 触屏事件
    Cocos2d-x学习笔记(二十三)之 动画
    Cocos2d-x学习笔记(二十二)之 网格动作
    Cocos2d-x学习笔记(二十一)之 动作管理类CCActionManager
    Cocos2d-x学习笔记(二十)之 组合动作
    Cocos2d-x学习笔记(十九)之 缓冲动作
    Cocos2d-x学习笔记(十八)之 基本动作
    Cocos2d-x学习笔记(十七)之 动作类CCAction
    Cocos2d-x学习笔记(十六)之 定时器(时间调度)
  • 原文地址:https://www.cnblogs.com/cly84920/p/4426556.html
Copyright © 2011-2022 走看看