AS3客户端 - C#服务器套接字连接失败,

时间:2012-02-10 20:20:04

标签: c# flash actionscript-3 client-server

嗨,我正在开发一个Flash应用程序。我是Flash in Action-script 3(CS5)的套接字。服务器是C#。当我作为投影仪(.exe)导出时,我的功能是非常好的工作和连接服务器与套接字。但是当我导出为.swf时,我的函数再次很好地工作,但socket.connected返回“false”。

我添加了crossdomain.xml,但仍然存在同样的问题。

你能帮帮我吗?

注意:我找到了这个页面,但我没有加载外部页面的任何数据:(。

这是我的代码

编辑:我没有发送到服务器但是当我运行时.swf flash将数据发送到服务器

“<策略的文件请求/>” 中

  

连接错误[SecurityErrorEvent type =“securityError”bubbles = false   cancelable = false eventPhase = 2 text =“错误#2048”]

但是root包含了crossdomain.xml,我添加了这个as3 kod,但问题仍未解决。

    Security.allowDomain("*");
    Security.loadPolicyFile("http://MY-IP-ADDRESS/crossdomain.xml");

- 最新代码

import flash.display.MovieClip;
import flash.display.Shape;
import flash.display.DisplayObject;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.display.BitmapData;
import flash.geom.ColorTransform;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.ByteArray;
import flash.net.FileReference;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.Socket;
import flash.display.BitmapData;
import flash.utils.setInterval;
import flash.system.* ;
import PNGEncoder;

public class Main extends MovieClip
{

    /* Variables */
    var aktarilan:BitmapData = new BitmapData(600,290);

    /* Pencil Tool shape, everything drawed with this tool and eraser is stored inside board.pencilDraw */

    var pencilDraw:Shape = new Shape();

    /* Text format */

    var textformat:TextFormat = new TextFormat();

    /* Colors */

    var colorsBmd:BitmapData;
    var pixelValue:uint;
    var activeColor:uint = 0x000000;

    /* Save dialog instance */

    var saveDialog:SaveDialog;

    /* Active var, to check wich tool is active */

    var active:String;

    private static var fl_socket:Socket;

    /* Shape size color */

    var ct:ColorTransform = new ColorTransform();

    private function onConnect(e:Event){
          if(fl_socket.connected){
            durum_bilgisi.text = "Connected to Server";
          }else{
            durum_bilgisi.text = "Connection Error";
          } 
      }

    public function Main():void     
    {  
    Security.allowDomain("*");
    Security.loadPolicyFile("http://MY-IP-ADDRESS/crossdomain.xml");
    fl_socket = new Socket();
    fl_socket.addEventListener(Event.CONNECT, onConnect);
    try{
      fl_socket.connect("MY-IP-ADDRESS", 1234);
       trace('Socket connection error');
        }
    catch(e:Error){
      trace('Socket connection error ' + e.message );
       durum_bilgisi.text = "Connection Error" + e.message;
    }
        textformat.font = "Quicksand Bold Regular";
        textformat.bold = true;
        textformat.size = 16;

        // Soket baglantisi


        convertToBMD();
        addListeners();

        /* Hide tools highlights */
        pencil.visible = false;
        hideTools(eraser, txt);

    }

    /* Pencil Tool */

    private function PencilTool(e:MouseEvent):void
    {
        /* Quit active tool */
        quitActiveTool();
        /* Set to Active */
        active = "Pencil";
        /* Listeners */
        board.addEventListener(MouseEvent.MOUSE_DOWN, startPencilTool);
        board.addEventListener(MouseEvent.MOUSE_UP, stopPencilTool);

        /* Highlight */
        highlightTool(pencil);
        hideTools(eraser, txt);

        ct.color = activeColor;
        shapeSize.transform.colorTransform = ct;
    }

    private function startPencilTool(e:MouseEvent):void
    {
        pencilDraw = new Shape();

        board.addChild(pencilDraw);

        pencilDraw.graphics.moveTo(mouseX, mouseY);



        pencilDraw.graphics.lineStyle(shapeSize.width, activeColor, 1.0,true,"normal","round");


        pencilDraw.graphics.lineTo(mouseX+1, mouseY+1);
        board.addEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool);
    }

    private function drawPencilTool(e:MouseEvent):void
    {

        pencilDraw.graphics.lineTo(mouseX, mouseY);
    }

    private function stopPencilTool(e:MouseEvent):void
    {
        board.removeEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool);
    }

    /* Eraser Tool */

    private function EraserTool(e:MouseEvent):void
    {
        var bmd:BitmapData = new BitmapData(600, 290);
        bmd.draw(board);
        var ba:ByteArray = PNGEncoder.encode(bmd);

        /* Quit active tool */

        quitActiveTool();

        /* Set to Active */

        active = "Eraser";

        /* Listeners */

        board.addEventListener(MouseEvent.MOUSE_DOWN, startEraserTool);
        board.addEventListener(MouseEvent.MOUSE_UP, stopEraserTool);

        /* Highlight */

        highlightTool(eraser);
        hideTools(pencil, txt);

        ct.color = 0x000000;
        shapeSize.transform.colorTransform = ct;
    }

    private function startEraserTool(e:MouseEvent):void
    {
        pencilDraw = new Shape();
        board.addChild(pencilDraw);

        pencilDraw.graphics.moveTo(mouseX, mouseY);
        pencilDraw.graphics.lineStyle(shapeSize.width, 0xFFFFFF);

        board.addEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);
    }

    private function drawEraserTool(e:MouseEvent):void
    {
        pencilDraw.graphics.lineTo(mouseX, mouseY);
    }

    function stopEraserTool(e:MouseEvent):void
    {
        board.removeEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);

    }

    /* Text Tool */

    private function TextTool(e:MouseEvent):void
    {
        /* Quit active tool */

        quitActiveTool();

        /* Set to Active */

        active = "Text";

        /* Listener */

        board.addEventListener(MouseEvent.MOUSE_UP, writeText);

        /* Highlight */

        highlightTool(txt);
        hideTools(pencil, eraser);
    }

    private function writeText(e:MouseEvent):void
    {
        var textfield = new TextField();

        textfield.type = TextFieldType.INPUT;
        textfield.autoSize = TextFieldAutoSize.LEFT;
        textfield.selectable = false;
        textfield.defaultTextFormat = textformat;
        textfield.textColor = activeColor;
        textfield.x = mouseX;
        textfield.y = mouseY;
        stage.focus = textfield;

        board.addChild(textfield);
    }


    /* Clear Tool */

    private function clearBoard(e:MouseEvent):void
    {
        /* Create a blank rectangle on top of everything but board */

        var blank:Shape = new Shape();

        blank.graphics.beginFill(0xFFFFFF);
        blank.graphics.drawRect(0, 0, board.width, board.height);
        blank.graphics.endFill();

        board.addChild(blank);
    }

    /* Default colors function */

    private function convertToBMD():void
    {
        colorsBmd = new BitmapData(colors.width,colors.height);
        colorsBmd.draw(colors);
    }

    private function chooseColor(e:MouseEvent):void
    {
        pixelValue = colorsBmd.getPixel(colors.mouseX,colors.mouseY);
        activeColor = pixelValue;//uint can be RGB!

        ct.color = activeColor;
        shapeSize.transform.colorTransform = ct;
    }

    /* Quit active function */

    private function quitActiveTool():void
    {
        switch (active)
        {
            case "Pencil" :
                board.removeEventListener(MouseEvent.MOUSE_DOWN, startPencilTool);
                board.removeEventListener(MouseEvent.MOUSE_UP, stopPencilTool);
            case "Eraser" :
                board.removeEventListener(MouseEvent.MOUSE_DOWN, startEraserTool);
                board.removeEventListener(MouseEvent.MOUSE_UP, stopEraserTool);
            case "Text" :
                board.removeEventListener(MouseEvent.MOUSE_UP, writeText);
            default :
        }
    }

    /* Highlight active Tool */

    private function highlightTool(tool:DisplayObject):void
    {
        tool.visible=true;
    }

    private function hideTools(tool1:DisplayObject, tool2:DisplayObject):void
    {
        tool1.visible=false;
        tool2.visible=false;
    }

    /* Change shape size */

    private function changeShapeSize(e:MouseEvent):void
    {
        if (shapeSize.width >= 50)
        {
            shapeSize.width = 1;
            shapeSize.height = 1;

            /* TextFormat */

            textformat.size = 16;
        }
        else
        {
            shapeSize.width += 5;
            shapeSize.height=shapeSize.width;

            /* TextFormat */

            textformat.size+=5;
        }
    }

    private function addListeners():void
    {
        pencilTool.addEventListener(MouseEvent.MOUSE_UP, PencilTool);
        eraserTool.addEventListener(MouseEvent.MOUSE_UP, EraserTool);
        textTool.addEventListener(MouseEvent.MOUSE_UP, TextTool);
        clearTool.addEventListener(MouseEvent.MOUSE_UP, clearBoard);
        colors.addEventListener(MouseEvent.MOUSE_UP, chooseColor);
        sizePanel.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);
        shapeSize.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);
    }


}

1 个答案:

答案 0 :(得分:1)

您发布的代码无法运行。

//  remove the following lines
fl_socket = new Socket();           
fl_socket.connect("MY-IP-ADDRESS", 1234);
if(fl_socket.connected){durum_bilgisi.text = "Connected to Server";} else {durum_bilgisi.text = "Connection Error";}


// in the main function add this
fl_socket = new Socket();
fl_socket.addEventListener(Event.CONNECT, onConnect);
try{
  fl_socket.connect("MY-IP-ADDRESS", 1234);
}catch(e:Error){
  trace('Socket connection error ' + e.message )
}



// and lastly add this function
private function onConnect(e:Event){
  if(fl_socket.connected){
    durum_bilgisi.text = "Connected to Server";
  }else{
    durum_bilgisi.text = "Connection Error";
  }