Flash FMS Helloword

FMS 在安装后有一些DEMO

安装路径\Adobe\Flash Media Server 3.5\documentation\samples 

这里有很多例子

 

applications文件夹

你可以在里边建一个文件夹例如叫 FirstApp,这就建了一个Application,以后所有此项目服务器端的flv文件啊,共享文件啊都会在这里边,有时服务器端需要写程序的时候 里边会有个main.asc 文件,这个就是服务器端程序,服务器端程序目前只支持as1写,是在服务器上执行的,你也可以用trace调试,怎么trace管理服务器时候你会看到, 你可能会建若干个app,在一台服务器上同时运行,比如录象的app,聊天室的app。。。这些所有的app都通过fms自带的 fms2_console.swf管理,现在就去看看如何管理服务器吧!

管理服务器~
点开Tools/fms_adminConsole.swf,别看他只是个swf,后台管理就靠他了,输入你安装时候的密码和用户,服务器地址写 localhost 就好看到了吧,熟悉熟悉他吧




以 后我们写程序最常用的就是这个View Applications ,每当有客户端连接服务器时,左边就会列出连接的是哪个app,有多少连接 下图为连接applications文件夹下的bs文件夹(bs文件夹,因为每个文件夹对应一个app,就是bs app),连接数为1,458是实例名(实例名默认为 _definst_ ,每个文件夹可以有若干实例,互相不影响,这个特性可以用来做聊天是的房间,以后再说)




选中某个app后,或者客户端有连接,会看到当前打开的app的状态,这个




Live Log 服务器端的trace就这里看了 ,右边依次是客户端情况 , 共享对象,流,执行的情况(占内存,cpu等),后边两个小按钮,调试时候常用哦,reload和unload!
每当服务器端main.asc修改后一定记得reload或者unload一下,否则不会生效,unload会把窗口关闭,有客户端连的时候还会自动打开
总有人告诉我,他的fms经常会出现诡异现象,比如连接不上,代码已经删了还会执行,一生气连文件夹都删了,还会执行,怀疑是自己的rp有问题。。
那不是rp问题,记住出现问题就reload !实在不行就去tools文件夹点StopServerService.bat

conf文件夹

还有重要的是conf文件夹了,里边是一些服务器端的配置文件,以后可能会用,先不用动。。。。

自此,安装部分结束,go on...

有一种协议叫rtmp

 

RTMP协议概述
介绍:

RTMP协议是被Flash用于对象,视频,音频的传输.该协议建立在TCP协议或者轮询HTTP协议之上.
RTMP协议就像一个用来装数据包的容器,这些数据可以是AMF格式的数据,也可以是FLV中的视/音频数据.
一个单一的连接可以通过不同的通道传输多路网络流.这些通道中的包都是按照固定大小的包传输的.

网络连接(Connection)

一个Actionscript连接并播放一个流的简单代码:
复制内容到剪贴板
代码:
var videoInstance:Video = your_video_instance;
var nc:NetConnection = new NetConnection();
var connected:Boolean = nc.connect("rtmp:/localhost/myapp");
var ns:NetStream = new NetStream(nc);
videoInstance.attachVideo(ns);
ns.play("flvName");
默认端口为1935

握手

Client → Server :向服务器发出握手请求.这不属于协议包一部分,该握手请求第一个字节为(0×03),其后跟着1536个字节.看上去这部分的内容对于RTMP协议来说并不是至关重要的,但也不可随意对待.
Server → Client :服务器向客户端回应握手请求.这部分的数据仍然不属于RTMP协议的部分.该回应的其实字节仍然为(0x03),但是后边跟着个长度为1536个字节 (一共为3072 )的包块.第一个1536块看上去似乎可以是任意内容,甚至好像可以是Null都没有关系.第二个1536的代码块,是上一步客户端向服务器端发送的握手请求的内容.
Client→Server:把上一步服务器向客户端回应的第二块1536个字节的数据块.

至此客户端与服务器端的握手结束,下面将发送RTMP协议的包内容.

Client → Server :向服务器发送连接包.
Server → Client :服务器回应.

... .... 等等... ...

 接着上面说例子

打开  HelloWorld 文件夹

 

其中HelloWorld.asc

ASC文件用于存储将在运行 Flash Media Server 的计算机上执行的 ActionScript 的文件。

这些文件提供了实现与 SWF 文件中的 ActionScript 结合使用的服务器端逻辑的功能。

HelloWorld.asc 放到 applications文件夹下新建的HelloWorld文件夹里就OK了。

 HelloWorld.asc文件内容

 

代码
/*
 * (C) Copyright 2007 Adobe Systems Incorporated. All Rights Reserved.
 *
 * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the 
 * terms of the Adobe license agreement accompanying it.  If you have received this file from a 
 * source other than Adobe, then your use, modification, or distribution of it requires the prior 
 * written permission of Adobe. 
 * THIS CODE AND INFORMATION IS PROVIDED "AS-IS" WITHOUT WARRANTY OF
 * ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 *
 *  THIS CODE IS NOT SUPPORTED BY Adobe Systems Incorporated.
 *
 
*/

// Server-side action taken upon connect request.
application.onConnect = function( client ) {

    
// Define new client function for a nc.call().
    client.serverHelloMsg = function( helloStr ) {
        
return "Hello, " + helloStr + "!";
    }
        
    
// Accept the connection.
    application.acceptConnection( client );
}

 Application类的事件处理器汇总
Application.onAppStart 当这个应用程序被服务器装载时调用。

Application.onAppStop 当这个应用程序被服务器卸载时调用。

Application.onConnect 当一个客户机连接到这个应用程序时调用。

Application.onAppStop 当这个应用程序被服务器卸载时调用。

Application.onConnect 当一个客户机连接到这个应用程序时调用。

Application.onConnectAccept 当一个客户机成功的连接到这个应用程序时调用;仅用于通讯组件。

Application.onConnectReject 当一个客户机连接到这个应用程序失败时调用;仅用于通讯组件。

Application.onDisconnect 当一个客户机从这个应用程序断开连接时调用。

Application.onStatus 当一个脚本产生一个错误时调用。

 

我们用CS3打个 HelloWorld.fla 测式运行OK了

 

最后我们看看 HelloWorld.fla 对应的角本文件HelloWorld.as

代码
/*
 * (C) Copyright 2007 Adobe Systems Incorporated. All Rights Reserved.
 *
 * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the 
 * terms of the Adobe license agreement accompanying it.  If you have received this file from a 
 * source other than Adobe, then your use, modification, or distribution of it requires the prior 
 * written permission of Adobe. 
 * THIS CODE AND INFORMATION IS PROVIDED "AS-IS" WITHOUT WARRANTY OF
 * ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 *
 *  THIS CODE IS NOT SUPPORTED BY Adobe Systems Incorporated.
 *
 
*/

package {
    
    import flash.display.MovieClip;
    import flash.net.Responder;
    import flash.net.NetConnection;
    import flash.events.NetStatusEvent;
    import flash.events.MouseEvent;
    
    
    public class HelloWorld extends MovieClip {
              
      
// Represents a network connection.
      private var nc:NetConnection;
      
      
// Responder for call to server's serverHelloMsg -- see onReply() below.
      private var myResponder:Responder = new Responder(onReply);

        
      
// Constructor.
      public function HelloWorld() {
            
// Set display values.
            textLbl.text = "";
            connectBtn.label = "Connect";

            
// Register a listener for mouse clicks on the button.
            connectBtn.addEventListener(MouseEvent.CLICK, connectHandler);
       }
      
    
     
// When button is pressed, connect to or disconnect from the server.
     public function connectHandler(event:MouseEvent):void {
            
if (connectBtn.label == "Connect") {
                trace("Connecting");
                
                nc = new NetConnection();

                
// Connect to the server.
                nc.connect("rtmp://localhost/HelloWorld");
                
                
// Call the server's client function serverHelloMsg, in HelloWorld.asc.
                nc.call("serverHelloMsg", myResponder, "World");
                
                connectBtn.label = "Disconnect";
                
            } else {            
                trace("Disconnecting");
                
                
// Close the connection.
                nc.close();
                
                connectBtn.label = "Connect";
                textLbl.text = "";
            }
     }

     
// Responder function for nc.call() in connectHandler(). 
     private function onReply(result:Object):void {
            trace("onReply received value: " + result);
            textLbl.text = String(result);
     }

   }
}

 

 

 

 

 

 

 

posted @ 2009-11-30 16:27  天纯蓝  阅读(470)  评论(0编辑  收藏  举报