(译)Learning Flash Media Server 3中文版-第7章之4/5
到目前为止,File.write()是唯一用来写数据的方法,File.readAll()是唯一用来读取文件内容的方法。可是,在服务端你仍然有更多的选择。表7-1中显示了File类中所有写和读的方法。
表7-1
| File 方法 | 说明 |
| write(p) | 把每一个参数转换成一个字符串, 写入一个文件 |
| writeAll(arr) | 把一个数组参数写入打开的文件,并在数组中的每一个元素后面加上换行(我使用汉字的时候只写入了第一个元素,后面的写不进去,不知道是不是bug,后面再来研究一下) |
| writeByte(n) | 把一个字节写入一个文件 |
| writen(p) | 把数据写入到一个文件并在最后一个参数的后面添加一个与平台有关的行尾字符 |
| read(n) | 从一个文件中读取n个字符数并返回一个字符串 |
| readAll( ) | 从文件指针的位置后读取文件并返回一个数组,数组中的每一个元素对应文件中的每一行 |
| readByte() | 从这个文件中读取下一个字节并返回下一个字节的数字值 |
| readln() | 从这个文件中读取下一行并作为一个字符串返回 |
使用File.write(),FMS3只是简单地把数据写入文件,传递给客户端。使用File.writeln(),FMS3添加了一个换行符。意思就是在客户端脚本中你不需要添加\n符,当数据发送到服务端时。
In reading the data back, instead of sending an array with a single statement as in the File.readAll() method, File.readln() reads the file a line at a time as a string. When you’re searching for a term in a file, reading a single line at a time can be handy if you have a search word on each line, such as a customer number or name. Then you can extract the single line and send it to the client-side for viewing.
First, use File –> Save As to save the fileIt.asc file as fileEof.asc. Then change the write and read functions to the following and save it as fileIt.asc:
Code View:
fileWriter.WriteNow = function(cliMsg) { sandFile.open("text", "append"); if (sandFile.open) { sandFile.writeln(cliMsg); sandFile.close( ); } trace(cliMsg); };
fileWriter.ReadNow = function( ) { sandFile.open("text", "read"); var contentNow = new Array(); if (sandFile.isOpen) { var counter =0; while(!sandFile.eof()) { readBuf=sandFile.readln(); contentNow[counter]=readBuf; counter++; } return contentNow; sandFile.close( ); } };
};
The major difference in these functions is employment of the File.eof() method. Using File.readAll() places the entire contents of the file into an array. In this case, the File.eof() function first iterates through the lines in the file and then puts the elements into the array one at a time until the end-of-file condition is met.
7.4.1. 服务端数据初始化
假设你想要找出谁在使用你的应用程序的信息,想要这些从服务端的登录信息中被初始化。当用户登录到你的应用程序中,通常它们会输入一个用户名,如果想要把登录的用户名和用户的IP地址放到一个文件中,这是可以办到的。如果你觉得某个用户有问题,你可以使用IP地址来拒绝他连接到应用程序。所有这些都可以在服务端完成。请参考下面的代码:
-
在服务端,建立一个叫fileIP的目录
-
在fileIp目录下建立一个fileIP.asc 文件
-
在fileIP.asc 文件中输入以下代码
Example 7-4. fileIP.asc
application.onAppStart = function(){ trace("Trap IP is up");};application.onConnect = function(client, name){ application.acceptConnection(client); client.id = name; var ip = client.ip; var ipFile = new File("ip.txt"); ipFile.open("text", "append"); if (ipFile.isOpen) { ipFile.writeln(client.id, ip); ipFile.close(); } trace(client.id+"'s IP address is "+ip);};application.onDisconnect = function(client){ trace(client.id+" has left.");};
在Application.onConnect()中触发打开文件和记录当前的用户名名和IP地址。
客户端代码如下:
Example 7-5. IPfile.as
Code View:
package{ import flash.net.NetConnection; import flash.events.NetStatusEvent; import flash.events.MouseEvent; import flash.display.Sprite; import fl.controls.Button; import fl.controls.TextInput;
public class IPfile extends Sprite { private var nc:NetConnection; private var textInput:TextInput; private var logBtn:Button; private var rtmpNow:String; private var good:Boolean;
public function IPfile () { nc=new NetConnection(); nc.addEventListener (NetStatusEvent.NET_STATUS,checkConnect); rtmpNow="rtmp://192.168.0.11/fileIP/getIP/"; textInput=new TextInput(); textInput.x=20, textInput.y=20; addChild (textInput);
logBtn=new Button(); logBtn.x=20, logBtn.y=45; logBtn.width=120; logBtn.label="Enter name and click"; logBtn.addEventListener (MouseEvent.CLICK, logOn); addChild (logBtn); }
private function logOn (e:MouseEvent) { nc.connect (rtmpNow,textInput.text); }
private function checkConnect (e:NetStatusEvent):void { good=e.info.code == "NetConnection.Connect.Success"; if (good) { textInput.text="IP Stored"; } else { textInput.text="Failed Connection"; } } }}
The user enters his or her name and clicks the Logon button, labeled Enter Name And Click. This sends the name of the user to the server-side script as the second argument in the server-side line:
application.onConnect = function(client, name) {}
Creating a client.id property with the name passed from the client side and a variable, ip with the value from the client.ip property places both into the ip.txt file using File.writln()., Figure 7-5 shows how the values of two different names from two different connections on a LAN are stored.
Figure 7-5. User names and IP addresses stored in file
注意:当在File.writeln()方法中使用多个参数,值都会被放在单独的行中。当取得数据时,只需要使用其中的一种read方法。



