动作3:如何获取最新的服务器(而不是客户端) [英] Actionscript 3: How do I get date from server (not client)

查看:149
本文介绍了动作3:如何获取最新的服务器(而不是客户端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的圣诞日历供我上学时,我注意到,该日期级是不是我虽然是。

I am working on a Christmas calendar for my school, when I noticed that, the Date-class is not what I though it was.

它得到的时候从客户端的CPU,这是不行的。

It gets the time from the client CPU, and that won't do.

所以,我一直在四处寻找在网络上,但没有出现过很好的答案(至少是我可以做的任何意义)。

So, I been looking around on the web, but there have been no good answers (at least that I could make any sense of).

所以在这里,它是:

我要如何在UTC +1的时间和日期(*),从服务器我的闪存程序托管,或者一些外部时间服务器?

How do i get the time and date (*) in UTC +1, from the server my flash-program are hosted, or some external time-server?

*的在UNIX的时间,或以某种方式否则ActionScript 3的可以理解

我寻找答案了一些PHP-脚本的建议,但我不知道如何使用(在那里有几个例子,但他们在AS2位置)。

My search for answers had suggestions for some php-scripts, but i don't understand how to use that (there where a few examples, but they where in AS2).

推荐答案

(例如,不使用它可以被篡改的Flash变量),这样做的一个更安全的方法,是创建一个非常简单的服务器页面(输出纯文本它输出的日期,而不是​​HTML),然后加载到闪存与装载机,并解析成一个原生日期对象。

A more secure way of doing this (eg not using flash vars which can be tampered with), would be to create a very simple server page (outputs plain text instead of html) that outputs the date, then load that into flash with a loader and parse it into a native Date object.

Flash端是这样的:

The flash side would look like this:

var loader:URLLoader = new URLLoader()
loader.load(new URLRequest("your server page"))
loader.addEventListener(Event.COMPLETE, onLoaded)
function onLoaded(evt:Event){
    var date:Date;
    try{
        date = new Date(Number(loader.data)); //need to cast the loader data as a number
    }catch(e:Error){
    }

    if(!date){
        //your output from the server isn't formatted in a way flash can convert it
        date = new Date(); //use local time
    }
}


之所以使用try / catch语句,是因为如果从服务器输出是不是有效日期,将尝试转换它的时候抛出一个错误。如果错误被抛出try块内的任何地方,它会退出在错误的行(跳过的code中的其余部分),然后运行的是在catch块。如果在try块中没有错误,code在catch块将永远不会运行。


The reason for using try/catch, is because if output from the server isn't a valid date it will throw an error when trying to convert it. If an error is thrown anywhere inside of a try block, it will exit at the line of the error (skip the rest of the code) and run what is in the catch block. If there is no error in the try block, the code in the catch block will never run.

使用PHP(虽然我没有在任何地方的PHP方便检查所以这可能需要有人来纠正,它已经10年了,我编程吧)

With PHP (though I don't have php anywhere handy to check so this may need to be corrected by someone, and it's been about 10 years since I've programmed with it)

<?php 
    header("Content-Type: text/plain");
    print time() * 1000;
?>

这篇关于动作3:如何获取最新的服务器(而不是客户端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆