简体   繁体   English

AS3-LocalConnection似乎不起作用

[英]AS3 - LocalConnection Doesn't Seem to Work

I'm trying to set up communication between three swfs loaded into separate broswer windows (all opened by the initially opened page) using LocalConnection. 我正在尝试使用LocalConnection在加载到单独的浏览器窗口(均由初始打开的页面打开)中的三个swf之间建立通信。 I'm new to inter-swf communication, and am going by the tutorial found here . 我是瑞士法郎之间的交流新手,并且正在阅读此处的教程。

I'm running these from a xampp server for testing. 我正在从xampp服务器运行这些文件进行测试。

Essentially, I have 3 swfs. 本质上,我有3个瑞士法郎。 I have one 'master' swf, and two 'slave' swfs. 我有一个“主”瑞士法郎和两个“从”瑞士法郎。 When the master swf is launched, it opens both other swfs in new windows. 启动主SWF文件时,它将在新窗口中同时打开其他两个SWF文件。 When a button is clicked in the master, I'm trying to just update some text in one of the slaves. 当单击主服务器中的按钮时,我试图仅更新其中一个从属服务器中的一些文本。 More complicated stuff will happen later, I'm just trying to figure out LocalConnections first. 稍后会发生更复杂的事情,我只是想先弄清楚LocalConnections。

The code (on the timeline) in 'master.swf': “ master.swf”中的代码(在时间轴上):

import flash.net.URLRequest;
import flash.events.Event;

//Used as a flag to open each slave window, one of the first frame, one on the second frame, as using navigateToURL twice in a row doesn't seem to work.
var frameCount = 0;

//This is what I'm attempting to send over the LocalConnection.
var messageText = "it worked!";

var slave1url:URLRequest = new URLRequest("slave1.html");
var slave2url:URLRequest = new URLRequest("slave2.html");

//Creates a one-way connection to communicate with JUST slave1
var slave1OutboundConnection:LocalConnection = new LocalConnection();

this.addEventListener(Event.ENTER_FRAME, Update);

slave1Button.addEventListener(MouseEvent.CLICK, SendMessageToSlave1);
slave1Button.buttonMode = true;

//Send a message to slave1 via the slave1OutboundConnection
function SendMessageToSlave1(e:Event){  
    slave1OutboundConnection.send("masterToSlave1Connection", "UpdateTextFromCommunication", messageText);  
}

function Update(e:Event){

    //Open the slave1 page on the first frame, and the slave2 page on the second frame.
    if(frameCount == 0){
        navigateToURL(slave1url, "_blank");
        frameCount++;
    } else if (frameCount == 1){
        navigateToURL(slave2url, "_blank"); 
        frameCount++;
    }   

}

In slave1: 在slave1中:

var masterInboundConnection:LocalConnection = new LocalConnection();

masterInboundConnection.allowDomain("*");

masterInboundConnection.connect("masterToSlave1Connection");

function UpdateTextFromCommunication(receivedArguments){
   displayText.text = receivedArguments;
}

I've also got a little bit of custom javascript in each html page to set page size and position, but I don't think that should matter: 我在每个html页面中都有一些自定义javascript来设置页面大小和位置,但是我认为这并不重要:

<script>
        //Get the width of the user's monitor
        var resolutionWidth = window.screen.availWidth;

        //Get the height of the user's monitor (minus the height of the task bar)
        var resolutionHeight = window.screen.availHeight;   

        //First screen is 0, second is 1, third is 2, etc.
        var screenNumber = 2;

        //If this screen is not displayed on the primary monitor, add 40 pixels for the lack of the taskbar on other monitors
        if(screenNumber > 0){
            resolutionHeight += 40;
        }

        //Maximize the window
        this.resizeTo(resolutionWidth, resolutionHeight);

        //Move the window to the appropriate display
        this.moveTo(resolutionWidth * screenNumber, 0);         
    </script>

When I click the button that's supposed to trigger the communication, nothing appears to happen. 当我单击应该触发通信的按钮时,似乎什么也没有发生。

I'm running the html pages containing these swfs in IE11 on a localhost xampp server on Windows 7. Swfs made in CS5.5. 我正在Windows 7上的localhost xampp服务器上的IE11中运行包含这些swfs的html页面。

In each of the inbound connections, I was missing a single line of code: 在每个入站连接中,我都缺少一行代码:

masterInboundConnection.client = this;

Now everything is working as expected. 现在一切都按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM