最精确硬盘分区的算法

Windows 无评论 »

最精确硬盘分区的算法如下(来自网络搜索):

硬盘一般有255磁头,63扇区,故每柱面大小为:
512byte x 255 x 63=8225280bytes =7.84423828125 MB
如果要分40GB,那么要40×1024MB=40960MB
需要柱面数为40960÷7.84423828125=5221.66
取整数既为5222个柱面
应分M数为5222×7.84423828125=40962.6123046875MB
不管小数点后面几位都进1,也就是40963MB,windows就认为是40GB了。
这个方法NTFS和FAT32通用。

下面附10GB到200GB整10GB递增的精确计算结果:
10GB = 10245MB
20GB = 20482MB
30GB = 30726MB
40GB = 40963MB
50GB = 51208MB
60GB = 61444MB
70GB = 71681MB
80GB = 81926MB
90GB = 92162MB
100GB = 102407MB
110GB = 112644MB
120GB = 122888MB
130GB = 133125MB
140GB = 143362MB
150GB = 153606MB
160GB = 163843MB
170GB = 174088MB
180GB = 184324MB
190GB = 194561MB

此精确分区结果,在管理工具-磁盘管理界面,和Windows资源管理器里面显示的是整数,10G就是10.00GB,20G就是20.00GB,40G就是40.00GB。

Tags »

Windwos 下配置多个Apache服务站点

Apache 无评论 »

更改第一个站点的根目录:在文件Apache2.2\conf\httpd.conf中查找 DocumentRoot 属性,将后面的路径改为你的主站点的路径,如:D:\www\web1

为第二个Apache服务建立配置文件:复制并重命名httpd.conf为web2.conf(举个例子而已,也可以叫my.conf等等), 修改web2.conf中的Listen 8080(原来为80)、ServerName localhost:8080(原来为80)、DocumentRoot “D:/www/web2″ (原来为web1)
添加第二个Apache服务:Apache安装目录的bin子目录下,使用如下命令将Apache安装为Windows NT服务:httpd.exe -k install -n “服务名” -f “d:\apache2.2\conf\web2.conf”
其他的命令:
将Apache安装为Windows NT服务:
httpd -k install
指定服务的名称,当你在同一机器上安装多个Apache服务时,你必须为它们指定不同的名字。
httpd -k install -n “服务名”
为不同名称的服务使用不同的配置文件,则安装时需要指定配置文件:
httpd -k install -n “服务名” -f “c:\files\my.conf” 如果你使用的是第一个命令,也就是除 -k install 外没有其它命令行参数,那么被安装的服务名称将是:Apache2 ,配置文件将使用conf\httpd.conf 。
移除一个Apache服务:
httpd -k uninstall
使用下述命令移除特定名称的Apache服务:
httpd -k uninstall -n “服务名”
通常,启动、重启、关闭Apache服务的方法是使用Apache Service Monitor工具,另外也可以使用控制台命令:NET START Apache2 和 NET STOP Apache2 或者通过Windows服务控制面板。在启动Apache服务之前,你应当使用下面的命令检查一下配置文件的正确性:
httpd -n “服务名” -t
你可以通过命令行开关来控制Apache服务。要启动一个已经安装的Apache服务,可以使用:
httpd -k start
要停止一个已经安装的Apache服务,可以使用:
httpd -k stop

httpd -k shutdown
要重启一个运行中的Apache服务,强制它重新读取配置文件,可以使用:
httpd -k restart

Tags »

Flash10使用RTMFP 开发点对点应用

FMS/FCS, Flex/Flash 无评论 »

通过Stratus 服务器在Flash Player中使用RTMFP 开发 点对点应用Adobe Flash Player 10 和 Adobe AIR 1.5 引入了一个新的通讯协议,Real-Time Media Flow Protocol (RTMFP),其低延迟,端到端的对等功能,安全性和可扩展性使它特别适合开发实时协作应用,不仅提供卓越的用户体验,而且运营商降低成本。

早前的Flash Player 版本使用Real-Time Messaging Protocol (RTMP) 需要Adobe Flash Media Server (FMS)提供合作应用(例如 Adobe Acrobat Connect Pro)或者音频视频流。RTMP是streaming media, shared objects, 和 remoting连接的卓越选择,它满足实时性要求的交互式音频和视频通信的能力有限。

为了能够使用RTMFP,Flash Player 客户端必须连接到一个支持RTMFP协议的服务器,例如Adobe Stratus beta 服务器或者是新版本的FMS。Stratus 是一台用于Flash Player 客户端间通信的主机。不同于FMS, Stratus 不支持视频转播,shared objects,脚本,等。因此,使用Stratus,只可以开发Flash Player的客户端直接相互交流的应用程序。

阅读全文 »

Tags »||

jodconverter

Java, Red5 无评论 »

官方網站:
http://www.artofsolving.com/opensource/jodconverter

下載地點:
http://www.artofsolving.com/opensource/jodconverter
http://zh.openoffice.org/new/zh_tw/downloads.html

目前版本: JODConverter v2.2.1, OpenOffice v3.0.0

使用需求: JDK1.4以上, 安裝OpenOffice v2.0.3以上

基本簡介:

JODConverter主要的功能是用來做各種檔案的轉換. 目前測試過, Word,Excel,PowerPoint轉PDF都是沒問題的.

因為JODConverter是透過OpenOffice來做轉換, 所以使用前需要先安裝OpenOffice, 並且將OpenOffice的Service啟動, 才可以使用.

使用教學:
Step1: 安裝OpenOffice
Step2: 啟動OpenOffice Service

1 cd C:\Program Files\OpenOffice.org 3\program
2 soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard

Step3:將JODConverter的Jar檔放進專案中的Library, 請檢查你的專案是否包含以下的Jar檔:

jodconverter-2.2.1.jar
jurt-2.3.0.jar
xstream-1.2.2.jar
ridl-2.3.0.jar
commons-io-1.3.1.jar
juh-2.3.0.jar
slf4j-api-1.4.3.jar
unoil-2.3.0.jar
slf4j-jdk14-1.4.3.jar

Step4: 準備一個word檔放在c:/document.doc
Step5: 執行以下程式

import java.io.File;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class JodDemo {
public static void main(String[] args) throws Exception{
File inputFile = new File(”c:/document.doc”);
File outputFile = new File(”c:/document.pdf”);
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
// close the connection
connection.disconnect();
}
}

程式說明:
程式的部份相當簡潔, 特別要注意的地方是第12行連線的port必須與你啟動OpenOffice的Port相同,
另外JODConverter預設是用副檔名作文件種類的判斷, 所以副檔名必須要正確才行.
如果副檔名比較特別的話, 就必須在convert()的時候強制指定Document Type.

心得:
JODConverter使用起來相當方便, 官網也提供War檔讓JODConverter變成Web Service提供給不同的語言來呼叫.
特別要注意的是, OpenOffice Service並不是ThreadSafe的, 多個Web AP在使用的時候必須要注意.

參考資料:
http://www.artofsolving.com/opensource/jodconverter

那我也來補充一些好了
之前也在試這個檔案轉換的程式
程式最好加上 try-catch
因為之前發現有些檔案 format 不能轉,發生 Exception 後,connection 不會自動切斷,程式會 hand 住
所以改成如下方式:

1234567891011121314
public void convert(String input, String output){
File inputFile = new File(input);
File outputFile = new File(output);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
} catch(Exception e) {
e.printStackTrace();
} finally {
try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
}
}

再來,明明就是 open office 的檔案,卻生不能轉換的問題。例如:*.STW, *.SXD, *.ODF 等,後來才知道可以自行指定來源檔和輸出檔的 mime-type,程式如下:

1234567891011121314151617
public void convertSTW(String input, String output){
DocumentFormat stw = new DocumentFormat(”OpenOffice.org 1.0 Template”, DocumentFamily.TEXT, “application/vnd.sun.xml.writer”, “stw”);
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
DocumentFormat pdf = formatReg.getFormatByFileExtension(”pdf”);
File inputFile = new File(input);
File outputFile = new File(output);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, stw, outputFile, pdf);
} catch(Exception e) {
e.printStackTrace();
} finally {
try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
}
}

上面的程式是轉換 STW 到 PDF,如果是 SXD / ODF 則只需要變更 DocumentFormat 的內容即可。

123
DocumentFormat sxd = new DocumentFormat(”OpenOffice.org 1.0 Drawing”, DocumentFamily.DRAWING, “application/vnd.sun.xml.sraw”, “sxd”);

DocumentFormat odf = new DocumentFormat(”OpenDocument Math”, DocumentFamily.TEXT, “application/vnd.oasis.opendocument.formula”, “odf”);

所有 default support 的 DocumentFormat 都在 com.artofsolving.jodconverter.DefaultDocumentFormatRegistry 裡,但並非所有 open office 支援的 file format 都有,所以要像上面的方法自行去定義 DocumentFormat。

在此獻給所有需要作 File Convert 的人試試。
免錢的,最好用。還有 source code 可以自己改。

Tags »||||

Red5 0.8 Final 开发环境配置(Eclipse 3.4 EE SR2, Tomcat 6.0.20, Svnclipse 1.4.x, IvyDe 2.0.0 b1)

Eclipse, Java, Red5, Tomcat 无评论 »

1) 下载 eclipse-jee-ganymede-SR2-win32.zip 安装文件, 解压在 X:\Java 下。

2) 下载 apache-tomcat-6.0.20.zip 安装文件, 解压在 X:\Java 下。

3) 在 X:\Java\eclipse\dropins\ 下新建 svnclipse 目录 。

4) 在 X:\Java 下新建 workspace 目录。

5) 下载 Subclipse 安装文件 site-1.4.8.zip, 解压在 X:\Java\eclipse\dropins\svnclipse\ 下。
同时删svnclipse根目录下其他文件, 只保留features和plugins两个目录下的内容。

6) 启动 eclipse.exe, 选择缺省工作空间 X:\Java\workspace 目录。

7) 切换 Eclipse 欢迎界面为工作界面。

8) 在工作界面最底下找到 Servers 选卡, 点击进入, 右击 New –> Server, 选择 Apache –> Tomcat v6.0 Server

9) 在 General Information 中修改运行参数, 点击 Open launch configuration 连接,  选择在 (x)=Arguments 卡, 在 VM arguments 中添加参数 -Dorg.apache.el.parser.COERCE_TO_ZERO=false, 作用是关闭 Tomcat 在转换字符串时的特点, 如果用户输入参数为空时, Tomcat 强行转换int型为0, string型为” “, 也就是用户输入参数永远不可能为null, 若java程序中依赖null特性, 就应该设置此参数,关闭Tomcat核心字符转换为0的特性。

10) 在 Server Locations 中指定 Tomcat 位置, 请选择 Use Tomcat Install (takes control of Tomcat installation), 让 Eclipse 全面控制 Tomcat 配置。

11) 在 Server Options 中选择 Publish module contexts separate XML files. 发布模块配置文件。

12) 至此, 可以通过 Servers 选卡的绿色三角按钮启动 Tomcat 和红色四方块按钮停止 Tomcat。启动后, 通过 http://localhost:8080 验证, 进入 Manager, 需要配置 X:\Java\apache-tomcat-6.0.20\conf\tomcat-users.xml
首先添加角色  <role rolename=”manager”/>   <role rolename=”admin”/>
最后添加用户 <user username=”admin” password=”tomcat-6.0.20″ roles=”manager, admin”/>
至此, 就可进入 Tomcat 的 Manager 管理界面。

13) 打开菜单 Show View –>Other … –> SVN –> SVN资源库, 右键点击 新建–>资源库位置–>URL, 输入资源库地址
http://red5.googlecode.com/svn/java/server/trunk 确认, 右键点击此资源条目 检出为… 选择 做为工作空间中的项目检出
确认, finish, 等待检出为工程.

14) 检出工程后, 可以看到test和src有错误, 是因为没有依赖库, 此刻需要安装Apache IvyDe Plugin, 打开 Help –> Software Update… –> Available Software –> Add Site –> http://www.apache.org/dist/ant/ivyde/updatesite –> Install, 安装等待, 安装完成后, 弹出的对话框, 选择 Apply, 然后, 重新启动 Eclipse, 重启Eclipse后, 注意此时要连接到网络上, IvyDe 自动根据项目【red5_server】中的ivy.xml和ivysettings.xml检查依赖关系, 下载相应的依赖库文件并自动编译, 等待完成后, 可以发现项目成功编译。

15) 指定运行主类 org.red5.server.Bootstrap, 并设置VM运行参数-Dlogback.ContextSelector=org.red5.logging.LoggingContextSelector -Dcatalina.useNaming=true -Djava.security.debug=failure -Dpython.home=lib 参数是从 red5.bat 中抄的。

16) 右击检出项目, 选择 Build Path –> Configure Build Path … 确认, 在 Librarie 选项卡中点击 Add Class Folder, 选择确认项目中的 src\conf 目录, 至此设置完毕。

17) Run/Debug 都可以了! 应用都在 webapps 中, 访问 http://localhost:5080 安装 http://localhost:5080/installer 演示 http://localhost:5080/demos

Tags »|||

Flex + AMFPHP: Mapping Value Objects

Flex/Flash, Flex3, PHP 无评论 »

Update: Added code samples to end of post.

One of the issues I see a lot of with people using AMFPHP is how to properly map VO classes between Flex and php. It used to stress me out too, until I realized how easy it really is.

First thing: Don’t worry about the package paths in the RemoteClass metadata tag and $_explicitType variables in your php classes. You don’t need them.

Second thing: Just use the name of the class you are mapping in for the values RemoteClass(”alias=”") and $_explicitType=”". For example, if you wanted to map a value object named ‘Book’ between flex and php, just use:

1
[RemoteClass(alias="Book")]
1
var $_explicitType="Book";

Like i already mentioned, you don’t need the package names. The reasons behind this are:

  • When sending from php to Flex, Flex will match the $_explicitType variable to the Remote class alias. It’s just an id to match…it doesn’t even have to be the class name…it can be jibberrish, as long as both strings match. (This won’t work the other way though…see next item)
  • When sending from Flex to php, AMFPHP will try to load the class with the name specified by the RemoteObject alias value. It simply looks in a specified directory to try and load the class…if it can’t find it it’s parsed as an array.

So, how does AMFPHP know where to look for the vo classes? Easy…you tell it exactly which directory to look in by setting the $voPath variable in globals.php. I usually do something along the lines of :

1
$voPath = $_SERVER["DOCUMENT_ROOT"]."/path/to/vo/directory";

Here’s a more flushed out code sample:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.website.vos
{
	[RemoteClass(alias="Book")]
	[Bindable]
	public class Book
	{
		public var title:String;
		public var author:String;
		public function Book()
		{
		}
	}
}
1
2
3
4
5
6
<?php
class Book {
	var $_explicitType="Book";
	var $title;
	var $author;
}

Say for example the Book.php class file is sitting on the server at webroot/php/classes/vos/Book.php. In order to tell AMFPHP where to look for the Book.php file, you open up globals.php in your AMFPHP installation, and set the $voPath variable to point to the ‘vos’ folder:

1
$voPath = $_SERVER["DOCUMENT_ROOT"]."/php/classes/vos";

Now when you send a Book object from Flex to php, AMFPHP will look in the $voPath (which we just set to the ‘vos’ folder) for the Book.php file.

Note that the $voPath variable is the starting point of where AMFPHP will start to look. If you specify a full package path for the RemoteClass alias, such as:

1
[RemoteClass(alias="com.website.vos.Book")]

then all the dots in the string are converted to slashes, and the full path is used. To continue with the example above, we have already set $voPath to point to the ‘vos’ folder, and the RemoteClass alias has been set to “com.website.vos.Book”. AMFPHP converts the alias to “com/website/vos/Book” and will attempt to locate the file $voPath/com/website/vos/Book.php. In addition, if you’re mapping this object from php back top Flex, remember the $_explicitType variable still must match the string for the RemoteClass alias, so you’d need to set that to:

1
var $_explicitType="com.website.vos.Book";
Tags »|||

FMS 带宽计算

FMS/FCS, Red5, 视频会议 无评论 »

在开始一个使用 FLASH MEDIA SERVER的项目开始之前,最好能够对你项目使用FLASH MEDIA SERVER 3的带宽需求进行计算.这样对你的项目最终的实现效果,会有一个稳定的结果.

去评估您的 Flash Media Server 应用所使用的带宽, 您需要了解如下内容:

  • 您的应用并发用户数量的最大负荷数
  • 数据传输速度,您预计您的用户连接速度( 56 Kbps 拨号上网, cable/DSL, 或者是居域网的使用比例)
  • 您的应用将要支持视频/音频操作的用户比率
  • 服务器硬件的局限性以及互联网状况
  • 您应用中视频和音频格式的频率

首先区分您应用的类型;一种为一对多(例如视频点播),另一种是多对多(例如视频会议),

例子一(一对多):

Flash Media Server仅提供一个视频流对多个用户,即视频点播项目

onetomany

Figure 1. One-to-many 视频点播系统

带宽计算公式

  • 计算服务器端带宽需求 (BWs):
    BWs = N × S
    N = 并发用户数量 //number of simultaneous users (subscribers)
    S = 视频内容平均编码比特率 //average bitrate of encoded A/V content
  • 计算客户端带宽需求(BWc):
    BWc = S
    S = 视频内容平均编码比特率 //average bitrate of encoded A/V content

举例:

视频编码是500 Kbps,有1000人进行观看那么服务总体带宽需求是

500 Mbps = 1000 × 500 Kbps

如果您的服务器支持600 Mbps吞吐量的硬件配置能力,您只需要一台服务和许可就可以.

当然如果您需要更多的用户访问量,比如10,000人并发需求,那么下面的计算式:

5000 Mbps = 10,000 × 500 Kbps

并发服务器硬件配置能力是600 Mbps,您需要:

8.3 = 5000 Mbps ÷ 600 Mbps

调整多比率计算方程式

例如,前面的例子有一半人使用350 Kbps DSL modem,另一半人使用的是3 Mbps cable modem.当观众收看视频编码为500 Kbps的时候,你想要分割视频为150KBPS给DSL modem用户.

325 Mbps = (500 Kbps × 500) + (150 Kbps × 500)

这样带宽被降到了325MBPS.

实例2:多对多

例如视频会议,需要多个人同时看到对方.

manytomany

Figure 2. 多对多视频会议

例如,如果有四个人在一个房间中,第一个人可以发送一个视频和接受三个其他的视频流,总共四个视频流.同样的第二,三,四个人也会消耗四个视频流.因此Flash Media Server视频流的总共数量是16每四个人.

带宽计算

  • 计算服务端带宽 (BWs):
    BWs = (P × N) × S
    P = 发布者的数量
    N = 订阅者的数量
    S = 视频编码的平均比特率
  • 计算客户端带宽 (BWc):
    BWc = P × S
    P = 发布者的数量
    S = 视频编码的平均比特率

实例计算

为四个人使用100 Kbps的视频流为一个会议室服务端带宽需要:

4.8 Mbps = (4 × 4) × 300 Kbps

为四个人使用100 Kbps的视频流为一个会议室客户端带宽需要:

900 Kbps = 3 × 300 Kbps 下载流

300 Kbps = 1 × 300 Kbps 上传流

因此每个视频会议房间将会需要一个4.8 Mbps的带宽.一台吞吐量为600 Mbps的服务器,你可以支持600 Mbps ÷ 4.8 Mbps = 125 房间.

Tags »||

Tomcat服务器处理字符问题

Java 无评论 »

Tomcat 版本 6.0.18 中, JSP 页面输入框,不填值,提交到后台接收不为null,G查了一下是Tomcat服务器处理字符问题。
缺省时: int型不填值,提交到后台为0,string型提交后台为””,解决这个问题,只需在Tomcat服务器启动参数里加上
-Dorg.apache.el.parser.COERCE_TO_ZERO=fals
参数, 告诉Tomcat不要转换即可。

Tags »

eclipse 3.4 ant 问题的解决办法

Eclipse, Java 无评论 »

问题描述:当console输出有中文时(比如用JUint会输出 信息 2字),会运行到输出汉字的前一步就停止。

解决方法:

第一种
在 ant 设置的 common 选项卡 中设置 Console Encoding 为GB18030(或者GBK)编码即可

当ant的common选项卡里的 Console Encoding 没有GB18030(或者GBK)时,先在 eclipse -> windows -> Preference -> General -> Workspace 选项的 text file encoding 改为GB18030(或者GBK)应用一下,再改回UTF-8

第二种
在 ant 设置的 main 选项卡 里的Arguments里输入 -Duser.language=en

第三种
在build.xml文件的javac标签里添加
<javac …>

<compilerarg value=”-J-Duser.language=en”/>
</javac>

第四种
在build.xml文件的xml声明里显式使用GB18030(或者GBK)编码即可。
<?xml version=”1.0″ encoding=”GB18030″ ?>

最后,如果采用命令行来运行ant,则什么都不用改。

Tags »|

Eclipse 3.4使用dropins的插件安装方式(转)

Eclipse 无评论 »

Eclipse 3.4插件安装方式

以前安装Eclipse插件无非两种方式, 直接copy插件到features/plugins目录或者在links目录下创建链接文件. 刚刚发布的Eclipse 3.4又推出另一种新的安装途径, 更加灵活。Eclipse 3.4下有个dropins目录, 只要把插件放到该目录下就可以加载, 有几种格式可以选择。

1. 最简单的,直接将jar包放到dropins目录:

1
2
3
    eclipse/
           dropins/
                com.essiembre.eclipse.i18n.resourcebundle_0.7.7

2. 传统格式,统一放到一个eclipse目录下:

1
2
3
4
5
      eclipse/
           dropins/
                eclipse/
                     features/
                     plugins/

3. 按照插件名称区分:

1
2
3
4
5
6
7
8
    eclipse/
          dropins/
               resourcebundleeditor/
                    features/
                    plugins/
                m2eclipse/
                    features/
                    plugins/

4. 类似links方式添加链接:

1
2
3
    eclipse/
          dropins/
               xfire.link


如果只安装一个Eclipse的话建议采用第三种,否则采用第四种。

如果发现安装了没有效果,可以删除eclipse主目录下的\configuration\org.eclipse.update后,再执行eclipse -clean试试。

Tags »|

Flex中捕获浏览器的前进、后退、刷新、关闭事件

AS3, Flex3, Javascript 无评论 »

如果不希望用户点击浏览器的前进、后退、刷新、关闭等误操作,可以捕获这些事件,实际上是用js捕获window.onbeforeunload而已,如果你不希望通过修改html来达到这个效果,可以参考我的另一篇从flex-ifram项目中看到的,在as中写js函数中的代码,可以做到不需要修改flex编译成的html文件即可达到效果。而你只需要在你的mxml代码中加入如下内容即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import flash.external.ExternalInterface;
private static var FUNCTION_USEREXIT:String = 
	"document.insertScript = function () " +
	"{ " +
		"window.onbeforeunload = function() " +
		"{ " +
			"var flexObj = MyTest2.checkExit(); " +//MyTest2是swf在html中object的id
			"if(flexObj != \"\") " +
			"{ " +
				"return flexObj; " +
			"}else{ " +
				"return; " +
			"} " +
		"} " +
	"} ";

然后在Application的creationComplete方法中添加如下代码:

1
2
3
4
5
6
ExternalInterface.call(FUNCTION_USEREXIT);
ExternalInterface.addCallback("checkExit",checkExit);
public function checkExit():String {
      var userExitStr:String = "你如果现在离开,则您的所有信息将失效!";
      return userExitStr;
}

这样就可以在不修改html的情况下达到你所希望的效果了。延展这个题目,我们所有和js打交道的代码其实都可以通过这个方式来将需要的js函数写入flex的源码中。

Tags »|

Zend AMF Server入门教程

AS3, PHP, Remoting 无评论 »

Zend_Amf是Zend Framework 1.7+的一部分。它提供了一种PHP头端对FlashPlayer和AIR的通信支持。Zend_Amf_Server采用的是AMF3格式。 Adobe已经确定Zend_AMF为其官方合作伙伴,支持性应该可以得到一定的保证。

具体请参见:http://framework.zend.com/manual/en/zend.amf.html

下载请到:http://framework.zend.com/download/latest

当我们下载完毕后,取出压缩包中的”library”文件夹,将其放到web目录下。这时,需要将这个目录配置到PHP的 ”include_path”路径下,有两种方式:一种是直接修改”php.ini”的”include_path”,将library的绝对路径添加到 上面;另一种是在php脚本中,使用ini_set()方法将其包含进去。第一种方法比较简便,但是如果没有环境配置权限的话,恐难执行,比如虚拟主机的 用户,这时就可以采取第二种方式了。

先给出大家一个最简单实例文件:server.php

1
2
3
4
5
6
7
8
9
10
<?PHP
ini_set("include_path",ini_get("include_path") . ";library");
require_once "Zend/Amf/Server.php";
require_once "CustomClass.php";
 
$server=new Zend_Amf_Server();
$server->setClass("CustomClass");
 
echo($server->handle());
?>

环境ok以后,我们首先需要包含进连接所必须的server文件”Zend/Amf/Server.php”。然后添加入我们自己所编写的类,例如 CustomClass。再来new 一个 Zend_Amf_Server对象,setClass到CustomClass,最后echo($server->handle())。很多地 方,包括官方文档,最后的时候直接用的是$server->handle(),但是我在使用的过程中发现这样是不行的,需要echo出来才行。

一个简单的发送和接收string类,可以这样:

1
2
3
4
5
6
7
8
9
<?php
class CustomClass
{
    public function customFunction ($msg)
    { 
        return "here returns ".$msg;
    }
}
?>

当flash调用这个类时,脚本会将接收到的string前加上”here returns”,以同样的string格式返回给flash。

Flash端的Actionscrpt脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var  nc:NetConnection=new NetConnection();
nc.connect("http://localhost/server.php");
var  res:Responder=new Responder(onResult,onError);
var  sendMsg:String = "Hi,man!"
nc.call("CustomClass.customFunction  ",res, sendMsg);
 
function  onResult(e:Object):void
{
    trace("Result: "+e);
}
 
function  onError(e:Object):void
{
    trace("Error: "+e);
}

Actionscrpt的主要工作,第一步是建立一个NetConnection对象到服务器上的server.php,然后创建一个 Responder对象来接收返回的信息内容和错误信息。再通过NetConnection.call将请求内容发送出去。实例中即是呼叫 CustomClass中的customFunction,并将sendMsg信息前加上”here returns”传给它。

本例中,我们trace出:

1
Result: here  returns Hi,man!

关于Zend Framework更为详细的配置,请参阅:Zend Framework QuickStart

文中所用到的程序源文件:zend_amf.rar

Tags »|

Flex 4 中的命名空间(fx、mx、s)

Flex/Flash, Flex4 无评论 »

Flex 4带给我们的,是全新的命名空间。了解这些命名空间必定是一件好事情。Flex 4有三个非常重要的命名空间,分别是:

  • xmlns:fx=”http://ns.adobe.com/mxml/2009″
  • xmlns:mx=”library://ns.adobe.com/flex/halo”
  • xmlns:s=”library://ns.adobe.com/flex/spark”

1、xmlns:fx=”http://ns.adobe.com/mxml/2009″

fx命名空间下包含ActionScript顶级元素内建到MXML编译器里的标签

前者ActionScript顶级元素就是指 Object、Number、Boolean以及Array对象等等,在《Adobe Flex 4 Beta Language Reference》的顶级包(Top Level)里可以查看完整的顶级元素。后者内建到MXML编译器里的标签包括<fx:Script>、<fx:Declarations>和<fx:Style>等等,在《Adobe Flex 4 Beta Language Reference》附录(Appendixes)里的“MXML Only Tags”可以查看完整的编译器元素。值得注意的是, 这个命名空间并不包含 Halo 和 Spark 组件(Halo是以前版本的组件库,而Spark是Flex 4新出的组件库)。在Flash Builder安装目录下可以找到“mxml-2009-manifest.xml”文件(例如我的电脑是C:\Program Files\Adobe\Flash Builder Beta\sdks\4.0.0\frameworks\mxml-2009-manifest.xml),这个xml文件描述了完整的 ActionScript顶级类。不过你可以注意到,它并没有包含MXML编译器标签,那是因为这些标签是内建到MXML编译器。

2、xmlns:mx=”library://ns.adobe.com/flex/halo”

mx命名空间包含着放在 mx.* 包里那些组件,Flex图表、Flex数据可视化组件等等。对Flex 3很熟悉的话,mx包里的组件你也不陌生了。在Flex 4 SDK的安装目录下可以找到“halo-manifest.xml”文件(例如我的电脑是C:\Program Files\Adobe\Flash Builder Beta\sdks\4.0.0\frameworks\halo-manifest.xml),这个XML文件列出了所有组件了。

3、xmlns:s=”library://ns.adobe.com/flex/spark”

s命名空间可真是新鲜事了,spark.* 包里的组件都在这个命名空间里,不只spark包,flashx.*包的text framework类也在这个命名空间里。这个命名空间包含不少RPC类,例如 WebService、HTTPService和RemoteObject组件,这些组件同时也在mx命名空间下,也就是说,创建这些组件时,你不但可以 使用“S:”前缀,也可以使用“mx:”前缀。其实不只这些RPC组件共用两个命名空间,像graphics、effect和state类也是共用 s 和 mx 命名空间的。在Flex 4 SDK安装目录下打开spark-manifest.xml文件来看看完整的spark组件吧。

以下列出共用 s 和 mx 命名空间的类

Category Class
RPC classes
  • mx.messaging.channels.AMFChannel
  • mx.rpc.CallResponder
  • mx.messaging.ChannelSet
  • mx.messaging.Consumer
  • mx.messaging.channels.HTTPChannel
  • mx.rpc.http.mxml.HTTPService
  • mx.messaging.Producer
  • mx.rpc.remoting.mxml.RemoteObject
  • mx.rpc.remoting.mxml.Operation
  • mx.messaging.channels.RTMPChannel
  • mx.messaging.channels.SecureAMFChannel
  • mx.messaging.channels.SecureStreamingAMFChannel
  • mx.messaging.channels.SecureHTTPChannel
  • mx.messaging.channels.SecureStreamingHTTPChannel
  • mx.messaging.channels.SecureRTMPChannel
  • mx.messaging.channels.StreamingAMFChannel
  • mx.messaging.channels.StreamingHTTPChannel
  • mx.rpc.soap.mxml.WebService
  • mx.rpc.soap.mxml.Operation
  • mx.data.mxml.DataService
Graphics classes
  • mx.graphics.BitmapFill
  • mx.geom.CompoundTransform
  • mx.graphics.GradientEntry
  • mx.graphics.LinearGradient
  • mx.graphics.LinearGradientStroke
  • mx.graphics.RadialGradient
  • mx.graphics.RadialGradientStroke
  • mx.graphics.SolidColor
  • mx.graphics.SolidColorStroke
  • mx.graphics.Stroke
  • mx.geom.Transform
Effedt classes
  • mx.effects.Parallel
  • mx.effects.Sequence
  • mx.states.Transition
  • mx.effects.Wait
States classes
  • mx.states.State
  • mx.states.AddItems

想更快了解Flex 4,可以下载《Adobe Flex 4 Help》看一下。

Tags »||

Flex Logging Framework

Flex/Flash, Flex3 无评论 »

介紹一個Flex提供的Logging framework,這是原生的功能,API可在mx.logging下找到,用起來跟trace差不多,最大的不同是Logging有分成以下五層:

  1. DEBUG:紀錄Flex內部活動。這在應用程式除錯時最為有用
  2. INFO   :紀錄一般資訊
  3. WARN :當應用程式遇到問題時紀錄訊息。這些問題不造成應用程式停止運作,但可能會導致近一步的錯誤
  4. ERROR :當重要的服務無法使用,或是限制應用程式的使用的狀況發生時加以紀錄
  5. FATAL :當導致應用程式失敗的事件發生時加以記錄

愈高層的資訊愈完整,例如WARN就包含WARN、ERROR跟FATAL,而FATAL就只會顯示FATAL

程式範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="main();">
    <mx:Script>
        <![CDATA[
            import mx.logging.ILogger;
            import mx.logging.Log;
            import mx.logging.targets.TraceTarget;
            import mx.logging.LogEventLevel;
 
            private function main():void
            {
               initLogging();
            }
 
            private function initLogging():void
            {
               var logTarget:TraceTarget = new TraceTarget();
               var myLogger:ILogger = Log.getLogger("idv.gd.project.MyClass");
 
               var myLogger2:ILogger = Log.getLogger("idv.gd.project.MyClass2");
 
               logTarget.filters = ["idv.gd.project.*"];
 
               //指定要聽 level
               logTarget.level = LogEventLevel.ALL;
 
               //開啟顯示資訊
               logTarget.includeLevel = true;
               logTarget.includeCategory = true;
               logTarget.includeDate = true;
               logTarget.includeTime = true;
 
               //將Target加入Log
               Log.addTarget(logTarget);
 
               //logger送出訊息,由低至高,高層會顯示低層資訊(error: error & fatal)
               myLogger.debug("myLogger debug");
               myLogger.fatal("myLogger fatal");
 
               myLogger2.info("myLogger2 init");
            }
        ]]>
    </mx:Script>
</mx:Application>

首先使用靜態方法取得ILogger,getLogger(Category),Category是用來過濾用,一般的命名原則同package,雖然亂取也可以但自己別搞混了,再來指定要顯示的LogTarget,預設是提供traceTarget,也可自己改寫,像是寫一個能記錄在文字檔或是回傳給伺服器的都行,接著給logTarget指定filter,filter用來過濾要聽的Category Name,預設為全都聽,可用*字元,比較特殊的是Logging會拆解格式,寫idv.gd.project.MyClass就只會聽到 MyClass,而idv.gd.project.*則聽的到MyClass、MyClass2,另外,TraceTarget也可指定要顯示的資訊,包含Level、Date、Time、Category,指定完後記得加給Log

最後在要顯示資訊的地方用Logger指定Level即可,可設定要顯示的內容,時間Logging會自動加上

Tags »|||

as3强大的正则表达式:RegExp

AS3, Flex/Flash 无评论 »

14.2.2 正则表达式的两个构成部分
简写模式:
var 正则对象:RegExp = /匹配模式/标志位
构造函数模式:
var 正则对象:RegExp = new RegExp(匹配模式,标志位)

匹配模式就是正则表达式,标志位包括五个字母 gimsx

14.3 正则表达式语法
14.3.1 正则表达式中的文字符号
普通文字和符号:A-Z a-z 0-9 以及不是元字符的其它符号
输入Unicode字符时,同String一样,使用\uXXXX

元字符:三三二三个一(记忆口诀)
三个括号: {[(
三个匹配次数: *+? 星加问
两个断言: ^$ 定头定尾
三个一: | 选择符 . 模糊匹配 \ 转义

三个括号:
{定义匹配次数
[字符集
(子模式

三个匹配次数:
* 匹配出现0次或多次
+ 匹配出现至少1次或多次
? 匹配出现0次最多1次

两个断言:
^ 在/后出现时为左端断言, /^
$ 在/前出现时为右端断言, $/

三个独立元字符:
| 选择符,匹配时兼容匹配选择符两边的字符
. 模糊匹配,可以代替

不可见字符:比较一些转义的控制字符

14.3.2 正则表达式中的字符集
1.字符集的概念和最简单的字符集形式
[字符集]

字符集用中括号括起来
Unicode字符请使用\uXXXX 四位十六进制Unicode代码输入
如:/g[eo]t,分别匹配get和got,但不会匹配geot,因为字符集只能匹配一个字符

2.连字符在字符集中的运用
[字符-字符]
如:[a-z][A-Z],就是匹配英文字母字符集了

3.取反符号在字符集中的运用
[^字符集]
添加取反符号后,匹配时将忽略字符集中的所有元素

4.字符集中需要转义的特殊字符
\转义符号
常见要转义的字符有三类:
1.字符串: 双引号” 单引号 ‘ 反斜杠 \
2.正则表达式的源字符
3.字符集中的: [\^-

尽可能将会产生歧义的字符转义

14.3.3 特殊的点号(.)
.号可以替代任意单个字符,作为正则表达式中的模糊匹配字符

1.点号不匹配新行符(\n)
多行字符的模糊匹配中,.号不能匹配\n换行符,要匹配换行符,必须在标志位中使用s

2.尽量少使用点号“.”
只在明确不会模糊匹配时出错的情况下才用.模糊匹配,点号匹配所有的Unicode字符。

14.3.4 选择符
| 对选择符两边的元素作任意匹配。
使用|选择符时,尽可能使用括号将两边的操作数括起来,以方便区别。

14.3.5 括号与分组
() 圆括号将表达式的一部分括起来,就会将这部分定交成组,一旦命名了组,就可以通过“向后引用”来调用它。
它所括起的部分一般用于:
1.和选择符搭配,规定作用于选择符的范围,如/he(got|get)it/
2.和限定符搭配使用,指定匹配的重复次数,如/(get)*/

14.3.6 用正则表达式描述字符串的重复
使用{}定义匹配的重复次数,跟在要重复的表达式或字符后面
{最小出现次数[,最多出现次数]}
还有常用的三个 非显式限定符

*+? 星加问

* 出现0次或多次 等同 {0,}
+ 出现1次或多次 等同 {1,}
? 出现0次或最多1次 等同 {0,1}

14.3.7 注意正则表达式的贪婪性和懒惰性
使用*+?{}的时候,正则表达式总是尽可能向右搜寻尽可能多的匹配结果,如果想禁止此贪婪性,可在操作符后加上 ?,迫使程序运行时使用懒惰模式,优先使用左边匹配的第一个结果。

14.3.8 用正则表达式来定位
断言:指定匹配发生的位置
^ 匹配字符串起始的位置,一般跟在起始/后,组合成 /^
$ 匹配字符串结尾的位置,一般跟在结束/前,组合成$/
\b 匹配字符边界
\B 匹配非字符边界

字符边界指空格的位置,可跟在要匹配的表达式左边或右边

14.3.9 正则表达式的5个标志位。
g global 全局匹配,尽可能匹配全部结果
i ignoreCase 不区分大小写匹配
m multiline 断言控制符,使用后 ^$两个断言符才能生效
s dotall 匹配换行符标志
x extended 令正则表达式支持空格,但编译时会自动忽略,增加可视的友好度

14.3.10 正则表达式元字符优先线
优先级顺序从高到低排列

\ 转义符
(),(?:),(?=),[] 分级定义符号,字符集定义符号
*,+,?,{n},{n,},{n,m} 限定符
^,$,\位置定义 位置和顺序
| 选择符

14.4 正则表达式与字符串的结合使用详解
14.4.1 RegExp的exec()和test()
test方法
var RegExp对象:RegExp = /正则表达式/ 或 new RegExp(正则式,标志位)
var String对象:String = 字符;
RegExp对象.test(String对象)

RegExp对象.exec(正则表达式):Array
exec返回一个匹配后的数组

14.4.3 String类的replace()与正则表达式向后引用
()使用子表达式括起的表达式,可以在后边引用 ,使用\n n为索引 从1至99
/\b([a-z]+) \1\b/gi 其中的\1就是引用前面第一个([a-z])
可在处理函数中用 $n 来引用子模式

Tags »|