2012-02-10 313 views
0

您好我正在處理Flash應用程序。我是ActionScript 3(CS5)中來自Flash的套接字。服務器是C#。當我作爲投影儀導出(.exe)時,我的功能非常好,工作也很好,並將服務器與套接字連接起來。但是當我作爲.swf導出時,我的函數很好地工作,但socket.connected返回「false」。AS3客戶端 - C#服務器套接字連接失敗,

我添加了crossdomain.xml,但仍然是同樣的問題。

你能幫我嗎?

注:我找到了這個網頁,但我不加載任何數據的外部頁面:(

這裏是我的代碼

編輯:我沒有發送到服務器,但是當我運行瑞士法郎閃光燈發出數據到服務器

「<策略的文件請求/ >」

連接錯誤[SecurityErrorEvent類型= 「的securityError」 氣泡=假或取消=假的EventPhase = 2文本= 「錯誤#2048」]

但根包括crossdomain.xml,我添加了這個爲kod,但仍然沒有解決問題。

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

--lastest代碼

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); 
    } 


} 
+0

你在所有得到任何錯誤...如果是的話,請張貼在這裏的錯誤.. – MethodMan 2012-02-10 20:25:46

+0

沒有錯誤僅此行 如果(fl_socket.connected){durum_bilgisi.text =「連接到服務器」 ;} else {durum_bilgisi.text =「Connection Error」;} is return false – 2012-02-10 20:32:08

+0

該代碼如何工作它不在函數中 – 2012-02-10 22:28:07

回答

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"; 
    } 
+0

@感謝您的糾正我粘貼舊代碼,但仍然是相同的問題 – 2012-02-10 22:56:29

+0

然後解決你的問題,這是很難明白你在問什麼。 – 2012-02-11 00:32:01

+0

您還應該在其代碼中添加IOErrorEvent.IO_ERROR和SecurityErrorEvent.SECURITY_ERROR偵聽器,以確保他確實連接。 – turbosqel 2012-02-11 08:07:16